A config file manager for Go that uses a defaults and overrides file.
Go to file
2020-07-13 21:39:24 -07:00
example Initial commit 2020-07-13 21:39:24 -07:00
go.mod Initial commit 2020-07-13 21:39:24 -07:00
go.sum Initial commit 2020-07-13 21:39:24 -07:00
LICENSE.md Initial commit 2020-07-13 21:39:24 -07:00
README.md Initial commit 2020-07-13 21:39:24 -07:00
yamlsettings_test.go Initial commit 2020-07-13 21:39:24 -07:00
yamlsettings.go Initial commit 2020-07-13 21:39:24 -07:00

YamlSettings for Go

This is a config file manager for Go, inspired by Python yamlsettings.

Example

Your Go code:

import "git.kirsle.net/go/yamlsettings"

// Typedef for your config object.
type Config struct {
    Database struct {
        Host string `yaml:"host"`
        Port string `yaml:"port"`
        User string `yaml:"user"`
        Password string `yaml:"passwd"`
    } `yaml:"database"`

    Redis struct {
        Host string `yaml:"redis_host"`
        Port int `yaml:"redis_port"`
    } `yaml:"redis"`

    Debug bool `yaml:"DEBUG"`
    SecretKey string `yaml:"SECRET_KEY"`
}

func main() {
    // Marshal your app config from the defaults + settings files.
    // Only the defaults.yml should exist on disk; the settings.yml is
    // optional and will be ignored if not found.
    var setting Config
    err := yamlsettings.LoadFiles(
        "defaults.yml", "settings.yml",
        &setting,
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("Redis host: %s\n", settings.Redis.Host)
}

Your defaults.yml file can be committed to version control. It uses sensible defaults and has dummy text in place of passwords and needed configuration:

---
database:
  host: "localhost"
  port: 5432
  user: "postgres"
  passwd: "postgres"
redis:
  host: "localhost"
  port: 6379
DEBUG: false
SECRET_KEY: "!! CHANGE ME !!"

The settings.yml file can override values from the defaults file. This file goes in your .gitignore to avoid accidentally committing secrets to version control!

---
database:
  host: "pgprod.example.com"
  user: "pgprod_admin"
  passwd: "big secret long password"
SECRET_KEY: "fd09s0sfv0dhgf098sasd"

License

MIT. © 2020 Noah Petherbridge.