cfg

package
v0.0.0-...-2d17607 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInitErr

func AddInitErr(err error)

func AddInitErr adds an error to the init error list. This is checked only during startup, to deal with any errors generated by init() funcs.

func ApplicationDataPath

func ApplicationDataPath() string

func InitApp

func InitApp(appName string) error

InitApp supplies names to any paths that want to use the application name, such as the AppDataPath.

func InitErr

func InitErr() error

func InitErr answers a composition of errors generated during init() funcs, if any.

func SaveSettings

func SaveSettings(path string, s Settings) error

SaveSettings saves the settings as JSON to the path. It will remove any private keys.

Types

type EnvMatcher

type EnvMatcher interface {
	// Answer a key if a match, or else "". The key
	// might be the same key in the args, or it might be modified.
	Match(key string) (string, error)
}

EnvMatcher is used by WithEnv to accept env vars and potentially modify the name.

func EnvPattern

func EnvPattern(pattern string) EnvMatcher

EnvPattern loads all env vars that match the pattern into the Settings. See path.Match() for match rules.

func EnvPrefix

func EnvPrefix(prefix string) EnvMatcher

EnvPrefix loads all env vars that match the prefix into the Settings. The prefix will be stripped.

type Option

type Option func(Settings, oferrors.Block)

func WithEnv

func WithEnv(match EnvMatcher) Option

WithEnv loads all matching env vars.

func WithFS

func WithFS(fsys fs.FS, pattern string) Option

WithFS loads all files that match the pattern into the Settings. All matched files must be in JSON format. See path.Match() for match rules.

func WithKeys

func WithKeys(src Settings, keys []string) Option

WithKeys adds all keys in the supplied settings.

type Settings

type Settings struct {
	// contains filtered or unexported fields
}

Settings stores a tree of settings values, accessed via a path syntax ("path/to/value").

func NewSettings

func NewSettings(opts ...Option) (Settings, error)

func (Settings) Bool

func (s Settings) Bool(path string) (bool, bool)

Bool answers the bool value at the given path. The value must be a bool, a string (with value "true" or "t") or an element of a slice (for example, if the Settings contains "fruits": ["apple", "orange"], then Bool("fruits/apple") will return true).

func (Settings) Float64

func (s Settings) Float64(path string) (float64, bool)

Float64 answers the float64 value at the given path.

func (Settings) Int64

func (s Settings) Int64(path string) (int64, bool)

Int64 answers the int64 value at the given path.

func (Settings) IsChanged

func (s Settings) IsChanged() bool

func (Settings) Length

func (s Settings) Length(path string) int

Length answers the length of the slice at path, or 0 if path is not a slice.

func (Settings) MustBool

func (s Settings) MustBool(path string, fallback bool) bool

MustBool answers the bool value at the given path or fallback if path is absent.

func (Settings) MustFloat64

func (s Settings) MustFloat64(path string, fallback float64) float64

MustFloat64 answers the bool value at the given path or fallback if path is absent.

func (Settings) MustInt64

func (s Settings) MustInt64(path string, fallback int64) int64

MustInt64 answers the bool value at the given path or fallback if path is absent.

func (Settings) MustString

func (s Settings) MustString(path, fallback string) string

MustString answers the string value at the given path or fallback if path is absent.

func (Settings) Print

func (s Settings) Print()

func (Settings) SetValue

func (s Settings) SetValue(key string, value interface{}) error

SetValue sets the given key to the given value. `value` can be nil, and an empty map will be created. Currently the key can not contain a path element; if you want to se a path, find the subset first.

func (Settings) String

func (s Settings) String(path string) (string, bool)

String answers the string value at the given path.

func (Settings) Strings

func (s Settings) Strings(path string) []string

Strings answers the string slice at the given path. If path points to a string slice, it's returned, otherwise the current keys are returned.

func (Settings) Subset

func (s Settings) Subset(path string) Settings

Subset answers a subset of the settings tree based on walking down the path. The path can have components separated with "/".

func (Settings) WalkKeys

func (s Settings) WalkKeys(fn WalkKeysFunc) error

WalkKeys iterates the keys. You can get the same info from calling Strings(), which is easier to use but less efficient. Not sure that I'll ever need the difference in efficiency though, so this might go away.

type WalkKeysFunc

type WalkKeysFunc func(key string) error

Jump to

Keyboard shortcuts

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