configue - Configuration library for Go
configue is a library for reading configuration from different sources in Go.
It is a simpler alternative to popular configuration loading libraries
(spf13/viper,
knadh/koanf,
etc).
configue has package for reading configuration from environment variables,
INI files and command line flags. It is easy to plug in custom source by
implementing the
Backend interface.
There is no external dependency and the API is strongly inspired by the flag
package from the standard library.
Getting started
Here is a simple example:
package main import ( "errors" "fmt" "runtime" "github.com/negrel/configue" ) type Option struct { Debug bool MaxProc int } func main() { // Create backends. env := configue.NewEnv("MYAPP") flag := configue.NewFlag() ini := configue.NewINI(configue.File("./", "config.ini")) // Create a figue that loads from flags, INI file, env vars and flags in this // specific order. figue := configue.New( "", // Subcommand name. configue.ContinueOnError, // Error handling strategy. flag, ini, env, flag, ) figue.Usage = func() { _, _ = fmt.Fprintln(figue.Output(), "myapp - a great app") _, _ = fmt.Fprintln(figue.Output()) _, _ = fmt.Fprintln(figue.Output(), "Usage:") _, _ = fmt.Fprintln(figue.Output(), " myapp [flags]") _, _ = fmt.Fprintln(figue.Output()) figue.PrintDefaults() } // Define options. var options Option figue.BoolVar(&options.Debug, "debug", false, "enable debug logs") figue.IntVar(&options.MaxProc, "max.proc", runtime.NumCPU(), "maximum number of CPU that can be executed simultaneously") // Flag only option. flag.StringVar(&ini.FilePath, "config", ini.FilePath, "custom config file path") // Parse options. err := figue.Parse() if errors.Is(err, configue.ErrHelp) { return } if err != nil { // handle error } }
$ myapp -h
myapp - a great app
Usage:
myapp [flags]
Flags:
-config string
custom config file path (default "/home/anegrel/.config/myapp/config.ini")
-debug
enable debug logs
-max-proc value
maximum number of CPU that can be executed simultaneously (default 16)
Environment variables:
MYAPP_DEBUG
enable debug logs
MYAPP_MAX_PROC int
maximum number of CPU that can be executed simultaneously (default 16)
Configuration file is located at /home/anegrel/.config/myapp/config.iniFor a real example, see Prisme Analytics configuration loading.
Contributing
If you want to contribute to configue to add a feature or improve the code contact
me at alexandre@negrel.dev, open an
issue or make a
pull request.
🌠 Show your support
Please give a ⭐ if this project helped you!
📜 License
MIT © Alexandre Negrel

