Golang Configor 配置文件工具

https://www.jianshu.com/p/f826d2cc361b

介绍

一个支持 yaml、json、toml、shell 的配置文件工具

安装

  1. go get github.com/jinzhu/configor
  2. //or
  3. gopm get -v github.com/jinzhu/configor

使用示例

创建一个 yaml 配置文件,config.yml

  1. appname: test
  2. db:
  3. name: test
  4. user: root
  5. password: 123
  6. port: 3306
  7. contacts:
  8. - name: jack
  9. email: jack@test.com
  10. - name: tom
  11. email: tom@test.com

编写代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/configor"
  5. )
  6. type Config struct {
  7. APPName string `default:"app name"`
  8. DB struct{
  9. Name string
  10. User string `default:"root"`
  11. Password string `required:"true" env:"DBPassword"`
  12. Port uint `default:"3306"`
  13. }
  14. Contacts []struct{
  15. Name string
  16. Email string `required:"true"`
  17. }
  18. }
  19. func main() {
  20. var conf = Config{}
  21. err := configor.Load(&conf, "config.yml")
  22. if err != nil {
  23. panic(err)
  24. }
  25. fmt.Printf("%v \n", conf)
  26. }

测试模式

Usage:

  1. // 上面的代码 下面这这句
  2. err := configor.Load(&conf, "config.yml")
  3. // 修改为
  4. err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")
  1. # 使用环境变量开启测试模式,无需修改代码
  2. CONFIGOR_DEBUG_MODE=true go run config.go

Output:

  1. Current environment: 'development'
  2. Loading configurations from file 'config.yml'...
  3. Configuration:
  4. &main.Config{APPName:"test", DB:struct { Name string; User string "default:\"root\""; Password string "required:\"true\" env:\"DBPassword\""; Port uint "default:\"3306\"" }{Name:"test", User:"root", Password:"123", Port:0xcea}, Contacts:[]struct { Name string; Email string "required:\"true\"" }{struct { Name string; Email string "required:\"true\"" }{Name:"jack", Email:"jack@test.com"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@test.com"}}}
  5. {test {test root 123 3306} [{jack jack@test.com} {tom tom@test.com}]}

详细模式

Usage:

  1. // 上面的代码 下面这这句
  2. err := configor.Load(&conf, "config.yml")
  3. // 修改为
  4. err := configor.New(&configor.Config{Verbose: true}).Load(&conf, "config.yml")
  1. # 使用环境变量开启详细模式,无需修改代码
  2. CONFIGOR_VERBOSE_MODE=true go run config.go

Output:

  1. Current environment: 'development'
  2. Loading configurations from file 'config.yml'...
  3. Trying to load struct `Config`'s field `APPName` from env Configor_APPName, CONFIGOR_APPNAME
  4. Trying to load struct `Config`'s field `DB` from env Configor_DB, CONFIGOR_DB
  5. Trying to load struct ``'s field `Name` from env Configor_DB_Name, CONFIGOR_DB_NAME
  6. Trying to load struct ``'s field `User` from env Configor_DB_User, CONFIGOR_DB_USER
  7. Trying to load struct ``'s field `Password` from env DBPassword
  8. Trying to load struct ``'s field `Port` from env Configor_DB_Port, CONFIGOR_DB_PORT
  9. Trying to load struct `Config`'s field `Contacts` from env Configor_Contacts, CONFIGOR_CONTACTS
  10. Trying to load struct ``'s field `Name` from env Configor_Contacts_0_Name, CONFIGOR_CONTACTS_0_NAME
  11. Trying to load struct ``'s field `Email` from env Configor_Contacts_0_Email, CONFIGOR_CONTACTS_0_EMAIL
  12. Trying to load struct ``'s field `Name` from env Configor_Contacts_1_Name, CONFIGOR_CONTACTS_1_NAME
  13. Trying to load struct ``'s field `Email` from env Configor_Contacts_1_Email, CONFIGOR_CONTACTS_1_EMAIL
  14. Configuration:
  15. &main.Config{APPName:"test", DB:struct { Name string; User string "default:\"root\""; Password string "required:\"true\" env:\"DBPassword\""; Port uint "default:\"3306\"" }{Name:"test", User:"root", Password:"123", Port:0xcea}, Contacts:[]struct { Name string; Email string "required:\"true\"" }{struct { Name string; Email string "required:\"true\"" }{Name:"jack", Email:"jack@test.com"}, struct { Name string; Email string "required:\"true\"" }{Name:"tom", Email:"tom@test.com"}}}
  16. {test {test root 123 3306} [{jack jack@test.com} {tom tom@test.com}]}

高级用法

加载多个配置文件

  1. // application.yml 的优先级 大于 database.json, 排在前面的配置文件优先级大于排在后的的配置
  2. configor.Load(&Config, "application.yml", "database.json")

根据环境变量加载配置文件

使用CONFIGOR_ENV 设置环境变量,如果 CONFIGOR_ENV 没有设置,框架将会使用 development 作为默认环境变量。

  1. // config.go
  2. configor.Load(&Config, "config.json")
  1. $ go run config.go
  2. // 将会加载 `config.json`,如果存在 `config.development.json` 将会自动加载
  3. // `config.development.json` 将会覆盖 `config.json` 的配置
  4. $ CONFIGOR_ENV=production go run config.go
  5. // 将会加载 `config.json`,如果存在 `config.production.json` 将会自动加载
  6. // `config.production.json` 将会覆盖 `config.json` 的配置
  7. $ go test
  8. // 将会加载 `config.json`,如果存在 `config.test.json` 将会自动加载
  9. // `config.test.json` 将会覆盖 `config.json` 的配置
  10. $ CONFIGOR_ENV=production go test
  11. // 将会加载 `config.json`,如果存在 `config.production.json` 将会自动加载
  12. // `config.production.json` 将会覆盖 `config.json` 的配置
  1. // 在代码里面设置 环境变量
  2. configor.New(&configor.Config{Environment: "production"}).Load(&Config, "config.json")

示例配置

  1. configor.Load(&Config, "config.yml")
  1. $ go run config.go
  2. # 将会自动加载 `config.example.yml` 如果 `config.yml` 不存在将会输出警告信息

Output:

  1. Failed to find configuration config.yml, using example file config.example.yml
  2. {test {dodododo root 123 3306 } [{jack jack@test.com} {tom tom@test.com}]}

从shell加载配置项

  1. CONFIGOR_APPNAME="hello world" go run config.go
  2. # 格式为 {{prefix}}_FieldName
  1. # 覆盖 prefix shell 操作
  2. $ CONFIGOR_ENV_PREFIX="WEB" WEB_APPNAME="hello, prefix" go run config.go
  3. # 在代码的书写方式为
  4. configor.New(&configor.Config{ENVPrefix: "WEB"}).Load(&Config, "config.json")

匿名结构 - Anonymous Struct

  1. type Details struct {
  2. Description string
  3. }
  4. type Config struct {
  5. Details `anonymous:"true"`
  6. }

如果使用 anonymous:"true" 标签,那么 Description 参数的环境变量将会变为 CONFIGOR_DESCRIPTION,否则环境变量将会是 CONFIGOR_DETAILS_DESCRIPTION

ft_authoradmin  ft_create_time2019-06-01 22:11
 ft_update_time2019-06-01 22:13