Skip to content

ruggi/konf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

konf

A simple and straightforward configuration files manager.

Description

konf loads configurations into structs, supporting multiple formats.

It supports direct files loading, or from []byte raw data, and also injects environment variables into the structs, overriding the values in the configuration file.

Supported formats

  • JSON
  • YAML
  • TOML

Usage

Get the package with go get github.com/ruggi/konf, then use it like this:

Loading

You can load a configuration file into a struct with the konf.Load function:

type Config struct {
	Address string `json:"address" yaml:"address" env:"ADDRESS"`
	Port    int    `json:"port" yaml:"port" env:"PORT"`
}

func main() {
	var config Config
	err := konf.Load("path/to/config.yaml", &config)
	if err != nil {
		// ...
	}
}

Environment variables

Looking at the example above, if your config looks like

address: "127.0.0.1"
port: 8080

and you have an environment variable PORT=1234, the final configuration struct will be

Config{
    Address: "127.0.0.1",
    Port:    1234,
}

because the environment variable PORT (as specified in the struct's tag) overrides the value 8080 in the loaded configuration file.

Saving

You can save a type to a file with the konf.Save function:

func main() {
	config := Config{
		Address: "127.0.0.1",
		Port:    1234,
	}
	err := konf.Save("path/to/config.yaml", config)
	if err != nil {
		// ...
	}
}

konf.Save uses the right format depending on the destination file's extension.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages