cmdutil

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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 CacheDir

func CacheDir() (string, error)

CacheDir returns the directory used for the on-disk response cache.

func Code

func Code(s string) string

Code normalizes a broker-code path argument (e.g. mg -> MG).

func Do

func Do(cmd *cobra.Command, fn func(*sectors.ClientWithResponses) (int, []byte, error)) error

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

func Emit(cmd *cobra.Command, statusCode int, body []byte) error

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

func Fail(status int, msg string, body []byte) error

Fail renders a structured error to stderr and returns a *HandledError whose exit code reflects the status category.

func Format

func Format(cmd *cobra.Command) output.Format

Format returns the validated --output format for this command.

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 OptBool

func OptBool(cmd *cobra.Command, name string, val bool) *bool

func OptEnum

func OptEnum[T ~string](cmd *cobra.Command, name, val string) *T

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.

func OptFloat

func OptFloat(cmd *cobra.Command, name string, val float64) *float64

func OptInt

func OptInt(cmd *cobra.Command, name string, val int) *int

func OptStr

func OptStr(cmd *cobra.Command, name, val string) *string

func Slug

func Slug(s string) string

Slug normalizes a kebab-case slug path argument (e.g. Banks -> banks).

func Sym

func Sym(s string) string

Sym normalizes a ticker path argument (e.g. bbca -> BBCA).

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL