config

package
v1.0.15-pre Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 7 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetString

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

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

func GetStringMapString

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

func GetStringSlice

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

Types

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)

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 Sub added in v1.11.0

func Sub(key string) *Config

func (*Config) ExpandString

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

Return a string that has all contents of the form ${var} or $var expanded according to the following rules:

url - read the contents of the url, which can be a local file, as below:
	${file://path/to/file} - read the entire contents of the file, trim whitespace
	${https://host/path} - fetch the remote contents, trim whitespace. "http:" also supported.
${env:VARNAME} - replace with the contents of the environment variable VARNAME, trim whitespace
${path.to.config} - and var containing a '.' will be looked up in global viper config space - this is NOT recursive
${name} - replace with the contents of confmap["name"] - trim whitespace

While the form $var is also supported but may be ambiguous and is not recommended.

If confmap is not given then environment variables are used directly.

Any errors result in an empty string being returned.

func (*Config) GetString

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

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

func (*Config) GetStringMapString

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

func (*Config) GetStringSlice

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

func (*Config) Sub

func (c *Config) Sub(key string) *Config

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