Documentation
¶
Overview ¶
Package output provides unified output formatting for CLI commands. It supports multiple output formats (table, JSON, YAML) and structured errors.
Index ¶
- Constants
- func AddHelpJSONFlag(cmd *cobra.Command)
- func ResolveFormat(outputFlag string, jsonFlag bool) string
- func SetupHelpJSON(rootCmd *cobra.Command)
- type CommandInfo
- type FlagInfo
- type FormatConfig
- type HelpInfo
- type JSONFormatter
- type OutputFormatter
- type StructuredError
- func (e StructuredError) Error() string
- func (e StructuredError) WithContext(key string, value interface{}) StructuredError
- func (e StructuredError) WithGuidance(guidance string) StructuredError
- func (e StructuredError) WithRecoveryCommand(cmd string) StructuredError
- func (e StructuredError) WithRequestID(requestID string) StructuredError
- type TableFormatter
- type YAMLFormatter
Constants ¶
const ( ErrCodeConfigNotFound = "CONFIG_NOT_FOUND" ErrCodeServerNotFound = "SERVER_NOT_FOUND" ErrCodeDaemonNotRunning = "DAEMON_NOT_RUNNING" ErrCodeInvalidOutputFormat = "INVALID_OUTPUT_FORMAT" ErrCodeAuthRequired = "AUTH_REQUIRED" ErrCodeConnectionFailed = "CONNECTION_FAILED" ErrCodeTimeout = "TIMEOUT" ErrCodePermissionDenied = "PERMISSION_DENIED" ErrCodeInvalidInput = "INVALID_INPUT" ErrCodeOperationFailed = "OPERATION_FAILED" )
Common error codes for CLI operations
Variables ¶
This section is empty.
Functions ¶
func AddHelpJSONFlag ¶
AddHelpJSONFlag adds a --help-json flag to a command. When invoked, it outputs structured help information as JSON.
func ResolveFormat ¶
ResolveFormat determines the output format from flags and environment. Priority: explicit flag > --json alias > MCPPROXY_OUTPUT env var > default (table)
func SetupHelpJSON ¶
SetupHelpJSON sets up --help-json on a command tree. It adds the flag and hooks to check for it on all commands.
Types ¶
type CommandInfo ¶
type CommandInfo struct {
// Name is the subcommand name
Name string `json:"name"`
// Description is a short description of the subcommand
Description string `json:"description"`
// Usage shows how to use the subcommand
Usage string `json:"usage"`
// HasSubcommands indicates if this command has further subcommands
HasSubcommands bool `json:"has_subcommands,omitempty"`
}
CommandInfo contains information about a subcommand.
type FlagInfo ¶
type FlagInfo struct {
// Name is the long flag name (e.g., "output")
Name string `json:"name"`
// Shorthand is the short flag (e.g., "o")
Shorthand string `json:"shorthand,omitempty"`
// Description describes what the flag does
Description string `json:"description"`
// Type is the flag type (string, bool, int, etc.)
Type string `json:"type"`
// Default is the default value
Default string `json:"default,omitempty"`
// Required indicates if the flag is required
Required bool `json:"required,omitempty"`
}
FlagInfo contains information about a command flag.
type FormatConfig ¶
type FormatConfig struct {
// Format is the output format (table, json, yaml)
Format string
// NoColor disables ANSI color codes
NoColor bool
// Quiet suppresses non-essential output
Quiet bool
// Pretty enables human-readable formatting (indentation, etc.)
Pretty bool
}
FormatConfig holds configuration for output formatting behavior.
func DefaultConfig ¶
func DefaultConfig() FormatConfig
DefaultConfig returns the default format configuration.
func (FormatConfig) WithFormat ¶
func (c FormatConfig) WithFormat(format string) FormatConfig
WithFormat returns a copy with the specified format.
func (FormatConfig) WithNoColor ¶
func (c FormatConfig) WithNoColor(noColor bool) FormatConfig
WithNoColor returns a copy with NoColor set.
func (FormatConfig) WithQuiet ¶
func (c FormatConfig) WithQuiet(quiet bool) FormatConfig
WithQuiet returns a copy with Quiet set.
type HelpInfo ¶
type HelpInfo struct {
// Name is the command name
Name string `json:"name"`
// Description is a short description of the command
Description string `json:"description"`
// Usage shows how to use the command
Usage string `json:"usage"`
// Flags lists all available flags for this command
Flags []FlagInfo `json:"flags,omitempty"`
// Commands lists subcommands (for parent commands)
Commands []CommandInfo `json:"commands,omitempty"`
}
HelpInfo contains structured help information for a command hierarchy.
func ExtractHelpInfo ¶
ExtractHelpInfo builds a HelpInfo struct from a cobra.Command.
type JSONFormatter ¶
type JSONFormatter struct {
Indent bool // Whether to pretty-print with indentation
}
JSONFormatter formats output as JSON.
func (*JSONFormatter) Format ¶
func (f *JSONFormatter) Format(data interface{}) (string, error)
Format marshals data to JSON.
func (*JSONFormatter) FormatError ¶
func (f *JSONFormatter) FormatError(err StructuredError) (string, error)
FormatError marshals a structured error to JSON.
func (*JSONFormatter) FormatTable ¶
func (f *JSONFormatter) FormatTable(headers []string, rows [][]string) (string, error)
FormatTable converts tabular data to a JSON array of objects.
type OutputFormatter ¶
type OutputFormatter interface {
// Format converts data to formatted string output.
// data should be a struct, slice, or map that can be marshaled.
Format(data interface{}) (string, error)
// FormatError converts a structured error to formatted output.
FormatError(err StructuredError) (string, error)
// FormatTable formats tabular data with headers.
// headers defines column names, rows contains data.
FormatTable(headers []string, rows [][]string) (string, error)
}
OutputFormatter formats structured data for CLI output. Implementations are stateless and thread-safe.
func NewFormatter ¶
func NewFormatter(format string) (OutputFormatter, error)
NewFormatter creates a formatter for the specified format. Supported formats: table, json, yaml (case-insensitive).
type StructuredError ¶
type StructuredError struct {
// Code is a machine-readable error identifier (e.g., "CONFIG_NOT_FOUND")
Code string `json:"code" yaml:"code"`
// Message is a human-readable error description
Message string `json:"message" yaml:"message"`
// Guidance provides context on why this error occurred
Guidance string `json:"guidance,omitempty" yaml:"guidance,omitempty"`
// RecoveryCommand suggests a command to fix the issue
RecoveryCommand string `json:"recovery_command,omitempty" yaml:"recovery_command,omitempty"`
// Context contains additional structured data about the error
Context map[string]interface{} `json:"context,omitempty" yaml:"context,omitempty"`
// RequestID is the server-generated request ID for log correlation (T023)
RequestID string `json:"request_id,omitempty" yaml:"request_id,omitempty"`
}
StructuredError represents errors with machine-parseable metadata for AI agent recovery.
func FromError ¶
func FromError(err error, code string) StructuredError
FromError converts a standard error to a StructuredError.
func NewStructuredError ¶
func NewStructuredError(code, message string) StructuredError
NewStructuredError creates a new StructuredError with the given code and message.
func (StructuredError) Error ¶
func (e StructuredError) Error() string
Error implements the error interface for StructuredError.
func (StructuredError) WithContext ¶
func (e StructuredError) WithContext(key string, value interface{}) StructuredError
WithContext adds context data to the error.
func (StructuredError) WithGuidance ¶
func (e StructuredError) WithGuidance(guidance string) StructuredError
WithGuidance adds guidance to the error.
func (StructuredError) WithRecoveryCommand ¶
func (e StructuredError) WithRecoveryCommand(cmd string) StructuredError
WithRecoveryCommand adds a recovery command suggestion.
func (StructuredError) WithRequestID ¶
func (e StructuredError) WithRequestID(requestID string) StructuredError
WithRequestID adds a request ID for log correlation (T023).
type TableFormatter ¶
type TableFormatter struct {
NoColor bool // Disable ANSI colors
Unicode bool // Use Unicode box-drawing characters
Condensed bool // Simplified output for non-TTY
}
TableFormatter formats output as a human-readable table.
func (*TableFormatter) Format ¶
func (f *TableFormatter) Format(data interface{}) (string, error)
Format renders data as a formatted table. data must be a slice of structs or maps.
func (*TableFormatter) FormatError ¶
func (f *TableFormatter) FormatError(err StructuredError) (string, error)
FormatError renders an error in human-readable format.
func (*TableFormatter) FormatTable ¶
func (f *TableFormatter) FormatTable(headers []string, rows [][]string) (string, error)
FormatTable renders tabular data with headers and alignment.
type YAMLFormatter ¶
type YAMLFormatter struct{}
YAMLFormatter formats output as YAML.
func (*YAMLFormatter) Format ¶
func (f *YAMLFormatter) Format(data interface{}) (string, error)
Format marshals data to YAML.
func (*YAMLFormatter) FormatError ¶
func (f *YAMLFormatter) FormatError(err StructuredError) (string, error)
FormatError marshals a structured error to YAML.
func (*YAMLFormatter) FormatTable ¶
func (f *YAMLFormatter) FormatTable(headers []string, rows [][]string) (string, error)
FormatTable converts tabular data to YAML.