config

package
v1.2.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 17 Imported by: 1

README

config package

The config package wraps the common viper methods and types but adds some custom processing.

This probably could be done using custom decode hooks for viper, but I have not had time to understand them in detail.

Using a wrapper also allows custom methods on config for further functionality.

Unless the file format is given to LoadConfig then all the viper formats are supported.

Expansion of values

The following package functions and type methods are overridden to add expansion of embedded strings of the form ${name} and $name:

The string values are passed to ExpandString which supports a range of data substitutions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checksum

func Checksum(r io.Reader) (c uint32, err error)

func ChecksumFile

func ChecksumFile(path string) (c uint32, err error)

func ChecksumString

func ChecksumString(in string) (c uint32, err error)

func GetString

func GetString(s string, values ...map[string]string) string

Returns the configuration item as a string with ExpandString() applied, passing the first "values" if given

func GetStringMapString

func GetStringMapString(s string, values ...map[string]string) map[string]string

func GetStringSlice

func GetStringSlice(s string, values ...map[string]string) []string

Types

type AESValues

type AESValues struct {
	Key []byte
	IV  []byte
}

An AESValues structure contains the values required to create a Geneos Gateway AES key files and also the encode and decode AES passwords in the configuration

func NewAESValues

func NewAESValues() (a AESValues, err error)

NewAESValues returns a new AESValues structure or an error

func ReadAESValues

func ReadAESValues(r io.Reader) (a AESValues, err error)

ReadAESValues returns an AESValues struct populated with the contents read from r. The caller must close the Reader on return.

func ReadAESValuesFile

func ReadAESValuesFile(path string) (a AESValues, err error)

ReadAESValuesFile returns an AESValues struct populated with the contents of the file passed as path.

func (*AESValues) Checksum

func (a *AESValues) Checksum() (c uint32, err error)

Checksum returns the CRC32 checksum of the AESValue it is called on.

func (AESValues) DecodeAES

func (a AESValues) DecodeAES(in []byte) (out []byte, err error)

func (AESValues) DecodeAESString

func (a AESValues) DecodeAESString(in string) (out string, err error)

DecodeAESString returns a plain text of the input or an error

func (AESValues) EncodeAES

func (a AESValues) EncodeAES(in []byte) (out []byte, err error)

func (AESValues) EncodeAESString

func (a AESValues) EncodeAESString(in string) (out string, err error)

func (AESValues) String

func (a AESValues) String() string

String method for AESValues

func (AESValues) WriteAESValues

func (a AESValues) WriteAESValues(w io.Writer) error

WriteAESValues writes the AESValues structure to the io.Writer. Each fields acts as if it were being marshalled with an ",omitempty" tag.

type Config

type Config struct {
	*viper.Viper
	Type string
}

Config embeds Viper and also exposes the config type used

func GetConfig

func GetConfig() *Config

func LoadConfig

func LoadConfig(configName string, options ...Options) (c *Config, err error)

LoadConfig loads configuration files from internal defaults, external defaults and the given configuration file. The configuration file can be passed as an option. Each layer is only loaded once, if given. Internal defaults are passed as a []byte intended to be loaded from an embedded file. External defaults and the main configuration file are passed as ordered slices of strings. The first match is loaded.

LoadConfig("geneos")

//go:embed somefile.json
var myDefaults []byte
LoadConfig("geneos", config.SetDefaults(myDefaults, "json"), )

Options can be passed to change the default behaviour and to pass any embedded defaults or an existing viper.

for defaults see: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html ... find windows equiv

func New

func New() *Config

func (*Config) ExpandString

func (c *Config) ExpandString(input string, values map[string]string) (value string)

