cfg

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 10 Imported by: 0

README

Quikstart

Read local config file as Inifile

  • Create config file ./app.ini
[db]
host=127.0.0.1:3306
user=yaitoo
passwd=amazing
database=sparrow
  • Open and convert config to Inifile

import (
    "fmt"
    "context"
    "github.com/yaitoo/sparrow/cfg"
)

c := cfg.Open(context.TODO(), "./app.ini").ToInifile(context.TODO())

host := c.Section("db").Value("host","")
user := c.Section("db").Value("user","")

  • Enable HotReload feature

c := cfg.Open(context.TODO(), "./app.ini")
inifile := c.ToInifile(context.TODO())
c.OnChanged(func(c *cfg.Config){
    inifile.TryParse(c.Content)
})

  • Works with other config formats. eg toml,yaml...

import "github.com/BurntSushi/toml"

c := cfg.Open(context.TODO(), "./app.ini")

var conf Config
if _, err := toml.Decode(c.Content), &conf); err != nil {
  // handle error
}

//Enable hot-reload feature
c.OnChanged(func(c *cfg.Config){
    if _, err := toml.Decode(c.Content, &conf); err != nil {
  // handle error
    }
})

Read remote config content as Inifile

  • Implement a custom Reader to read remote content via http/tcp...

type RemoteReader struct {
    Name string
}

func NewReader(name string) *RemoteReader {
    return &RemoteReader{name:name}
}

//Read implement `Reader.Read`
func (r *RemoteReader) Read(ctx context.Context) (string, error) {

   return ReadBytesFromRemote(r.name)
}

//ModTime implement `Reader.ModTime`
func (r *RemoteReader) ModTime(ctx context.Context) (int64, error) {

	return ReadLatestModTimeFromRemote(r.name)
}

  • Open config with RemoteReader from remote store(eg. redis,mysql)

 c := cfg.Open(context.TODO(),"cmdb.apis", cfg.WithReader(func(ctx context.Context) cfg.Reader {
		return NewReader("cmdb.apis")
  })).ToInifile(context.TODO())


host := c.Section("db").Value("host","")
user := c.Section("db").Value("user","")

Documentation

Overview

Package cfg implements a simple config loader with hot reload feature. Local and remote file are both supported.

Index

Constants

This section is empty.

Variables

View Source
var ReloadInterval = 30 * time.Second

ReloadInterval the frequency at configs are checked

Functions

This section is empty.

Types

type Config

type Config struct {
	sync.RWMutex
	//Name name
	Name string
	//Content content
	Content string
	// contains filtered or unexported fields
}

Config Config represents an open config descriptor.

func Open

func Open(ctx context.Context, name string, options ...Option) *Config

Open open a Config with name

func (*Config) OnChanged

func (c *Config) OnChanged(handler Handler)

OnChanged subscribe config change

func (*Config) ToInifile

func (c *Config) ToInifile(ctx context.Context) Inifile

ToInifile convert config to Inifile

type FsReader

type FsReader struct {
	// contains filtered or unexported fields
}

FsReader FsReader implements `Reader` inferface for local file system

func (*FsReader) ModTime

func (r *FsReader) ModTime(ctx context.Context) (int64, error)

ModTime implement `Reader.ModTime`

func (*FsReader) Read

func (r *FsReader) Read(ctx context.Context) (string, error)

Read implement `Reader.Read`

type Handler

type Handler func(c *Config)

Handler A hanlder responds to config changed.

type Inifile

type Inifile struct {
	// contains filtered or unexported fields
}

Inifile represents all data from an INI file. see https://en.wikipedia.org/wiki/INI_file NOTE:

  • Global properties will be ignored.
  • Section and property names are not case sensitive. Both of them are converted to lower-case. And space is trimed in leading and trailing.
  • The value of bool key is not case sensitive. "True" is same as "true". Space is trimed too.

func (*Inifile) Section

func (i *Inifile) Section(name string) *Section

Section get section with name.

func (*Inifile) TryParse

func (i *Inifile) TryParse(ctx context.Context, data string)

TryParse try parse string. if it is ok, Infile will be updated.

type Option

type Option func(ctx context.Context, c *Config)

Option set optional parameter

func WithReader

func WithReader(create func(ctx context.Context) Reader) Option

WithReader using a custom Reader

type Reader

type Reader interface {
	//Read reads contents
	Read(ctx context.Context) (string, error)
	//ModTime get latest modification time
	ModTime(ctx context.Context) (int64, error)
}

Reader Reader is the interface that wrap the Read and ModTime method of config

func CreateFsReader

func CreateFsReader(ctx context.Context, name string) Reader

CreateFsReader create an instance of FsReader

type Section

type Section struct {
	//Name the name of section
	Name string
	// contains filtered or unexported fields
}

Section information associated to a section in a INI File

func (*Section) Keys

func (s *Section) Keys() []string

Keys return key list

func (*Section) Value

func (s *Section) Value(key string, defaultValue string) string

Value get string with key. return defaultValue if it doesn't exists

func (*Section) ValueBool

func (s *Section) ValueBool(key string, defaultValue bool) bool

ValueBool get bool with key. return defaultValue if it doesn't exists or is invalid value. valid values: 0/1, on/off, true/false.

func (*Section) ValueFloat32

func (s *Section) ValueFloat32(key string, defaultValue float32) float32

ValueFloat32 get float32 with key. return defaultValue if it doesn't exists or is invalid float32

func (*Section) ValueFloat64

func (s *Section) ValueFloat64(key string, defaultValue float64) float64

ValueFloat64 get float64 with key. return defaultValue if it doesn't exists or is invalid float32

func (*Section) ValueInt

func (s *Section) ValueInt(key string, defaultValue int) int

ValueInt get int with key. return defaultValue if it doesn't exists or is invalid int

func (*Section) ValueInt32

func (s *Section) ValueInt32(key string, defaultValue int32) int32

ValueInt32 get int32 with key. return defaultValue if it doesn't exists or is invalid int64

func (*Section) ValueInt64

func (s *Section) ValueInt64(key string, defaultValue int64) int64

ValueInt64 get int64 with key. return defaultValue if it doesn't exists or is invalid int64

Jump to

Keyboard shortcuts

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