configset

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2022 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

configset

GoDev Workflow Status Coverage Status

Simple & powerful configuration library

Features

  • Load all configuration files under a directory at once.

  • 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
`), 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.bar.secrets.luck_numbers.1", "99") // env value should be valid YAML

        // 3. Read in configuration files.
        configset.MustOpen("./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": {
        //     "nickname": "lisa",
        //     "user_id": 1000
        //   }
        // }

        // 5. Load a configuration item by a path.
        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.MustLoadItem("bar.secrets", &secrets)
        fmt.Printf("%v\n", secrets)
        // output:
        // {s0g00d [1 99 5]}
}

Documentation

Overview

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
`), 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.bar.secrets.luck_numbers.1", "99") // env value should be valid YAML

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

	// 4. Dump the whole configuration in form of JSON for debugging.
	json := string(configset.Dump("", "  "))
	fmt.Println("===== Dump =====")
	fmt.Print(json)

	// 5. Load a configuration item by a path.
	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.MustLoadItem("bar.secrets", &secrets)
	fmt.Println("===== MustLoadItem  =====")
	fmt.Printf("%v\n", secrets)

}
Output:
===== Dump =====
{
  "bar": {
    "secrets": {
      "luck_numbers": [
        1,
        99,
        5
      ],
      "password": "s0g00d"
    }
  },
  "foo": {
    "nickname": "lisa",
    "user_id": 1000
  }
}
===== MustLoadItem  =====
{s0g00d [1 99 5]}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrValueNotFound = errors.New("configset: value not found")

ErrValueNotFound is returned when the JSON value does not exist.

Functions

func Dump

func Dump(prefix string, indention string) json.RawMessage

Dump returns the cache in form of JSON.

func LoadItem

func LoadItem(path string, item interface{}) error

LoadItem finds the value for the given path from the cache and unmarshals the given item from that value in form of JSON. If no value can be found by the path, ErrValueNotFound is returned.

func MustLoadItem

func MustLoadItem(path string, item interface{})

MustLoadItem likes LoadItem but panics when an error occurs.

func MustOpen

func MustOpen(dirPath string)

MustOpen likes Open but panics when an error occurs.

func Open

func Open(dirPath string) error

Open sets up the config set. All *.yaml files under the given directory will be read into a cache. If there are environment variables set such as CONFIGSET.{path}={value}, the cache will be overwritten according to {paths} and {values}.

Types

This section is empty.

Jump to

Keyboard shortcuts

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