Skip to content

crvcrv/configor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Configor

Golang Configuration tool that support YAML, JSON, Shell Environment

Usage

import (
	"github.com/jinzhu/configor"
)

var Config = struct {
	APPName string `default:"app name"`

	DB struct {
		Name     string
		User     string `default:"root"`
		Password string `required:"true" env:"DBPassword"`
		Port     uint   `default:"3306"`
	}

	Contacts []struct {
		Name  string
		Email string `required:"true"`
	}
}{}

configor.Load(&Config, "config.yml", "config.json"...)

Advanced Usage

  • Different configuration for each environment

Use CONFIGOR_ENV to set the environment

// config.go
configor.Load(&Config, "config.json")

$ CONFIGOR_ENV=production go run config.go
// Will load `config.yml`, `config.production.yml` if it is exist
// And `config.production.yml` will overwrite `config.yml`'s configuration
// You could use this to share same configuration across different environments
  • Example Configuration
// config.go
configor.Load(&Config, "config.yml")

$ go run config.go
// Will load `config.example.yml` automatically if `config.yml` not found and print warning message
  • Read From Shell Environment
$ CONFIGOR_APPNAME="hello world" CONFIGOR_DB_NAME="hello world" go run config.go
// Will use shell environment's value if found with upcase of prefix (by default is CONFIGOR) + field name as key
// You could overwrite the prefix with environment CONFIGOR_ENV_PREFIX, for example:
$ CONFIGOR_ENV_PREFIX="WEB" WEB_APPNAME="hello world" WEB_DB_NAME="hello world" go run config.go

Author

jinzhu

License

Released under the MIT License

About

Golang Configuration tool that support YAML, JSON, Shell Environment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%