Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Test_SetArgs ¶ added in v1.2.0
func Test_SetArgs(args []string)
func Test_SetEnvironment ¶ added in v1.2.0
func Test_SetEnvironment(env []string)
func Test_SetWorkingDir ¶ added in v1.2.0
func Test_SetWorkingDir(dir string)
Types ¶
type File ¶
type File struct { // Path is the absolute path to the configuration file. Path string // Values is a map of configuration values loaded from the file. Note that // these values are flattened into paths separated by periods. Slice indexes // are represented by square brackets with the index inside. The value is // always a slice of any. It's a slice because theoretically a configuration // file could have multiple values for the same path. This is not the case with // toml so as of now it's always a slice of length 1. Values map[string][]any }
File represents a configuration file loaded from disk.
type Loader ¶
type Loader struct { // FlagValues is a map of flag values by path. FlagValues map[string][]any // EnvironmentValues is a map of environment variable values by path. EnvironmentValues map[string][]any // ConfigurationFiles is a slice of configuration files. ConfigurationFiles []*File }
Loader is a struct that contains all the values loaded from flags, environment variables, and configuration files. It can be used to marshal the values into a struct.
func Load ¶
Load loads configuration values from flags, environment variables, and configuration files. Flags are taken from `os.Args[1:]`. Environment variables are taken from `os.Environ()`. Configuration files are taken from the working directory and all parent directories. The configuration file name is the application name with the extension `.config.toml`. If the name contain
func LoadFromValues ¶
func LoadFromValues(programArgs []string, envVarPrefix string, envVars []string, configSearchStartPath string, configFileNames []string) (*Loader, error)
LoadFromValues works like Load, but allows the caller to specify configuration such as flag and environment values, as well as which path to start searching for configuration files and which configuration file names to look for.
func (*Loader) Get ¶
```go
type TestConfig struct { Database struct { ConnectionUri string `config:"connection_uri"` } `config:"database"` Server struct { Port int `config:"port"` } `config:"server"` Channels []struct { Name string `config:"name"` Id int `config:"id"` } `config:"channels"` } loader, err := orale.Load("my-app") if err != nil { panic(err) } var testConfig TestConfig if err := loader.Get("", &testConfig); err != nil { panic(err) }
```
As you can see in the example above, the TestConfig struct is populated with the loaded configuration values. The property names of each field are specified by the `config` tag. If the `config` tag is not specified, the property name is converted to snake case. For example `ConnectionUri` becomes `connection_uri` path.