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: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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