Documentation
¶
Overview ¶
Package cmdutil holds helpers shared across every command package: building an authenticated client, rendering responses/errors, and turning optional flags into the pointer-typed query parameters the generated client expects.
Command packages depend on this; this package depends on no command package, so there are no import cycles.
Index ¶
- Constants
- func CacheDir() (string, error)
- func Code(s string) string
- func Do(cmd *cobra.Command, fn func(*sectors.ClientWithResponses) (int, []byte, error)) error
- func Emit(cmd *cobra.Command, statusCode int, body []byte) error
- func Fail(status int, msg string, body []byte) error
- func Format(cmd *cobra.Command) output.Format
- func NewClient(cmd *cobra.Command) (*sectors.ClientWithResponses, error)
- func OptBool(cmd *cobra.Command, name string, val bool) *bool
- func OptEnum[T ~string](cmd *cobra.Command, name, val string) *T
- func OptFloat(cmd *cobra.Command, name string, val float64) *float64
- func OptInt(cmd *cobra.Command, name string, val int) *int
- func OptStr(cmd *cobra.Command, name, val string) *string
- func Slug(s string) string
- func Sym(s string) string
- type HandledError
Constants ¶
const ( FlagAPIKey = "api-key" FlagBaseURL = "base-url" FlagOutput = "output" FlagTimeout = "timeout" FlagRetries = "retries" FlagRetryWait = "retry-max-wait" FlagSelect = "select" FlagMax = "max" FlagCount = "count" FlagVerbose = "verbose" FlagDryRun = "dry-run" FlagNoCache = "no-cache" FlagCacheTTL = "cache-ttl" )
Global persistent flag names, defined once on the root command and read here from any (possibly deeply nested) subcommand via inherited flags.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do builds a client, runs fn, and emits the result. fn performs one API call and returns its HTTP status code and raw body. It keeps no-parameter commands down to a single expression.
func Emit ¶
Emit checks the HTTP status and either prints the body as JSON or renders an error. Use it for every command's response handling.
func Fail ¶
Fail renders a structured error to stderr and returns a *HandledError whose exit code reflects the status category.
func NewClient ¶
func NewClient(cmd *cobra.Command) (*sectors.ClientWithResponses, error)
NewClient builds an authenticated API client from the resolved configuration (flag → env → config file precedence for the API key and base URL).
func OptEnum ¶
OptEnum is like OptStr but for the generated string-enum param types. The value is validated server-side, so an invalid one surfaces as a 400.
Types ¶
type HandledError ¶
type HandledError struct{ Code int }
HandledError signals that a command already rendered its own error to stderr. The top-level Execute exits with Code without printing anything more.
func (*HandledError) Error ¶
func (e *HandledError) Error() string