Documentation
¶
Overview ¶
Package config is config file manager for golang
Index ¶
- Variables
- type AtomicFieldBool
- type Config
- type Option
- type SpringConfigServer
- func (c *SpringConfigServer) Fetch() error
- func (c *SpringConfigServer) Get(name string) (interface{}, bool)
- func (c *SpringConfigServer) GetBool(name string) (val bool, ok bool)
- func (c *SpringConfigServer) GetInt(name string) (val int, ok bool)
- func (c *SpringConfigServer) GetString(name string) (string, bool)
- func (c *SpringConfigServer) Map(set func(string, interface{}))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var S = Shared
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
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) ¶
/*
import {
"github.com/spf13/cobra"
}
// with cobra command
rootCmd := &cobra.Command{}
childCmd := &cobra.Command{
PreRun: func(cmd *cobra.Command, args []string) {
Settings.BindPFlags(cmd.Flags()); err != nil {
Logger.Panic("parse args")
}
},
}
rootCmd.AddCommand(childCmd)
childCmd.Flags().BoolP("verbose", "v", false, "verbose")
fmt.Println(Settings.GetBool("verbose"))
// Output: false
*/
type Option ¶ added in v1.0.1
type Option func(*option) error
Option opt for settings
func WithAesEncrypt ¶ added in v1.0.1
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
WithEncryptedFileSuffix only decrypt files which name ends with `encryptedSuffix`
func WithWatchFileModified ¶ added in v1.0.1
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")
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)`