config

package module
v0.0.0-...-3f885d8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2020 License: MIT Imports: 2 Imported by: 0

README

English | 简体中文

Helmsman

Helmsman is a ready-to-use Go project configuration solution


Go Report Card Coverage Status Build Status GitHub stars GitHub issues

Why use?

  • multi-format
    • Json
    • Yaml
    • Envionment vars
    • Xml
    • Toml
    • ini
  • multi-source
    • Local File
    • Github Repo
    • Gitlab Repo
    • Aliyun OSS
    • Consul
    • Etcd
  • data format & data source separation
  • watching changes

Install

go get github.com/overtalk/helmsman

How to use?

file source & json format

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

change to github config source

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
}

config struct build tools

Documentation

Index

Constants

View Source
const (
	JsonFormat = "JSON"
	YamlFormat = "YAML"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser interface {
	Parse(data []byte, cfg interface{}) error
}

func GetParser

func GetParser(t ParserType) Parser

type ParserType

type ParserType string

type Source

type Source interface {
	ParseConfig(t ParserType, cfg interface{}) error
}

Directories

Path Synopsis
parser
xml
yml
source
env

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL