config

package
v1.44.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OptionConfig = newOpt(
		"config",
		"Config file path",
		DefaultConfigPath(),
		OptionFlagPFlag|OptionFlagEnv,
		nil,
	)

	OptionToken = newOpt(
		"token",
		"Hetzner Cloud API token",
		"",
		OptionFlagConfig|OptionFlagEnv|OptionFlagSensitive,
		nil,
	)

	OptionContext = newOpt(
		"context",
		"Currently active context",
		"",
		OptionFlagConfig|OptionFlagEnv|OptionFlagPFlag,
		&overrides{configKey: "active_context"},
	)

	OptionEndpoint = newOpt(
		"endpoint",
		"Hetzner Cloud API endpoint",
		hcloud.Endpoint,
		DefaultPreferenceFlags,
		nil,
	)

	OptionDebug = newOpt(
		"debug",
		"Enable debug output",
		false,
		DefaultPreferenceFlags,
		nil,
	)

	OptionDebugFile = newOpt(
		"debug-file",
		"File to write debug output to",
		"",
		DefaultPreferenceFlags,
		nil,
	)

	OptionPollInterval = newOpt(
		"poll-interval",
		"Interval at which to poll information, for example action progress",
		500*time.Millisecond,
		DefaultPreferenceFlags,
		nil,
	)

	OptionQuiet = newOpt(
		"quiet",
		"If true, only print error messages",
		false,
		DefaultPreferenceFlags,
		nil,
	)

	OptionDefaultSSHKeys = newOpt(
		"default-ssh-keys",
		"Default SSH keys for new servers",
		[]string{},
		(DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice,
		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 ContextNames(cfg Config) []string

func DefaultConfigPath

func DefaultConfigPath() string

func RemoveContext

func RemoveContext(cfg Config, context Context)

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
}

func New added in v1.44.0

func New() Config

type Context

type Context interface {
	Name() string
	Token() string
	Preferences() Preferences
}

func ContextByName

func ContextByName(cfg Config, name string) Context

func NewContext added in v1.44.0

func NewContext(name, token string) Context

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
	// 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
	// GetAsAny reads the option value from the config and returns it as an any
	GetAsAny(c Config) any
	// 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
}

func (*MockConfig) Write

func (c *MockConfig) Write(_ io.Writer) error

type Option added in v1.44.0

type Option[T any] struct {
	Name        string
	Description string
	Default     T
	Flags       OptionFlag
	// 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]) Changed added in v1.44.0

func (o *Option[T]) Changed(c Config) bool

func (*Option[T]) Completions added in v1.44.0

func (o *Option[T]) Completions() []string

func (*Option[T]) ConfigKey added in v1.44.0

func (o *Option[T]) ConfigKey() string

func (*Option[T]) EnvVar added in v1.44.0

func (o *Option[T]) EnvVar() string

func (*Option[T]) FlagName added in v1.44.0

func (o *Option[T]) FlagName() string

func (*Option[T]) Get added in v1.44.0

func (o *Option[T]) Get(c Config) T

func (*Option[T]) GetAsAny added in v1.44.0

func (o *Option[T]) GetAsAny(c Config) any

func (*Option[T]) GetDescription added in v1.44.0

func (o *Option[T]) GetDescription() string

func (*Option[T]) GetName added in v1.44.0

func (o *Option[T]) GetName() string

func (*Option[T]) HasFlags added in v1.44.0

func (o *Option[T]) HasFlags(src OptionFlag) bool

func (*Option[T]) Override added in v1.44.0

func (o *Option[T]) Override(c Config, v T)

func (*Option[T]) OverrideAny added in v1.44.0

func (o *Option[T]) OverrideAny(c Config, v any)

func (*Option[T]) Parse added in v1.44.0

func (o *Option[T]) Parse(values []string) (any, error)

func (*Option[T]) T added in v1.44.0

func (o *Option[T]) T() any

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

	DefaultPreferenceFlags = OptionFlagPreference | OptionFlagConfig | OptionFlagPFlag | OptionFlagEnv
)

type Preferences added in v1.44.0

type Preferences map[string]any

Preferences are options that can be set in the config file, globally or per context

func (Preferences) Get added in v1.44.0

func (p Preferences) Get(key string) (any, bool)

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"`
}

Jump to

Keyboard shortcuts

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