Documentation
¶
Index ¶
- Constants
- Variables
- func ArgScope(cmd *cobra.Command) int
- func BindLegacyEnv(w io.Writer)
- func CompleteArgs(sources ...*ArgSource) cobra.CompletionFunc
- func Confirm(cmd *cobra.Command, action string, args []string) error
- func ConfirmScoped(cmd *cobra.Command, action string, args []string, typed bool) error
- func EnumCheck(cmd *cobra.Command, flag string, allowed []string) error
- func FitWidth(s string, width int) string
- func FlagErrorf(format string, a ...any) error
- func FormatCheck(cmd *cobra.Command, flag, format string) error
- func GroupRunE(cmd *cobra.Command, args []string) error
- func HandleError(w io.Writer, cmd *cobra.Command, err error) int
- func IntPtrOrNil(i int) *int
- func Interactive(cmd *cobra.Command) bool
- func IsRequired(f *pflag.Flag) bool
- func JSONValue(raw, flag string) (any, error)
- func MigrateLegacyConfig(home string, w io.Writer) (configPath string, migrated bool)
- func ParseIntArg(s string) (int, error)
- func ParseUUIDArg(s string) (uuid.UUID, error)
- func PromptMissing(cmd *cobra.Command, args []string) error
- func PromptedArgs(cmd *cobra.Command, args []string) []string
- func RegisterArgs(cmd *cobra.Command, specs ...ArgSpec)
- func RegisterScopedArgs(cmd *cobra.Command, scope int, specs ...ArgSpec)
- func RegisterWaitFlags(cmd *cobra.Command)
- func ScopeInt(name, v string) (int, error)
- func StderrWidth() int
- func StringArrayToIntArray(strArr []string) []int
- func StringPtrOrNil(s string) *string
- func StringToDate(dateStr string) openapi_types.Date
- func StringToEmail(email string) openapi_types.Email
- func StringToInt(s string) int
- func StringToTime(dateStr string) time.Time
- func StringToUUID(s string) openapi_types.UUID
- func Wait(cmd *cobra.Command, body []byte, spec WaitSpec, ...) ([]byte, int, error)
- type APIError
- type ArgKind
- type ArgSource
- type ArgSpec
- type Call
- type FlagError
- type Version
- type WaitSpec
Constants ¶
const ( ConfigFileName = ".hostinger-mail.yaml" EnvPrefix = "HOSTINGER_MAIL_" )
ConfigFileName is the YAML config file in the user's home directory and EnvPrefix the prefix of every environment variable the CLI reads.
const ( ExitOK = 0 ExitError = 1 // API, network or other runtime failure ExitUsage = 2 // bad arguments, flags or input ExitAuth = 3 // HTTP 401/403 ExitNotFound = 4 // HTTP 404 ExitValidation = 5 // HTTP 422 )
Exit codes. Scripts can rely on these; keep README.md in sync.
const UsageTemplate = `` /* 1494-byte string literal not displayed */
UsageTemplate is cobra's default usage template with the local flags split into "Required Flags" and "Flags".
Variables ¶
var ErrAborted = fmt.Errorf("aborted")
ErrAborted is returned when the user declines a confirmation prompt.
var ErrActionFailed = errors.New("action failed")
ErrActionFailed is returned by Wait when the resource ends in a failed state.
var ErrDryRun = errors.New("dry run")
ErrDryRun is returned by the HTTP layer after --dry-run printed a request instead of sending it. It ends the command successfully.
var ErrNoCredentials = errors.New("no credentials configured")
ErrNoCredentials marks a request that could not be sent because no API token (or sign-in) is configured. It exits with ExitAuth.
Functions ¶
func BindLegacyEnv ¶
BindLegacyEnv binds config keys to their HOSTINGER_MAIL_* env var with a deprecated HAPI_* fallback on the global viper, emitting a one-time notice to w per key whose value comes from the legacy variable.
func CompleteArgs ¶ added in v1.3.0
func CompleteArgs(sources ...*ArgSource) cobra.CompletionFunc
CompleteArgs returns a ValidArgsFunction that completes the next positional argument from sources[len(args)]; a nil entry, an API error or a timeout yields no completions (and never falls back to file names).
func Confirm ¶ added in v1.3.0
Confirm asks before a destructive command runs. --yes and dry-run (flag, config or environment) skip the question; a non-interactive session must pass --yes explicitly.
func ConfirmScoped ¶ added in v1.4.0
ConfirmScoped is Confirm for a command whose target can come from the link, the environment or the config. Such a target is acted on only after a question; with --yes or without a terminal it must be typed.
func FitWidth ¶ added in v1.5.0
FitWidth shortens s to width columns, ending in "…". A line wider than the terminal wraps, and a spinner that redraws it with \r then adds a row per frame.
func FlagErrorf ¶ added in v1.3.0
FlagErrorf formats a FlagError.
func FormatCheck ¶ added in v1.3.0
FormatCheck validates that a changed string flag parses as format ("date-time", "date", "uuid" or "email") before it is converted.
func GroupRunE ¶ added in v1.3.0
GroupRunE makes a command group fail on an unknown subcommand (with suggestions) instead of printing help and exiting 0. Without arguments it shows the group's help.
func HandleError ¶ added in v1.3.0
HandleError prints err for the command that failed and returns the exit code. Usage mistakes get a pointer to --help; API errors list each invalid field against the flag that sets it.
func IntPtrOrNil ¶
func Interactive ¶ added in v1.3.0
Interactive reports whether the CLI may prompt: stdin and stderr are terminals and neither --no-input nor the no_input config/env key is set.
func IsRequired ¶ added in v1.3.0
IsRequired reports whether a flag was marked required with MarkFlagRequired.
func JSONValue ¶
JSONValue parses a JSON flag value into a generic value. The value is either inline JSON, @path to read it from a file, or - to read it from stdin.
func MigrateLegacyConfig ¶
MigrateLegacyConfig returns the YAML config path the CLI should read and whether a one-time migration (or legacy fallback) happened. When the new ~/.hostinger-mail.yaml is absent but a legacy ~/.hapi.yaml exists, it moves the old file to the new path. If the move fails, it falls back to the old path in place. Notices are written to w. With no legacy file, it is a no-op.
func ParseIntArg ¶ added in v1.3.0
ParseIntArg parses an integer argument where a bad value must not panic (shell completion of later arguments).
func ParseUUIDArg ¶ added in v1.3.0
ParseUUIDArg parses a UUID argument where a bad value must not panic.
func PromptMissing ¶ added in v1.3.0
PromptMissing asks for any missing positional arguments and required flags when the session is interactive. It runs from the root's PersistentPreRunE: after argument validation (which lets missing arguments through only when interactive) and before cobra's required-flag check.
func PromptedArgs ¶ added in v1.3.0
PromptedArgs returns args extended with any values prompted for interactively.
func RegisterArgs ¶ added in v1.3.0
RegisterArgs installs the positional-argument validator for cmd: exactly len(specs) arguments, each parsing as its kind. Missing trailing arguments are allowed in an interactive session; they are prompted for before RunE.
func RegisterScopedArgs ¶ added in v1.4.0
RegisterScopedArgs is RegisterArgs where the first scope args may be left out (the resolver supplies them); given args fill the specs from the end.
func RegisterWaitFlags ¶ added in v1.3.0
RegisterWaitFlags adds --wait and --wait-timeout to cmd.
func ScopeInt ¶ added in v1.4.0
ScopeInt parses an id resolved from a flag, the environment or the link, which argument validation never saw.
func StderrWidth ¶ added in v1.5.0
func StderrWidth() int
StderrWidth is the terminal width of stderr, or 0 when unknown.
func StringArrayToIntArray ¶
func StringPtrOrNil ¶
func StringToDate ¶ added in v1.1.2
func StringToDate(dateStr string) openapi_types.Date
func StringToEmail ¶ added in v1.1.2
func StringToEmail(email string) openapi_types.Email
StringToEmail keeps format:email query params converting through utils like every other formatted kind, so generated commands need no extra import.
func StringToInt ¶
func StringToTime ¶
func StringToUUID ¶ added in v1.1.4
func StringToUUID(s string) openapi_types.UUID
func Wait ¶ added in v1.3.0
func Wait(cmd *cobra.Command, body []byte, spec WaitSpec, poll func(ctx context.Context, id string) ([]byte, int, error)) ([]byte, int, error)
Wait polls an asynchronous resource until its state is one of spec.Done or spec.Failed. body is the response that started it; poll fetches the resource by the ID read from body. It returns the last fetched body; when the final state is a failure the error is ErrActionFailed (wrapped).
Types ¶
type APIError ¶ added in v1.3.0
type APIError struct {
Status int
Message string
Errors map[string][]string
CorrelationID string
Body []byte
}
APIError is an HTTP error response from the API.
func NewAPIError ¶ added in v1.3.0
NewAPIError decodes the API's error body ({message, errors, correlation_id}); an unexpected body is kept verbatim.
type ArgKind ¶ added in v1.3.0
type ArgKind int
ArgKind is the type a positional argument must parse as.
type ArgSource ¶ added in v1.3.0
type ArgSource struct {
Value string
Fetch func(ctx context.Context, args []string) ([]byte, int, error)
}
ArgSource supplies the completions of one positional argument: Fetch lists the candidates (it receives the arguments typed so far) and Value names the item field offered.
type ArgSpec ¶ added in v1.3.0
ArgSpec describes one positional argument of a generated command.
type Call ¶ added in v1.3.0
Call performs one API request and returns the raw body and status code.
type FlagError ¶ added in v1.3.0
type FlagError struct{ Err error }
FlagError is an input mistake (bad flag value, missing argument). The root command prints it with a pointer to --help and exits with ExitUsage.