Documentation
¶
Overview ¶
Package command holds helpers shared by the resource CRUD commands: delete confirmation, definition-file intake, flag-override merging, and resolving a missing flag value through an interactive prompt.
Index ¶
- func AnyChanged(cmd *cobra.Command, names ...string) bool
- func ApplyOverrides(data []byte, overrides map[string]any) ([]byte, error)
- func ConfirmDelete(ios *iostreams.IOStreams, yes bool, noun, fallbackName string, ...) (bool, error)
- func EnumUsage(allowed []string) string
- func Group(cmd *cobra.Command) *cobra.Command
- func ReadDefinitionFile(ios *iostreams.IOStreams, path string) ([]byte, error)
- func Resolve(ios *iostreams.IOStreams, value string, field Field) (string, error)
- func ValidateEnum(flag, value string, allowed []string) error
- type Field
- type Stream
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyChanged ¶
AnyChanged reports whether the user set any of the named flags on cmd. It backs the "provide --file or at least one of ..." guards that several update commands share, reading cobra's own change tracking instead of a parallel set of has-X booleans.
func ApplyOverrides ¶
ApplyOverrides merges overrides into a JSON object body and re-encodes it. It returns data unchanged when there are no overrides, so callers can build the map from whichever flags the user actually set and pass it through unconditionally.
func ConfirmDelete ¶
func ConfirmDelete(ios *iostreams.IOStreams, yes bool, noun, fallbackName string, fetchName func() (string, error)) (bool, error)
ConfirmDelete reports whether a delete should proceed. When yes is set it returns true without prompting. Otherwise it requires an interactive terminal, resolves a display name (calling fetchName only when a prompt is actually needed, so a --yes delete makes no extra API call), and prompts for y/N. fetchName may be nil, in which case fallbackName is shown. The caller decides what a declined delete means (return nil, or an error).
func EnumUsage ¶
EnumUsage renders allowed values for a flag's usage string, matching how ValidateEnum lists them when it rejects a value.
func Group ¶
Group configures a grouping command (one that only dispatches to subcommands) so that invoking it without a subcommand prints help and exits non-zero. Without this, cobra falls through to help with exit 0, making a bare or typo'd parent command read as success in CI guards.
Cobra already rejects an unknown subcommand with a non-zero "unknown command" error. Group adds the missing case: a bare parent invocation, which sets cobra.NoArgs (so any positional arg is rejected) and a RunE that prints help and returns an error.
func ReadDefinitionFile ¶
ReadDefinitionFile reads a JSON definition from a file path, or from stdin when path is "-", and sanitizes shell-mangled escapes (see jsonutil). It is the single home for the file/stdin intake that the create and update commands repeat.
func Resolve ¶
Resolve returns value when it is already set, otherwise prompts for it.
- value already set: returned as-is, no prompt.
- value empty, required, cannot prompt: returns field.NonInteractiveErr.
- value empty, can prompt: prompts (prompt.Choice when Choices is set, otherwise prompt.Line) and returns the answer.
- value empty, required, prompted but still empty: returns field.EmptyErr.
- value empty, optional, cannot prompt: returns "" with no error.
func ValidateEnum ¶
ValidateEnum reports whether value is one of allowed, returning a consistent error naming the flag and listing the accepted values otherwise. An empty value passes; callers gate on presence separately.
Types ¶
type Field ¶
type Field struct {
Prompt string
Required bool
Choices []string
NonInteractiveErr error
EmptyErr error
Stream Stream
}
Field describes a single create-command input that is supplied by a flag but falls back to an interactive prompt when the flag is empty. Resolve consumes it to collapse the flag-or-prompt branch that every create command would otherwise hand-write.
Prompt is the text shown when prompting. Choices, when non-empty, switches the prompt to prompt.Choice (the value must match one of the options). Required controls whether an empty result is an error: a required field that cannot be prompted returns NonInteractiveErr, and a required field that is prompted but left blank returns EmptyErr. An optional field (Required false) prompts only when possible and accepts an empty result.
Stream selects which writer the prompt text is written to. Most call sites prompt on Err; a zero Stream defaults to Err so only the call sites that prompt on Out (dataset create, query run) need to set it.