Documentation
¶
Index ¶
- Constants
- Variables
- func CheckRequiredFlags(cmd *Command, ns string, localFlagsName ...string) error
- func CheckRequiredFlagsSets(cmd *Command, ns string, localFlagsNameSets ...[]string) error
- func CheckRequiredFlagsSetsIfPredicate(cmd *Command, ns string, localFlagsNameSets ...FlagNameSetWithPredicate) error
- func CmdConfigTest(t *testing.T, writer io.Writer, runner CmdRunnerTest)
- func ExecuteTestCases(t *testing.T, funcToTest func(c *CommandConfig) error, testCases []TestCase, ...)
- func FlagAsVariable(flag string) string
- func FlagUsage(flag string) string
- func FlagsUsage(flags ...string) string
- func GetFlagName(ns, flagName string) string
- func PreCmdConfigTest(t *testing.T, writer io.Writer, preRunner PreCmdRunTest)
- func RequiresMinOptionsErr(cmd *Command, flagNames ...string) error
- func RequiresMultipleOptionsErr(cmd *Command, flagNamesSets ...[]string) error
- type CmdRunnerTest
- type Command
- func NewCommand(ctx context.Context, parent *Command, info CommandBuilder) *Command
- func NewCommandWithJsonProperties(ctx context.Context, parent *Command, jsonExample string, ...) *Command
- func WithConfigOverride(c *Command, productNames []string, fallbackURL string) *Command
- func WithRegionalConfigOverride(c *Command, productNames []string, templateFallbackURL string, ...) *Command
- func (c *Command) AddBoolFlag(name, shorthand string, defaultValue bool, desc string, ...)
- func (c *Command) AddColsFlag(columns []table.Column)
- func (c *Command) AddCommand(commands ...*Command)
- func (c *Command) AddDurationFlag(name, shorthand string, defaultValue time.Duration, desc string, ...)
- func (c *Command) AddFloat32Flag(name, shorthand string, defaultValue float32, desc string, ...)
- func (c *Command) AddFloat64Flag(name, shorthand string, defaultValue float64, desc string, ...)
- func (c *Command) AddInt32Flag(name, shorthand string, defaultValue int32, desc string, ...)
- func (c *Command) AddInt32VarFlag(address *int32, name, shorthand string, defaultValue int32, desc string, ...)
- func (c *Command) AddIntFlag(name, shorthand string, defaultValue int, desc string, ...)
- func (c *Command) AddIntSliceFlag(name, shorthand string, defaultValue []int, desc string, ...)
- func (c *Command) AddIpFlag(name, shorthand string, defaultValue net.IP, desc string, ...)
- func (c *Command) AddIpSliceFlag(name, shorthand string, defaultValue []net.IP, desc string, ...)
- func (c *Command) AddSetFlag(name, shorthand, defaultValue string, allowed []string, desc string, ...)
- func (c *Command) AddStringFlag(name, shorthand, defaultValue, desc string, optionFunc ...FlagOptionFunc)
- func (c *Command) AddStringSliceFlag(name, shorthand string, defaultValue []string, desc string, ...)
- func (c *Command) AddStringSliceVarFlag(address *[]string, name, shorthand string, defaultValue []string, desc string, ...)
- func (c *Command) AddStringToStringFlag(name, shorthand string, defaultValue map[string]string, desc string, ...)
- func (c *Command) AddStringToStringVarFlag(v *map[string]string, name, shorthand string, defaultValue map[string]string, ...)
- func (c *Command) AddStringVarFlag(address *string, name, shorthand, value, desc string, ...)
- func (c *Command) AddUUIDFlag(name, shorthand, defaultValue, desc string, optionFunc ...FlagOptionFunc)
- func (c *Command) CommandPath() string
- func (c *Command) GetAnnotations() map[string]string
- func (c *Command) GetAnnotationsByKey(key string) string
- func (c *Command) GlobalFlags() *flag.FlagSet
- func (c *Command) IsAvailableCommand() bool
- func (c *Command) Name() string
- func (c *Command) SetFlagAnnotation(name, key string, values ...string)
- func (c *Command) SubCommands() []*Command
- type CommandBuilder
- type CommandConfig
- type CommandRun
- type FlagNameSetWithPredicate
- type FlagOptionFunc
- func DeprecatedFlagOption(help string) FlagOptionFunc
- func RequiredFlagOption() FlagOptionFunc
- func WithCompletion(completionFunc func() []string, baseURL string, locations []string) FlagOptionFunc
- func WithCompletionComplex(...) FlagOptionFunc
- func WithCompletionE(completionFunc func() ([]string, error), baseURL string, locations []string) FlagOptionFunc
- type FlagValuePair
- type PreCmdRunTest
- type PreCommandConfig
- type PreCommandRun
- type ResourcesMocksTest
- type SetFlag
- type TestCase
Constants ¶
const ( RequiredFlagsAnnotation = "RequiredFlags" DeprecatedFlagsAnnotation = "DeprecatedFlags" )
Variables ¶
var NoPreRun = func(c *PreCommandConfig) error { return nil }
var ( RootCmdTest = Command{ Command: &cobra.Command{ Use: testConst, }, } )
Functions ¶
func CheckRequiredFlags ¶
func CheckRequiredFlagsSets ¶
CheckRequiredFlagsSets focuses on commands that support multiple ways to run, and it is required at least one of the ways to be set. For example: for `datacenter delete`, it is required to be set either `--datacenter-id` option, either `--all` option.
func CheckRequiredFlagsSetsIfPredicate ¶
func CheckRequiredFlagsSetsIfPredicate(cmd *Command, ns string, localFlagsNameSets ...FlagNameSetWithPredicate) error
If a flag being set to a certain value creates some extra flag dependencies, then use this function!
func CmdConfigTest ¶
func CmdConfigTest(t *testing.T, writer io.Writer, runner CmdRunnerTest)
func ExecuteTestCases ¶
func ExecuteTestCases(t *testing.T, funcToTest func(c *CommandConfig) error, testCases []TestCase, cfg *CommandConfig)
func FlagAsVariable ¶
FlagAsVariable takes a flag name and returns it as a screaming camel case
e.g. `FlagAsVariable("datacenter-id") -> "DATACENTER_ID"
func FlagUsage ¶
FlagUsage ("datacenter-id") -> "--datacenter-id DATACENTER_ID"
Used as a convenience func
func FlagsUsage ¶
FlagsUsage calls FlagUsage for every flag in the slice
FlagsUsage ("datacenter-id") -> "--datacenter-id DATACENTER_ID"
func GetFlagName ¶
func PreCmdConfigTest ¶
func PreCmdConfigTest(t *testing.T, writer io.Writer, preRunner PreCmdRunTest)
func RequiresMinOptionsErr ¶
Types ¶
type CmdRunnerTest ¶
type CmdRunnerTest func(c *CommandConfig, mocks *ResourcesMocksTest)
type Command ¶
type Command struct {
// NS is the Global Namespace of the Command
NS string
Command *cobra.Command
// contains filtered or unexported fields
}
func NewCommand ¶
func NewCommand(ctx context.Context, parent *Command, info CommandBuilder) *Command
func WithConfigOverride ¶ added in v6.9.0
func WithRegionalConfigOverride ¶ added in v6.9.0
func WithRegionalConfigOverride(c *Command, productNames []string, templateFallbackURL string, allowedLocations []string) *Command
WithRegionalConfigOverride adds regional flag support to a command, allowing users to specify a location or override the server URL. To use this function, wrap the root command of your API and specify the baseURL and allowed locations.
Example:
func DNSCommand() *core.Command {
cmd := &core.Command{
Command: &cobra.Command{
Use: "dns",
Short: "The sub-commands of the 'dns' resource help automate DNS Zone and Record management",
TraverseChildren: true,
},
}
// Add regional flags
return core.WithRegionalConfigOverride(cmd, []string{fileconfiguration.Cloud, "compute"}, "https://dns.%s.ionos.com", []string{"de/fra", "de/txl"})
}
- 'baseURL': The base URL for the API, with an optional '%s' placeholder for the location (e.g., '"https://dns.%s.ionos.com"').
- 'allowedLocations': A slice of allowed locations (e.g., '[]string{"de/fra", "de/txl"}'). These will populate the '--location' flag completion.
Notes ¶
- The '--server-url' flag allows users to override the API host URL manually.
- The '--location' flag allows users to specify a region, which replaces the '%s' placeholder in the 'baseURL'.
- If '--location' is used and is valid (from 'allowedLocations'), the 'baseURL' is formatted with the normalized location.
- If '--location' is invalid or unsupported, a warning is logged, but the constructed URL is still attempted.
- If 'allowedLocations' is empty, the function panics, as this is considered a programming error.
- If an unsupported location is provided, a warning is logged: 'WARN: <location> is an invalid location. Valid locations are: <allowedLocations>'
- This also marks '--api-url' and '--location' flags as mutually exclusive.
- The first location in 'allowedLocations' is used as the default URL if no location is provided.
func (*Command) AddBoolFlag ¶
func (c *Command) AddBoolFlag(name, shorthand string, defaultValue bool, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddColsFlag ¶ added in v6.10.1
AddColsFlag registers a persistent --cols flag with tab-completion on the command. The flag is inherited by all subcommands and controls which table columns are displayed.
func (*Command) AddCommand ¶
func (*Command) AddDurationFlag ¶
func (*Command) AddFloat32Flag ¶
func (c *Command) AddFloat32Flag(name, shorthand string, defaultValue float32, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddFloat64Flag ¶ added in v6.9.0
func (c *Command) AddFloat64Flag(name, shorthand string, defaultValue float64, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddInt32Flag ¶
func (c *Command) AddInt32Flag(name, shorthand string, defaultValue int32, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddInt32VarFlag ¶
func (*Command) AddIntFlag ¶
func (c *Command) AddIntFlag(name, shorthand string, defaultValue int, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddIntSliceFlag ¶
func (c *Command) AddIntSliceFlag(name, shorthand string, defaultValue []int, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddIpSliceFlag ¶
func (*Command) AddSetFlag ¶
func (c *Command) AddSetFlag(name, shorthand, defaultValue string, allowed []string, desc string, optionFunc ...FlagOptionFunc)
AddSetFlag adds a String slice flag with support for limitation to certain values in a slice. It also adds completions for those limited values, on top of throwing an error if the flag value isn't found among the marked valid values
func (*Command) AddStringFlag ¶
func (c *Command) AddStringFlag(name, shorthand, defaultValue, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddStringSliceFlag ¶
func (c *Command) AddStringSliceFlag(name, shorthand string, defaultValue []string, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddStringSliceVarFlag ¶
func (*Command) AddStringToStringFlag ¶
func (*Command) AddStringToStringVarFlag ¶
func (*Command) AddStringVarFlag ¶
func (c *Command) AddStringVarFlag(address *string, name, shorthand, value, desc string, optionFunc ...FlagOptionFunc)
func (*Command) AddUUIDFlag ¶
func (c *Command) AddUUIDFlag(name, shorthand, defaultValue, desc string, optionFunc ...FlagOptionFunc)
func (*Command) CommandPath ¶
func (*Command) GetAnnotations ¶
func (*Command) GetAnnotationsByKey ¶
func (*Command) GlobalFlags ¶
func (*Command) IsAvailableCommand ¶
func (*Command) SetFlagAnnotation ¶
func (*Command) SubCommands ¶
type CommandBuilder ¶
type CommandBuilder struct {
// Command is a Wrapper around Cobra Command
Command *Command
// Command Levels
// Namespace is the first level of the Command. e.g. [ionosctl] server
Namespace string
// Resource is the second level of the Command. e.g. [ionosctl server] volume
Resource string
// Verb is the 3rd level of the Command. e.g. [ionosctl server volume] attach
Verb string
// Short Description
ShortDesc string
// Long Description
LongDesc string
// Aliases
Aliases []string
// Example of Command run
Example string
// Command Run functions
// to be executed
PreCmdRun PreCommandRun
CmdRun CommandRun
// Init Client Services
InitClient bool
}
CommandBuilder contains information about the new Command to be used in Cobra Command
func (*CommandBuilder) GetNS ¶
func (c *CommandBuilder) GetNS() string
type CommandConfig ¶
type CommandConfig struct {
// Command is a Wrapper around Cobra Command
Command *Command
// CLI Engine
// NS is Global Namespace for all Command Levels
NS string
// Namespace is the first level of the Command. e.g. [ionosctl] server
Namespace string
// Resource is the second level of the Command. e.g. [ionosctl server] volume
Resource string
// Verb is the 3rd level of the Command. e.g. [ionosctl server volume] attach
Verb string
Stdin io.Reader
// Services
CloudApiV6Services cloudapiv6.Services
// Context
Context context.Context
// contains filtered or unexported fields
}
CommandConfig Properties and Services
func NewCommandCfg ¶
func NewCommandCfg(ctx context.Context, info CommandBuilder) (*CommandConfig, error)
func (*CommandConfig) Cols ¶ added in v6.10.1
func (c *CommandConfig) Cols() []string
Cols returns the user-specified --cols flag value, or nil for defaults.
func (*CommandConfig) Msg ¶ added in v6.9.8
func (c *CommandConfig) Msg(format string, args ...any)
Msg writes a formatted message to stdout, respecting --quiet and --output flags. In text mode it prints the message as-is; in JSON modes it marshals the string.
func (*CommandConfig) Out ¶ added in v6.9.8
func (c *CommandConfig) Out(s string, err error) error
Out writes pre-rendered output (e.g. from table.Sprint or table.Render) to stdout. It accepts (string, error) so it chains directly:
return c.Out(table.Sprint(cols, data, userCols)) return c.Out(t.Render(table.ResolveCols(cols, userCols)))
func (*CommandConfig) Printer ¶ added in v6.10.1
func (c *CommandConfig) Printer(columns []table.Column) *printer
Printer returns a builder for table-based output. Usage:
return c.Printer(allCols).Print(data)
return c.Printer(allCols).Prefix("items").Print(data)
func (*CommandConfig) Verbose ¶ added in v6.9.8
func (c *CommandConfig) Verbose(format string, args ...any)
Verbose writes a formatted informational message to stderr, only when --verbose is set. Respects --quiet and --output flags.
type CommandRun ¶
type CommandRun func(commandConfig *CommandConfig) error
type FlagOptionFunc ¶
func DeprecatedFlagOption ¶
func DeprecatedFlagOption(help string) FlagOptionFunc
func RequiredFlagOption ¶
func RequiredFlagOption() FlagOptionFunc
func WithCompletion ¶ added in v6.7.9
func WithCompletion(completionFunc func() []string, baseURL string, locations []string) FlagOptionFunc
WithCompletion is a FlagOptionFunc that allows for a completion function that returns a list of strings.
Usage:
- WithCompletion(completionFunc, "api.%s.ionos.com") for a regional API
- WithCompletion(completionFunc, "api.ionos.com") for an API with a single endpoint
- WithCompletion(completionFunc, "") to let the SDKs choose the API endpoint
func WithCompletionComplex ¶ added in v6.7.9
func WithCompletionComplex( completionFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective), baseURL string, locations []string, ) FlagOptionFunc
WithCompletionComplex is a FlagOptionFunc that allows for more complex completion logic. It is recommended to use one of the simpler helper functions WithCompletion or WithCompletionE instead. Use this complex function only if you need to handle advanced logic, like args-based completion, or custom filtering based on already typed keys (from toComplete).
The function determines the URL to set based on the following rules: 1. If the baseURL does not contain a placeholder (e.g., "%s"), it will be used directly. 2. If the baseURL contains a placeholder and a location is provided, the location will be used to construct the URL. 3. If no location is provided, the first location in the `locations` list will be used as a default.
func WithCompletionE ¶ added in v6.7.9
func WithCompletionE(completionFunc func() ([]string, error), baseURL string, locations []string) FlagOptionFunc
WithCompletionE is a FlagOptionFunc that allows for a completion function that can return an error.
Usage:
- WithCompletionE(completionFuncE, "api.%s.ionos.com") for a regional API
- WithCompletionE(completionFuncE, "api.ionos.com") for an API with a single endpoint
- WithCompletionE(completionFuncE, "") to let the SDK choose the API endpoint
type FlagValuePair ¶
type FlagValuePair struct {
Flag string
Value interface{}
}
type PreCmdRunTest ¶
type PreCmdRunTest func(c *PreCommandConfig)
type PreCommandConfig ¶
type PreCommandConfig struct {
// Command is a Wrapper around Cobra Command
Command *Command
// NS is Global Namespace for all Command Levels
NS string
// Namespace is the first level of the Command. e.g. [ionosctl] server
Namespace string
// Resource is the second level of the Command. e.g. [ionosctl server] volume
Resource string
// Verb is the 3rd level of the Command. e.g. [ionosctl server volume] attach
Verb string
}
PreCommandConfig Properties
func NewPreCommandCfg ¶
func NewPreCommandCfg(info CommandBuilder) *PreCommandConfig
type PreCommandRun ¶
type PreCommandRun func(commandConfig *PreCommandConfig) error
PreCommandRun will run in PreRun of Cobra Command structure, before running the actual Command. Its purpose is to keep the validate part separate from run part.
func PreRunWithDeprecatedFlags ¶ added in v6.7.4
func PreRunWithDeprecatedFlags(f PreCommandRun, flags ...functional.Tuple[string]) PreCommandRun
PreRunWithDeprecatedFlags is a decorator for using a command with deprecated flags The value of the first flag in the Tuple is set as the value of the second flag of the Tuple
type ResourcesMocksTest ¶
type ResourcesMocksTest struct {
// Add New Services Resources Mocks
CloudApiV6Mocks cloudapiv6.ResourcesMocks
}
type SetFlag ¶
SetFlag / Values set for this flag must be part of allowed values NOTE: Track progress of https://github.com/spf13/pflag/issues/236 : Might be implemented in pflag