Skip to content

Latest commit

 

History

History
123 lines (102 loc) · 2.95 KB

README_CN.md

File metadata and controls

123 lines (102 loc) · 2.95 KB

简体中文 | English

Helmsman

Helmsman 是一个即用的 Go项目 配置的解决方案


Go Report Card Coverage Status Build Status GitHub stars GitHub issues

为什么使用?

  • 多格式
    • Json
    • Yaml
    • Envionment vars
    • Xml
    • Toml
    • ini
  • 多数据源
    • 本地文件
    • Github 仓库
    • Gitlab 仓库
    • 阿里云 OSS
    • Consul
    • Etcd
  • 数据格式数据源分离
  • 配置变更通知

安装

go get github.com/overtalk/helmsman

如何使用?

本地文件 & json

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)
}

github 仓库 数据源

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
}

配置 struct 生成工具