config

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 20 Imported by: 2

README

Go-Config

Separate from Laisky/go-utils Settings

Documentation

Overview

Package config is config file manager for golang

Index

Examples

Constants

This section is empty.

Variables

View Source
var Shared = New()

Shared is the settings for this project

enhance viper.Viper with threadsafe and richer features.

Basic Usage

  import gutils "github.com/Laisky/go-utils/v2"

	 gutils.Shared.

Functions

This section is empty.

Types

type AtomicFieldBool

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

AtomicFieldBool is a bool field which is goroutine-safe

Example
type foo struct {
	v AtomicFieldBool
}

f := new(foo)
f.v.SetTrue()
fmt.Println(f.v.True())
Output:

true

func (*AtomicFieldBool) SetFalse

func (a *AtomicFieldBool) SetFalse()

SetFalse set false

func (*AtomicFieldBool) SetTrue

func (a *AtomicFieldBool) SetTrue()

SetTrue set true

func (*AtomicFieldBool) True

func (a *AtomicFieldBool) True() bool

True value == true

type Config

type Config interface {
	BindPFlags(p *pflag.FlagSet) error
	Get(key string) interface{}
	GetString(key string) string
	GetStringSlice(key string) []string
	GetBool(key string) bool
	GetInt(key string) int
	GetInt64(key string) int64
	GetDuration(key string) time.Duration
	Set(key string, val interface{})
	IsSet(key string) bool
	Unmarshal(obj interface{}) error
	UnmarshalKey(key string, obj interface{}) error
	GetStringMap(key string) map[string]interface{}
	GetStringMapString(key string) map[string]string
	ReadConfig(in io.Reader) error
	MergeConfig(in io.Reader) error
	LoadFromDir(dirPath string, opts ...Option) error
	LoadFromFile(entryFile string, opts ...Option) (err error)

	LoadFromConfigServer(url, app, profile, label string) (err error)
	LoadFromConfigServerWithRawYaml(url, app, profile, label, key string) (err error)
	LoadSettings()
	// contains filtered or unexported methods
}

Config to load configurations from file

Features

support encrypted file with AES

support `include: xxx.toml` to include other file

support watch file changes and auto reload

goroutine-safe viper

Example

  import gconfig "github.com/Laisky/go-config"

  cfg := gconfig.New()
  cfg.LoadFromFile("/etc/app/settings.yml",
  	gconfig.WithEnableInclude(),
		gconfig.WithAesEncrypt([]byte("secret")),
		gconfig.WithWatchFileModified(nil),
  )

  cfg.Unmarshal(&yourConfigStruct)

More informations can be found at godoc samples

Example (Cobra)

Output:

func New

func New() Config

New new settings

type Option added in v1.0.1

type Option func(*option) error

Option opt for settings

func WithAesEncrypt added in v1.0.1

func WithAesEncrypt(key []byte) Option

WithAesEncrypt decrypt config file by aes

func WithEnableInclude added in v1.0.1

func WithEnableInclude() Option

WithEnableInclude enable `include` in config file

func WithEncryptedFileSuffix added in v1.0.1

func WithEncryptedFileSuffix(suffix string) Option

WithEncryptedFileSuffix only decrypt files which name ends with `encryptedSuffix`

func WithWatchFileModified added in v1.0.1

func WithWatchFileModified(callback func(fsnotify.Event)) Option

WithWatchFileModified automate update when file modified

callback will be called when file modified. you can set callback to nil if you don't want to process file changing event manually.

type SpringConfigServer

type SpringConfigServer struct {
	RemoteCfg *remoteCfg
	// contains filtered or unexported fields
}

SpringConfigServer can load configuration from Spring-Cloud-Config-Server

Example
var (
	url     = "http://config-server.un.org"
	app     = "appname"
	profile = "sit"
	label   = "master"
)

c := NewSpringConfigServer(url, app, profile, label)
c.Get("management.context-path")
c.GetString("management.context-path")
c.GetBool("endpoints.health.sensitive")
c.GetInt("spring.cloud.config.retry")
Output:

func NewSpringConfigServer

func NewSpringConfigServer(url, app, profile, label string) *SpringConfigServer

NewSpringConfigServer create ConfigSrv

func (*SpringConfigServer) Fetch

func (c *SpringConfigServer) Fetch() error

Fetch load data from config-server

func (*SpringConfigServer) Get

func (c *SpringConfigServer) Get(name string) (interface{}, bool)

Get get `interface{}` from the localcache of config-server

func (*SpringConfigServer) GetBool

func (c *SpringConfigServer) GetBool(name string) (val bool, ok bool)

GetBool get `bool` from the localcache of config-server

func (*SpringConfigServer) GetInt

func (c *SpringConfigServer) GetInt(name string) (val int, ok bool)

GetInt get `int` from the localcache of config-server

func (*SpringConfigServer) GetString

func (c *SpringConfigServer) GetString(name string) (string, bool)

GetString get `string` from the localcache of config-server

func (*SpringConfigServer) Map

func (c *SpringConfigServer) Map(set func(string, interface{}))

Map interate `set(k, v)`

Jump to

Keyboard shortcuts

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