Documentation
¶
Overview ¶
Package output provides utilities for formatting and outputting CLI command results.
Index ¶
- Constants
- Variables
- func AgentModeError(cmd *cobra.Command, message string, hints []string) error
- func AlreadyPrinted(err error) error
- func ApplyJqFilter(content interface{}, expression string) ([]interface{}, error)
- func CLIError(cmd *cobra.Command, err error) error
- func ColorizeJSON(data []byte) []byte
- func Error(cmd *cobra.Command, err error) error
- func ExitCodeFor(classification Classification) int
- func InitAgentMode(cmd *cobra.Command)
- func InstallErrorHandling(root *cobra.Command)
- func IsAgentMode() bool
- func IsAlreadyPrinted(err error) bool
- func IsMachineMode(cmd *cobra.Command) bool
- func IsRendered(err error) bool
- func LocalResult(cmd *cobra.Command, res interface{}) error
- func PrepareCallOpts(cmd *cobra.Command) ([]operations.Option, error)
- func PreparseRenderingFlags(target *cobra.Command, args []string)
- func ResetAgentMode()
- func ResetPreparsedRendering()
- func Result(cmd *cobra.Command, res interface{}) error
- func ShouldColorize(colorFlag string) bool
- func UsageHelpError(cmd *cobra.Command, err error) error
- func ValidateGlobalServerIndex(cmd *cobra.Command, count int) error
- func ValidateGlobalServerName(cmd *cobra.Command, validNames map[string]string) error
- func WantsRawJSON(cmd *cobra.Command) bool
- func WithCLIReason(err error, reason string) error
- type Classification
- type ErrorOrigin
- type ErrorType
- type StreamEventError
Constants ¶
const ( ReasonCLIValidation = "CLI_VALIDATION" ReasonCLIConnection = "CLI_CONNECTION" ReasonCLIProtocol = "CLI_PROTOCOL" ReasonCLIRuntime = "CLI_RUNTIME" ReasonCLIAuthentication = "CLI_AUTHENTICATION" ReasonCLIAsyncFailed = "CLI_ASYNC_FAILED" ReasonCLIAsyncTimeout = "CLI_ASYNC_TIMEOUT" ReasonCLIAsyncUnknownState = "CLI_ASYNC_UNKNOWN_STATE" )
The generator validates x-speakeasy-cli-commands hint reasons against this list.
Variables ¶
var Formats = []string{"pretty", "json", "yaml", "table", "toon"}
Formats lists the accepted --output-format values, in help order.
Functions ¶
func AlreadyPrinted ¶ added in v0.1.10
AlreadyPrinted wraps err in a marker type so callers (specifically cmd/compass/main.go) can detect that the friendly error rendering has already happened and suppress the default `fmt.Fprintln(os.Stderr, err)`. Callers that don't recognize the wrapper still get a working error value: .Error() returns the underlying message and errors.Is/As both work via Unwrap.
func ApplyJqFilter ¶
ApplyJqFilter applies a jq expression to the given content and returns the results. The content is first marshaled to JSON and back to ensure a clean interface{} structure, then the jq expression is evaluated against it.
func ColorizeJSON ¶
ColorizeJSON adds ANSI color codes to formatted JSON output. It distinguishes keys from string values, and highlights numbers, booleans, and null.
func ExitCodeFor ¶ added in v0.1.68
func ExitCodeFor(classification Classification) int
func InitAgentMode ¶
InitAgentMode applies the explicit --agent-mode setting.
func InstallErrorHandling ¶ added in v0.1.68
InstallErrorHandling runs after usage.Intercept, which rewrites the hooks and Args wrapped here.
func IsAlreadyPrinted ¶ added in v0.1.10
IsAlreadyPrinted reports whether err (or any error in its chain) was returned from output.Error() and has therefore already been rendered to the user. main.go uses this to skip the default error print so the message isn't duplicated.
func IsMachineMode ¶ added in v0.1.68
func IsRendered ¶ added in v0.1.68
func LocalResult ¶ added in v0.1.68
func PrepareCallOpts ¶
func PrepareCallOpts(cmd *cobra.Command) ([]operations.Option, error)
PrepareCallOpts builds common SDK call options from CLI flags. Parses --header flags into operations.WithSetHeaders options. Server resolution and skip-deserialization are handled per-operation.
func PreparseRenderingFlags ¶ added in v0.1.68
func ResetPreparsedRendering ¶ added in v0.1.68
func ResetPreparsedRendering()
ResetPreparsedRendering clears the rendering flags captured by PreparseRenderingFlags. Every parse starts from a reset so a later ExecuteRoot in the same process never inherits an earlier invocation's --output-format or --jq; the explore hand-off forwards the outer invocation's rendering flags in argv (see ExplorerHandoffArgs), so they are captured again there. Test harnesses that drive output helpers on hand-built commands without re-parsing call this directly.
func ShouldColorize ¶
ShouldColorize determines whether output should include ANSI color codes. It checks agent mode, the --color flag value, NO_COLOR / FORCE_COLOR env vars, and TTY status.
func ValidateGlobalServerIndex ¶
ValidateGlobalServerIndex validates the --server flag as an integer index in range [0, count). Provides clear error messages for invalid values. The actual server application happens in client.NewClient via sdk.WithServerIndex (which handles server URL template variable resolution).
func ValidateGlobalServerName ¶
ValidateGlobalServerName validates the --server flag against a named server map. The actual server application happens in client.NewClient via sdk.WithServer.
func WantsRawJSON ¶
WantsRawJSON returns true when the user has requested JSON output format (either --output-format=json or --jq is set), meaning the CLI should prefer raw JSON passthrough over typed-struct marshaling.
func WithCLIReason ¶ added in v0.1.68
Types ¶
type Classification ¶ added in v0.1.68
type Classification struct {
Type ErrorType
Reason string
StatusCode int
Message string
Hints []string
Body any
RawBody string
Origin ErrorOrigin
// contains filtered or unexported fields
}
func ClassificationFrom ¶ added in v0.1.68
func ClassificationFrom(err error) (Classification, bool)
type ErrorOrigin ¶ added in v0.1.68
type ErrorOrigin string
const ( ErrorOriginAPI ErrorOrigin = "api" ErrorOriginCLI ErrorOrigin = "cli" ErrorOriginStream ErrorOrigin = "stream" )
type ErrorType ¶ added in v0.1.68
type ErrorType string
const ( ErrorTypeAuthentication ErrorType = "authentication_error" ErrorTypeAuthorization ErrorType = "authorization_error" ErrorTypeNotFound ErrorType = "not_found" ErrorTypeValidation ErrorType = "validation_error" ErrorTypeRateLimit ErrorType = "rate_limit_error" ErrorTypeServer ErrorType = "server_error" ErrorTypeConnection ErrorType = "connection_error" ErrorTypeProtocol ErrorType = "protocol_error" ErrorTypeAPI ErrorType = "api_error" ErrorTypeRuntime ErrorType = "runtime_error" ErrorTypeUnsupported ErrorType = "unsupported_error" ErrorTypeAsyncFailed ErrorType = "async_failed" ErrorTypeAsyncTimeout ErrorType = "async_timeout" ErrorTypeAsyncUnknownState ErrorType = "async_unknown_state" )
type StreamEventError ¶ added in v0.1.68
type StreamEventError struct {
Body string
}
StreamEventError is an in-band error event; Body holds that event's JSON.
func (*StreamEventError) Error ¶ added in v0.1.68
func (e *StreamEventError) Error() string