Categorygithub.com/go-tk/configset
modulepackage
0.4.6
Repository: https://github.com/go-tk/configset.git
Documentation: pkg.go.dev

# README

configset

GoDev Workflow Status Coverage Status

Simple & powerful configuration library

Features

  • Aggregate all configuration files under a directory into one configuration.

  • Use environment variables to override configuration values.

Example

package main

import (
        "fmt"
        "io/ioutil"
        "os"

        "github.com/go-tk/configset"
)

func main() {
        // 1. Create configuration files for testing.
        _ = os.Mkdir("./temp", 0755)

        ioutil.WriteFile("./temp/foo.yaml", []byte(`
user_id: 1000
nickname: roy
friends: [dave]
`), 0644)

        ioutil.WriteFile("./temp/bar.yaml", []byte(`
secrets:
  password: s0g00d
  luck_numbers:
    - 1
    - 3
    - 5
`), 0644)

        // 2. Override configuration values with environment variables.
        os.Setenv("CONFIGSET.foo.nickname", "lisa")             // env value should be valid YAML
        os.Setenv("CONFIGSET.foo.friends", "[maria, victoria]") // env value should be valid YAML
        os.Setenv("CONFIGSET.bar.secrets.luck_numbers.1", "99") // env value should be valid YAML

        // 3. Read in configuration files.
        configset.MustLoad("./temp")

        // 4. Dump the configuration set in form of JSON for debugging.
        json := string(configset.Dump("", "  "))
        fmt.Print(json)
        // output:
        // {
        //   "bar": {
        //     "secrets": {
        //       "luck_numbers": [
        //         1,
        //         99,
        //         5
        //       ],
        //       "password": "s0g00d"
        //     }
        //   },
        //   "foo": {
        //     "friends": [
        //       "maria",
        //       "victoria"
        //     ],
        //     "nickname": "lisa",
        //     "user_id": 1000
        //   }
        // }

        // 5. Read a configuration value into a struct.
        var secrets struct {
                Password    string `json:"password"`     // should use json tag rather than yaml tag
                LuckNumbers []int  `json:"luck_numbers"` // should use json tag rather than yaml tag
        }
        configset.MustReadValue("bar.secrets", &secrets)
        fmt.Printf("%v\n", secrets)
        // output:
        // {s0g00d [1 99 5]}
}

# Functions

Dump returns the config set in form of JSON.
Load loads the config set from all *.yaml files under the given directory.
MustLoad likes Load but panics when an error occurs.
MustReadValue likes ReadValue but panics when an error occurs.
ReadValue finds the value for the given path from the config set and unmarshals the given config from that value in form of JSON.

# Variables

ErrValueNotFound is returned when the JSON value does not exist.