Documentation
¶
Index ¶
- Variables
- func ContextNames(cfg Config) []string
- func DefaultConfigPath() string
- func RemoveContext(cfg Config, context Context)
- type Config
- type Context
- type FlagCompletionFunc
- type IOption
- type MockConfig
- type Option
- func (o *Option[T]) Changed(c Config) bool
- func (o *Option[T]) Completions() []string
- func (o *Option[T]) ConfigKey() string
- func (o *Option[T]) EnvVar() string
- func (o *Option[T]) FlagName() string
- func (o *Option[T]) Get(c Config) (T, error)
- func (o *Option[T]) GetAsAny(c Config) (any, error)
- func (o *Option[T]) GetDescription() string
- func (o *Option[T]) GetFlagCompletionFunc() FlagCompletionFunc
- func (o *Option[T]) GetFlags() OptionFlag
- func (o *Option[T]) GetName() string
- func (o *Option[T]) HasFlags(src OptionFlag) bool
- func (o *Option[T]) Override(c Config, v T)
- func (o *Option[T]) OverrideAny(c Config, v any)
- func (o *Option[T]) Parse(values []string) (any, error)
- func (o *Option[T]) T() any
- type OptionFlag
- type Preferences
- type Schema
Constants ¶
This section is empty.
Variables ¶
View Source
var ( OptionConfig = newOpt( "config", "Config file path", DefaultConfigPath(), OptionFlagPFlag|OptionFlagEnv, nil, nil, ) OptionToken = newOpt( "token", "Hetzner Cloud API token", "", OptionFlagConfig|OptionFlagEnv|OptionFlagSensitive, nil, nil, ) OptionContext = newOpt( "context", "Currently active context", "", OptionFlagConfig|OptionFlagEnv|OptionFlagPFlag, func(_ hcapi2.Client, cfg Config, _ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { ctxs := cfg.Contexts() ctxNames := make([]string, 0, len(ctxs)) for _, ctx := range ctxs { ctxNames = append(ctxNames, ctx.Name()) } return ctxNames, cobra.ShellCompDirectiveDefault }, &overrides{configKey: "active_context"}, ) OptionEndpoint = newOpt( "endpoint", "Hetzner Cloud API endpoint", hcloud.Endpoint, DefaultPreferenceFlags, nil, nil, ) OptionDebug = newOpt( "debug", "Enable debug output", false, DefaultPreferenceFlags, nil, nil, ) OptionDebugFile = newOpt( "debug-file", "File to write debug output to", "", DefaultPreferenceFlags, nil, nil, ) OptionPollInterval = newOpt( "poll-interval", "Interval at which to poll information, for example action progress", 500*time.Millisecond, DefaultPreferenceFlags, nil, nil, ) OptionQuiet = newOpt( "quiet", "If true, only print error messages", false, DefaultPreferenceFlags, nil, nil, ) OptionDefaultSSHKeys = newOpt( "default-ssh-keys", "Default SSH keys for new servers", []string{}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice, nil, nil, ) OptionSortCertificate = newOpt( "sort.certificate", "Default sorting for Certificate resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortDatacenter = newOpt( "sort.datacenter", "Default sorting for Datacenter resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortFirewall = newOpt( "sort.firewall", "Default sorting for Firewall resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortFloatingIP = newOpt( "sort.floating-ip", "Default sorting for Floating IP resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortImage = newOpt( "sort.image", "Default sorting for Image resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortLoadBalancer = newOpt( "sort.load-balancer", "Default sorting for Load Balancer resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortLocation = newOpt( "sort.location", "Default sorting for Location resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortPlacementGroup = newOpt( "sort.placement-group", "Default sorting for Placement Group resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortPrimaryIP = newOpt( "sort.primary-ip", "Default sorting for Primary IP resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortServer = newOpt( "sort.server", "Default sorting for Server resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortSSHKey = newOpt( "sort.ssh-key", "Default sorting for SSH Key resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) OptionSortVolume = newOpt( "sort.volume", "Default sorting for Volume resource", []string{"id:asc"}, (DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice|OptionFlagHidden, nil, nil, ) )
Note: &^ is the bit clear operator and is used to remove flags from the default flag set
View Source
var Options = make(map[string]IOption)
Functions ¶
func ContextNames ¶
func DefaultConfigPath ¶
func DefaultConfigPath() string
func RemoveContext ¶
Types ¶
type Config ¶
type Config interface {
// Write writes the config to the given writer. If w is nil, the config is written to the config file.
Write(w io.Writer) error
// Read reads the config from the flags, env and the given config file f.
// f can be of the following types:
// - nil: the default config file is used
// - string: the path to the config file
// - io.Reader: the config is read from the reader
// - []byte: the config is read from the byte slice
// - any other type: an error is returned
Read(f any) error
// ActiveContext returns the currently active context
ActiveContext() Context
// SetActiveContext sets the currently active context and also modifies the schema to reflect this change
// This does NOT change any configuration values. Use [config.Config.UseConfig] to read the actual context into memory.
SetActiveContext(Context)
// Contexts returns a list of currently loaded contexts
Contexts() []Context
// SetContexts sets the list of contexts and also modifies the schema to reflect this change
SetContexts([]Context)
// UseContext temporarily switches context to the given context name and reloads the config, loading the values of the given context.
// If name is nil, the context is unloaded and only the global preferences are used.
// This change will not be written to the schema, so `active_context` will not be changed after writing.
UseContext(name *string) error
// Preferences returns the global preferences (as opposed to [Context.Preferences])
Preferences() Preferences
// Viper returns the currently active instance of viper
Viper() *viper.Viper
// FlagSet returns the FlagSet that options are bound to
FlagSet() *pflag.FlagSet
// Path returns the path to the used config file
Path() string
// Schema returns the TOML schema of the config file as a struct
Schema() *Schema
}
type Context ¶
type Context interface {
Name() string
Token() string
Preferences() Preferences
}
func ContextByName ¶
func NewContext ¶ added in v1.44.0
type FlagCompletionFunc ¶ added in v1.48.0
type IOption ¶ added in v1.44.0
type IOption interface {
// GetName returns the name of the option
GetName() string
// GetDescription returns the description of the option
GetDescription() string
// GetFlagCompletionFunc returns the completion function for this option's flag.
// If it doesn't exist it returns nil.
GetFlagCompletionFunc() FlagCompletionFunc
// ConfigKey returns the key used in the config file. If the option is not configurable via the config file, an empty string is returned
ConfigKey() string
// EnvVar returns the name of the environment variable. If the option is not configurable via an environment variable, an empty string is returned
EnvVar() string
// FlagName returns the name of the flag. If the option is not configurable via a flag, an empty string is returned
FlagName() string
// HasFlags returns true if the option has all the provided flags set
HasFlags(src OptionFlag) bool
// GetFlags returns all flags set for the option
GetFlags() OptionFlag
// GetAsAny reads the option value from the config and returns it as an any
GetAsAny(c Config) (any, error)
// OverrideAny sets the option value in the config to the provided any value
OverrideAny(c Config, v any)
// Changed returns true if the option has been changed from the default
Changed(c Config) bool
// Completions returns a list of possible completions for the option (for example for boolean options: "true", "false")
Completions() []string
// Parse parses a string slice (for example command arguments) based on the option type and returns the parsed value as an any
Parse(values []string) (any, error)
// T returns an instance of the type of the option as an any
T() any
// contains filtered or unexported methods
}
type MockConfig ¶
type MockConfig struct {
Config
}
type Option ¶ added in v1.44.0
type Option[T any] struct { Name string Description string Default T Flags OptionFlag FlagCompletionFunc FlagCompletionFunc // contains filtered or unexported fields }
func NewTestOption ¶ added in v1.44.0
func NewTestOption[T any](name, description string, def T, flags OptionFlag, ov *overrides) (*Option[T], func())
NewTestOption is a helper function to create an option for testing purposes
func (*Option[T]) Completions ¶ added in v1.44.0
func (*Option[T]) GetDescription ¶ added in v1.44.0
func (*Option[T]) GetFlagCompletionFunc ¶ added in v1.48.0
func (o *Option[T]) GetFlagCompletionFunc() FlagCompletionFunc
func (*Option[T]) GetFlags ¶ added in v1.45.0
func (o *Option[T]) GetFlags() OptionFlag
func (*Option[T]) HasFlags ¶ added in v1.44.0
func (o *Option[T]) HasFlags(src OptionFlag) bool
func (*Option[T]) OverrideAny ¶ added in v1.44.0
type OptionFlag ¶ added in v1.44.0
type OptionFlag int
const ( // OptionFlagPreference indicates that the option can be set in the config file, globally or per context (in the preferences section) OptionFlagPreference OptionFlag = 1 << iota // OptionFlagConfig indicates that the option can be configured inside the configuration file OptionFlagConfig // OptionFlagPFlag indicates that the option can be set via a command line flag OptionFlagPFlag // OptionFlagEnv indicates that the option can be set via an environment variable OptionFlagEnv // OptionFlagSensitive indicates that the option holds sensitive data and should not be printed OptionFlagSensitive // OptionFlagSlice indicates that the option value is a slice OptionFlagSlice // OptionFlagHidden indicates that the option should not be shown in the help output OptionFlagHidden DefaultPreferenceFlags = OptionFlagPreference | OptionFlagConfig | OptionFlagPFlag | OptionFlagEnv )
type Preferences ¶ added in v1.44.0
Preferences are options that can be set in the config file, globally or per context
func (Preferences) Set ¶ added in v1.44.0
func (p Preferences) Set(key string, val any)
func (Preferences) Unset ¶ added in v1.44.0
func (p Preferences) Unset(key string) bool
func (Preferences) Validate ¶ added in v1.44.0
func (p Preferences) Validate() error
type Schema ¶ added in v1.44.0
type Schema struct {
ActiveContext string `toml:"active_context"`
Preferences Preferences `toml:"preferences"`
Contexts []*context `toml:"contexts,omitempty"`
}
Click to show internal directories.
Click to hide internal directories.