core

package
v6.7.6 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequiredFlagsAnnotation   = "RequiredFlags"
	DeprecatedFlagsAnnotation = "DeprecatedFlags"
)

Variables

View Source
var NoPreRun = func(c *PreCommandConfig) error { return nil }
View Source
var (
	RootCmdTest = Command{
		Command: &cobra.Command{
			Use: testConst,
		},
	}
)

Functions

func CheckRequiredFlags

func CheckRequiredFlags(cmd *Command, ns string, localFlagsName ...string) error

func CheckRequiredFlagsSets

func CheckRequiredFlagsSets(cmd *Command, ns string, localFlagsNameSets ...[]string) error

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

func FlagAsVariable(flag string) string

FlagAsVariable takes a flag name and returns it as a screaming camel case

e.g. `FlagAsVariable("datacenter-id") -> "DATACENTER_ID"

func FlagUsage

func FlagUsage(flag string) string

FlagUsage ("datacenter-id") -> "--datacenter-id DATACENTER_ID"

Used as a convenience func

func FlagsUsage

func FlagsUsage(flags ...string) string

FlagsUsage calls FlagUsage for every flag in the slice

func GetFlagName

func GetFlagName(ns, flagName string) string

func PreCmdConfigTest

func PreCmdConfigTest(t *testing.T, writer io.Writer, preRunner PreCmdRunTest)

func RequiresMinOptionsErr

func RequiresMinOptionsErr(cmd *Command, flagNames ...string) error

func RequiresMultipleOptionsErr

func RequiresMultipleOptionsErr(cmd *Command, flagNamesSets ...[]string) error

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 NewCommandWithJsonProperties

func NewCommandWithJsonProperties(ctx context.Context, parent *Command, jsonExample string, toUnmarshal interface{}, info CommandBuilder) *Command

func (*Command) AddBoolFlag

func (c *Command) AddBoolFlag(name, shorthand string, defaultValue bool, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddCommand

func (c *Command) AddCommand(commands ...*Command)

func (*Command) AddDurationFlag

func (c *Command) AddDurationFlag(name, shorthand string, defaultValue time.Duration, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddFloat32Flag

func (c *Command) AddFloat32Flag(name, shorthand string, defaultValue float32, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddInt32Flag

func (c *Command) AddInt32Flag(name, shorthand string, defaultValue int32, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddInt32VarFlag

func (c *Command) AddInt32VarFlag(address *int32, name, shorthand string, defaultValue int32, desc string, optionFunc ...FlagOptionFunc)

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) AddIpFlag

func (c *Command) AddIpFlag(name, shorthand string, defaultValue net.IP, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddIpSliceFlag

func (c *Command) AddIpSliceFlag(name, shorthand string, defaultValue []net.IP, desc string, optionFunc ...FlagOptionFunc)

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 (c *Command) AddStringSliceVarFlag(address *[]string, name, shorthand string, defaultValue []string, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddStringToStringFlag

func (c *Command) AddStringToStringFlag(name, shorthand string, defaultValue map[string]string, desc string, optionFunc ...FlagOptionFunc)

func (*Command) AddStringToStringVarFlag

func (c *Command) AddStringToStringVarFlag(v *map[string]string, name, shorthand string, defaultValue map[string]string, desc string, optionFunc ...FlagOptionFunc)

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 (c *Command) CommandPath() string

func (*Command) GetAnnotations

func (c *Command) GetAnnotations() map[string]string

func (*Command) GetAnnotationsByKey

func (c *Command) GetAnnotationsByKey(key string) string

func (*Command) GlobalFlags

func (c *Command) GlobalFlags() *flag.FlagSet

func (*Command) IsAvailableCommand

func (c *Command) IsAvailableCommand() bool

func (*Command) Name

func (c *Command) Name() string

func (*Command) SetFlagAnnotation

func (c *Command) SetFlagAnnotation(name, key string, values ...string)

func (*Command) SubCommands

func (c *Command) SubCommands() []*Command

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
	AuthV1Services             authservice.Services
	CloudApiDbaasPgsqlServices cloudapidbaaspgsql.Services
	CertificateManagerServices certmanager.Services
	ContainerRegistryServices  container_registry.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)

type CommandRun

type CommandRun func(commandConfig *CommandConfig) error

type FlagNameSetWithPredicate

type FlagNameSetWithPredicate struct {
	FlagNameSet    []string
	Predicate      func(interface{}) bool
	PredicateParam interface{}
}

type FlagOptionFunc

type FlagOptionFunc func(cmd *Command, flagName string)

func DeprecatedFlagOption

func DeprecatedFlagOption(help string) FlagOptionFunc

func RequiredFlagOption

func RequiredFlagOption() FlagOptionFunc

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
	CloudApiDbaasPgsqlMocks cloudapidbaaspgsql.ResourcesMocks
	AuthV1Mocks             authservice.ResourcesMocks
}

type SetFlag

type SetFlag struct {
	Value   string
	Allowed []string
}

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

func (*SetFlag) Set

func (a *SetFlag) Set(p string) error

func (SetFlag) String

func (a SetFlag) String() string

func (*SetFlag) Type

func (a *SetFlag) Type() string

type TestCase

type TestCase struct {
	Name        string
	UserInput   io.Reader
	Args        []FlagValuePair
	Calls       func(...*gomock.Call)
	ExpectedErr bool // To be replaced by `error` type once it makes sense to do so (currently only one type of error is thrown)
}

Directories

Path Synopsis
Package doc generates Markdown files and organizes a directory structure that follows the command hierarchy.
Package doc generates Markdown files and organizes a directory structure that follows the command hierarchy.

Jump to

Keyboard shortcuts

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