ExpandString() returns input with any occurrences of the form ${name} interpolated using os.Expand for the supported format types in the order given below:

  1. "${enc:keyfile[|keyfile...]:encodedvalue}"

    The item "encodedvalue" is an AES256 ciphertext in Geneos format - or a reference to one - which will be decoded using the key file(s) given. Each "keyfile" must be one of either an absolute path, a path relative to the working directory of the program, or if prefixed with "~/" then relative to the home directory of the user running the program. The first valid decode (see below) is returned.

    The "encodedvalue" must be either prefixed "+encs+" to align with Geneos or will otherwise be looked up using the forms of any of the other references below, but without the surrounding dollar-brackets "${...}".

    To minimise (but not eliminate) false decodes that occur in some circumstances when using the wrong key file, the decoded value is only returned if it is a valid UTF-8 string as per utf8.Valid.

    Examples:

    * password: ${enc:~/.keyfile:+encs+9F2C3871E105EC21E4F0D5A7921A937D} * password: ${enc:/etc/geneos/keyfile.aes:env:ENCODED_PASSWORD}

  2. "${path.to.config}"

    Any value containing one or more dots "." will be looked-up in the existing configuration that the method is called on. The configuration is not changed and values are resolved each time ExpandString() is called. No locking of the configuration is done.

  3. "${name}"

    "name" will be substituted with the corresponding value from the map "values". If 'values' is empty (as opposed to the key "name" not being found) then name is looked up as an environment variable, see below.

  4. "${env:name}"

    "name" will be substituted with the contents of the environment variable of the same name.

  5. "${file://path/to/file}" or "${file:~/path/to/file}"

    The contents of the referenced file will be read. Multiline files are used as-is; this can, for example, be used to read PEM certificate files or keys. As an enhancement to a standard file url, if the first "/" is replaced with a tilde "~" then the path is relative to the home directory of the user running the process.

  6. "${https://host/path}" or "${http://host/path}"

    The contents of the URL are fetched and used similarly as for local files above. The URL is passed to http.Get and supports any embedded Basic Authentication and other features from that function.

The form "$name" is NOT supported, unlike os.Expand.

Expansion is not recursive. Configuration values are read and stored as literals and are expanded each time they are used. For each substitution any leading and trailing whitespace are removed. External sources are fetched each time they are used and so there may be a performance impact as well as the value unexpectedly changing during a process lifetime.

Any errors (particularly from substitutions from external files or remote URLs) may result in an empty or corrupt string being returned. Error returns are intentionally discarded.

It is not currently possible to escape the syntax supported by Expand and if it is necessary to have a configuration value be a literal of the form "${name}" then set an otherwise unused item to the value and refer to it using the dotted syntax, e.g. for YAML

config:
  real: ${config.temp}
  temp: "${unchanged}"

In the above a reference to ${config.real} will return the literal string ${unchanged} as there is no recursive lookups.

func (*Config) GetString

func (c *Config) GetString(s string, values ...map[string]string) string

Returns the configuration item as a string with ExpandString() applied, passing the first "values" if given

func (*Config) GetStringMapString

func (c *Config) GetStringMapString(s string, values ...map[string]string) (m map[string]string)

func (*Config) GetStringSlice

func (c *Config) GetStringSlice(s string, values ...map[string]string) (slice []string)

type Options

type Options func(*configOptions)

func AddConfigDirs

func AddConfigDirs(paths ...string) Options

AddConfigDirs() adds one or more directories to search for the configuration and defaults files. Directories are searched in FIFO order, so any directories given are checked before the built-in list.

func IgnoreSystemDir

func IgnoreSystemDir() Options

IgnoreSystemDir() tells LoadConfig() not to search in the system configuration directory. This only applies on UNIX-like systems and is normally /etc/[appName]

func IgnoreUserConfDir

func IgnoreUserConfDir() Options

IgnoreUserConfDir() tells LoadConfig() not to search in the user config directory (OS defined as per Go os.UserConfDir())

func IgnoreWorkingDir

func IgnoreWorkingDir() Options

IgnoreWorkingDir() tells LoadConfig not to search the working directory for configuration files. This should be used when the caller may be running in an unknown location.

func SetAppName

func SetAppName(name string) Options

SetAppName() overrides to mapping of the configName to the application name. Application name is used for the containing directories, while configName is used for the files in those directories.

func SetConfigFile

func SetConfigFile(path string) Options

SetConfigFile() allows the caller to override the searching for a config file in the given directories and instead loads only the given file (after defaults are loaded as normal).

func SetDefaults

func SetDefaults(defaults []byte, format string) Options

SetDefaults() takes a []byte slice and a format type to load embedded defaults

func UseGlobal

func UseGlobal() Options

UseGlobal() uses the global config instead of creating a new instance.

Jump to

Keyboard shortcuts

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