简体中文 | English
Helmsman 是一个即用的 Go项目 配置的解决方案
- 多格式
- Json
- Yaml
- Envionment vars
- Xml
- Toml
- ini
- 多数据源
- 本地文件
- Github 仓库
- Gitlab 仓库
- 阿里云 OSS
- Consul
- Etcd
-
数据格式
与数据源
分离 - 配置变更通知
go get github.com/overtalk/helmsman
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/overtalk/helmsman"
fileSource "github.com/overtalk/helmsman/source/file"
)
const str = `{
"A": "this is the test of a ",
"B": "this is the test of bbb"
}`
type ConfigDemo struct {
A string `json:"A"`
B string `json:"B"`
}
func GetConfig(path string) (*ConfigDemo, error) {
c := &ConfigDemo{}
fc := fileSource.NewFileConfig(path)
if err := fc.ParseConfig(config.JsonFormat, c); err != nil {
return nil, err
}
return c, nil
}
func main() {
// write config data to tmp file
tmpFile, err := ioutil.TempFile("", "temp_file")
if err != nil {
fmt.Println(err)
return
}
defer os.Remove(tmpFile.Name()) // clean up
// write data to the temp file
if err := ioutil.WriteFile(tmpFile.Name(), []byte(str), 0777); err != nil {
fmt.Println(err)
return
}
c, err := GetConfig(tmpFile.Name())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c.A)
fmt.Println(c.B)
}
func GetConfigFromGithub() (*ConfigDemo, error) {
c := &ConfigDemo{}
cfg := &githubSource.Config{
Token: "your github token",
Owner: "your github name",
Repo: "your github repo",
Ref: "your repo branch",
Path: "your config file path in the repo",
}
githubClient := githubSource.NewClient(cfg)
if err := githubClient.ParseConfig(config.JsonFormat, c); err != nil {
return nil, err
}
return c, nil
}