config

package
v0.0.0-...-4fa8e69 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0, Apache-2.0 Imports: 17 Imported by: 199

Documentation

Overview

Package config provides a common infrastructure for reading configuration data stored in local TOML files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExplicitlySet

func IsExplicitlySet(co *ConfigOption) bool

IsExplicitlySet returns true if and only if the given config option was set explicitly either via a command line argument or via an environment variable

func Read

func Read(path string, dest interface{}) error

Read takes the TOML configuration file at `path`, parses it into `dest` and then uses github.com/asaskevich/govalidator to validate the struct.

func SetDuration

func SetDuration(co *ConfigOption) error

SetDuration converts a command line int to a duration, and stores it in the final config.

func SetDurationMinutes

func SetDurationMinutes(co *ConfigOption) error

SetDurationMinutes converts a command line minutes value to a duration, and stores it in the final config.

func SetOptionalString

func SetOptionalString(co *ConfigOption) error

SetOptionalString converts a command line uint to a *string where the nil value indicates the flag was not explicitly set

func SetOptionalUint

func SetOptionalUint(co *ConfigOption) error

SetOptionalUint converts a command line uint to a *uint where the nil value indicates the flag was not explicitly set

func SetURL

func SetURL(co *ConfigOption) error

SetURL converts a command line string to a URL, and stores it in the final config.

Types

type ConfigOption

type ConfigOption struct {
	Name           string                    // e.g. "db-url"
	EnvVar         string                    // e.g. "DATABASE_URL". Defaults to uppercase/underscore representation of name
	OptType        types.BasicKind           // The type of this option, e.g. types.Bool
	FlagDefault    interface{}               // A default if no option is provided. Omit or set to `nil` if no default
	Required       bool                      // Whether this option must be set for Horizon to run
	Usage          string                    // Help text
	CustomSetValue func(*ConfigOption) error // Optional function for custom validation/transformation
	ConfigKey      interface{}               // Pointer to the final key in the linked Config struct

	Hidden         bool     // Indicates whether to hide the flag from --help output
	UsedInCommands []string // the list of sub-comands this flag is relevant in
	// contains filtered or unexported fields
}

ConfigOption is a complete description of the configuration of a command line option

func (*ConfigOption) Bind

func (co *ConfigOption) Bind()

Bind binds the config option to viper.

func (*ConfigOption) Init

func (co *ConfigOption) Init(cmd *cobra.Command) error

Init handles initialisation steps, including configuring and binding the env variable name.

func (*ConfigOption) Require

func (co *ConfigOption) Require()

Require checks that a required string configuration option is not empty, raising a user error if it is.

func (*ConfigOption) RequireE

func (co *ConfigOption) RequireE() error

RequireE is like Require, but returns the error instead of Fatal

func (*ConfigOption) SetDeprecated

func (co *ConfigOption) SetDeprecated(cmd *cobra.Command)

SetDeprecated Hides the deprecated flag from --help output

func (*ConfigOption) SetValue

func (co *ConfigOption) SetValue() error

SetValue sets a value in the global config, using a custom function, if one was provided.

func (*ConfigOption) ToggleHidden

func (co *ConfigOption) ToggleHidden(hidden bool)

ToggleHidden sets the hidden attibute on the persistent flag associated to this config option

func (*ConfigOption) UsageText

func (co *ConfigOption) UsageText() string

UsageText returns the string to use for the usage text of the option. The string returned will be the Usage defined on the ConfigOption, along with the environment variable.

type ConfigOptions

type ConfigOptions []*ConfigOption

ConfigOptions is a group of ConfigOptions that can be for convenience initialized and set at the same time.

func (ConfigOptions) GetCommandLineFlagsPassedByUser

func (cos ConfigOptions) GetCommandLineFlagsPassedByUser() []string

GetCommandLineFlagsPassedByUser returns a list of command-line flags that were passed by the user when running Horizon.

func (ConfigOptions) Init

func (cos ConfigOptions) Init(cmd *cobra.Command) error

Init calls Init on each ConfigOption passing on the cobra.Command.

func (ConfigOptions) Require

func (cos ConfigOptions) Require()

Require calls Require on each ConfigOption.

func (ConfigOptions) RequireE

func (cos ConfigOptions) RequireE() error

RequireE is like Require, but returns the error instead of Fatal

func (ConfigOptions) SetValues

func (cos ConfigOptions) SetValues() error

SetValues calls SetValue on each ConfigOption.

type InvalidConfigError

type InvalidConfigError struct {
	InvalidFields map[string]string
}

InvalidConfigError is the error that is returned when an invalid configuration is encountered by the `Read` func.

func (*InvalidConfigError) Error

func (err *InvalidConfigError) Error() string

type TLS

type TLS struct {
	CertificateFile string `toml:"certificate-file" valid:"required"`
	PrivateKeyFile  string `toml:"private-key-file" valid:"required"`
}

TLS represents a common configuration snippet for configuring TLS in a server process

Jump to

Keyboard shortcuts

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