Documentation ¶
Overview ¶
Package libconfd provides mini confd lib.
Example ¶
package main import ( "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) libconfd.NewProcessor().Run(cfg, backendClient) }
Output:
Example (Async) ¶
package main import ( "fmt" "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) call := libconfd.NewProcessor().Go(cfg, backendClient) // do some thing call = <-call.Done // will be equal to call fmt.Println(call.Error) }
Output:
Example (Close) ¶
package main import ( "os" "os/signal" "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) p := libconfd.NewProcessor() go func() { defer p.Close() c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) <-c }() p.Run(cfg, backendClient) }
Output:
Example (Logger) ¶
package main import ( "github.com/chai2010/libconfd" ) func main() { var logger = libconfd.GetLogger() logger.SetLevel("DEBUG") logger.Debug("1+1=2") logger.Info("hello") }
Output:
Example (MultiAsync) ¶
package main import ( "fmt" "sync" "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) var callList = []*libconfd.Call{ libconfd.NewProcessor().Go(cfg, backendClient), libconfd.NewProcessor().Go(cfg, backendClient), } var wg sync.WaitGroup for i := 0; i < len(callList); i++ { wg.Add(1) go func(i int) { <-callList[i].Done wg.Done() }(i) } wg.Wait() fmt.Println("Done") }
Output:
Example (MultiSync) ¶
package main import ( "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) go libconfd.NewProcessor().Run(cfg, backendClient) go libconfd.NewProcessor().Run(cfg, backendClient) <-make(chan bool) }
Output:
Example (Option) ¶
package main import ( "github.com/chai2010/libconfd" ) func main() { cfg := libconfd.MustLoadConfig("./confd.toml") backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml") backendClient := libconfd.MustNewBackendClient(backendConfig) libconfd.NewProcessor().Run(cfg, backendClient, libconfd.WithIntervalMode(), ) }
Output:
Index ¶
- Constants
- func RegisterBackendClient(typeName string, newClient func(cfg *BackendConfig) (BackendClient, error))
- type Application
- type BackendClient
- type BackendConfig
- type Call
- type Config
- type KVPair
- type KVStore
- func (p *KVStore) Del(key string)
- func (p *KVStore) Exists(key string) bool
- func (p *KVStore) Get(key string) (kv KVPair, ok bool)
- func (p *KVStore) GetAll(pattern string) ([]KVPair, error)
- func (p *KVStore) GetAllValues(pattern string) ([]string, error)
- func (p *KVStore) GetValue(key string, v ...string) (s string, ok bool)
- func (p *KVStore) List(filePath string) []string
- func (p *KVStore) ListDir(filePath string) []string
- func (s *KVStore) Purge()
- func (s *KVStore) Set(key string, value string)
- type Logger
- type Options
- func WithAbsKeyAdjuster(fn func(absKey string) (realKey string)) Options
- func WithFuncMap(maps ...template.FuncMap) Options
- func WithFuncMapUpdater(fn func(m template.FuncMap, basefn *TemplateFunc)) Options
- func WithHookOnCheckCmdDone(fn func(trName, cmd string, err error)) Options
- func WithHookOnReloadCmdDone(fn func(trName, cmd string, err error)) Options
- func WithHookOnUpdateDone(fn func(trName string, err error)) Options
- func WithInterval(interval int) Options
- func WithIntervalMode() Options
- func WithOnetimeMode() Options
- func WithWatchMode() Options
- type Processor
- type TemplateFunc
- func (_ TemplateFunc) Add(a, b int) int
- func (_ TemplateFunc) Atoi(s string) int
- func (_ TemplateFunc) Base(path string) string
- func (_ TemplateFunc) Base64Decode(data string) string
- func (_ TemplateFunc) Base64Encode(data string) string
- func (p TemplateFunc) Cget(key string) KVPair
- func (p TemplateFunc) Cgets(pattern string) []KVPair
- func (p TemplateFunc) Cgetv(key string) string
- func (p TemplateFunc) Cgetvs(pattern string) []string
- func (_ TemplateFunc) Contains(s, substr string) bool
- func (_ TemplateFunc) Datetime() time.Time
- func (_ TemplateFunc) Dir(path string) string
- func (_ TemplateFunc) Div(a, b int) int
- func (p TemplateFunc) Exists(key string) bool
- func (_ TemplateFunc) FileExists(filepath string) bool
- func (p TemplateFunc) Get(key string) KVPair
- func (_ TemplateFunc) Getenv(key string, defaultValue ...string) string
- func (p TemplateFunc) Gets(pattern string) []KVPair
- func (p TemplateFunc) Getv(key string, v ...string) string
- func (p TemplateFunc) Getvs(pattern string) []string
- func (_ TemplateFunc) Join(a []string, sep string) string
- func (_ TemplateFunc) Json(data string) map[string]interface{}
- func (_ TemplateFunc) JsonArray(data string) []interface{}
- func (_ TemplateFunc) LookupIP(data string) []string
- func (_ TemplateFunc) LookupIPV4(data string) []string
- func (_ TemplateFunc) LookupIPV6(data string) []string
- func (_ TemplateFunc) LookupSRV(service, proto, name string) []*net.SRV
- func (p TemplateFunc) Ls(filepath string) []string
- func (p TemplateFunc) Lsdir(filepath string) []string
- func (_ TemplateFunc) Map(values ...interface{}) map[string]interface{}
- func (_ TemplateFunc) Mod(a, b int) int
- func (_ TemplateFunc) Mul(a, b int) int
- func (_ TemplateFunc) ParseBool(s string) bool
- func (_ TemplateFunc) Replace(s, old, new string, n int) string
- func (_ TemplateFunc) Reverse(values interface{}) interface{}
- func (_ TemplateFunc) Seq(first, last int) []int
- func (_ TemplateFunc) SortByLength(values []string) []string
- func (_ TemplateFunc) SortKVByLength(values []KVPair) []KVPair
- func (_ TemplateFunc) Split(s, sep string) []string
- func (_ TemplateFunc) Sub(a, b int) int
- func (_ TemplateFunc) ToLower(s string) string
- func (_ TemplateFunc) ToUpper(s string) string
- func (_ TemplateFunc) TrimSuffix(s, suffix string) string
- type TemplateResource
- type TemplateResourceProcessor
- type TomlBackend
- func (_ *TomlBackend) Close() error
- func (p *TomlBackend) GetValues(keys []string) (m map[string]string, err error)
- func (_ *TomlBackend) Type() string
- func (_ *TomlBackend) WatchEnabled() bool
- func (_ *TomlBackend) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error)
Examples ¶
Constants ¶
const TomlBackendType = "libconfd-backend-toml"
Variables ¶
This section is empty.
Functions ¶
func RegisterBackendClient ¶
func RegisterBackendClient( typeName string, newClient func(cfg *BackendConfig) (BackendClient, error), )
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
func NewApplication ¶
func NewApplication(cfg *Config, client BackendClient) *Application
func (*Application) GetValues ¶
func (p *Application) GetValues(keys ...string)
func (*Application) Info ¶
func (p *Application) Info(names ...string)
func (*Application) List ¶
func (p *Application) List(re string)
func (*Application) Make ¶
func (p *Application) Make(names ...string)
func (*Application) Run ¶
func (p *Application) Run(opts ...Options)
type BackendClient ¶
type BackendClient interface { Type() string GetValues(keys []string) (map[string]string, error) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) WatchEnabled() bool Close() error }
func MustNewBackendClient ¶
func MustNewBackendClient(cfg *BackendConfig, opts ...func(*BackendConfig)) BackendClient
func NewBackendClient ¶
func NewBackendClient(cfg *BackendConfig, opts ...func(*BackendConfig)) (BackendClient, error)
type BackendConfig ¶
type BackendConfig struct { Type string `toml:"type" json:"type"` Host []string `toml:"host" json:"host"` UserName string `toml:"user" json:"user"` Password string `toml:"password" json:"password"` ClientCAKeys string `toml:"client_ca_keys" json:"client_ca_keys"` ClientCert string `toml:"client_cert" json:"client_cert"` ClientKey string `toml:"client_key" json:"client_key"` HookKeyAdjuster func(key string) (realKey string) `toml:"-" json:"-"` }
func LoadBackendConfig ¶
func LoadBackendConfig(path string) (p *BackendConfig, err error)
func LoadBackendConfigFromJsonString ¶
func LoadBackendConfigFromJsonString(s string) (p *BackendConfig, err error)
func MustLoadBackendConfig ¶
func MustLoadBackendConfig(path string) *BackendConfig
func (*BackendConfig) Clone ¶
func (p *BackendConfig) Clone() *BackendConfig
type Config ¶
type Config struct { // The path to confd configs. // If the confdir is rel path, must convert to abs path. // // abspath = filepath.Join(ConfigPath, Config.ConfDir) // ConfDir string `toml:"confdir" json:"confdir"` // The backend polling interval in seconds. (10) Interval int `toml:"interval" json:"interval"` // Enable noop mode. Process all template resources; skip target update. Noop bool `toml:"noop" json:"noop"` // The string to prefix to keys. ("/") Prefix string `toml:"prefix" json:"prefix"` // sync without check_cmd and reload_cmd. SyncOnly bool `toml:"sync_only" json:"sync_only"` // level which confd should log messages // DEBUG/INFO/WARN/ERROR/PANIC LogLevel string `toml:"log_level" json:"log_level"` // run once and exit Onetime bool `toml:"onetime" json:"onetime"` // enable watch support Watch bool `toml:"watch" json:"watch"` // keep staged files KeepStageFile bool `toml:"keep_stage_file" json:"keep_stage_file"` // PGP secret keyring (for use with crypt functions) PGPPrivateKey string `toml:"pgp_private_key" json:"pgp_private_key"` FuncMap template.FuncMap `toml:"-" json:"-"` FuncMapUpdater func(m template.FuncMap, basefn *TemplateFunc) `toml:"-" json:"-"` HookAbsKeyAdjuster func(absKey string) (realKey string) `toml:"-" json:"-"` HookOnCheckCmdDone func(trName, cmd string, err error) `toml:"-" json:"-"` HookOnReloadCmdDone func(trName, cmd string, err error) `toml:"-" json:"-"` HookOnUpdateDone func(trName string, err error) `toml:"-" json:"-"` }
func LoadConfig ¶
func MustLoadConfig ¶
func (*Config) GetConfigDir ¶
func (*Config) GetDefaultTemplateOutputDir ¶
func (*Config) GetTemplateDir ¶
type KVStore ¶
type KVStore struct {
// contains filtered or unexported fields
}
A KVStore represents an in-memory key-value store safe for concurrent access.
func (*KVStore) Get ¶
Get gets the KVPair associated with key. If there is no KVPair associated with key, Get returns KVPair{}, false.
func (*KVStore) GetAll ¶
GetAll returns a KVPair for all nodes with keys matching pattern. The syntax of patterns is the same as in path.Match.
type Logger ¶
type Logger interface { Assert(condition bool, v ...interface{}) Assertln(condition bool, v ...interface{}) Assertf(condition bool, format string, v ...interface{}) Debug(v ...interface{}) Debugln(v ...interface{}) Debugf(format string, v ...interface{}) Info(v ...interface{}) Infoln(v ...interface{}) Infof(format string, v ...interface{}) Warning(v ...interface{}) Warningln(v ...interface{}) Warningf(format string, v ...interface{}) Error(v ...interface{}) Errorln(v ...interface{}) Errorf(format string, v ...interface{}) Panic(v ...interface{}) Panicln(v ...interface{}) Panicf(format string, v ...interface{}) Fatal(v ...interface{}) Fatalln(v ...interface{}) Fatalf(format string, v ...interface{}) // Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL GetLevel() string SetLevel(new string) (old string) }
Logger interface
See https://github.com/chai2010/logger
func NewStdLogger ¶
NewStdLogger create new logger based on std log. If level is empty string, use WARN as the default level. If flag is zore, use 'log.LstdFlags|log.Lshortfile' as the default flag. Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL
type Options ¶
type Options func(*Config)
func WithAbsKeyAdjuster ¶
func WithFuncMap ¶
func WithFuncMapUpdater ¶
func WithFuncMapUpdater(fn func(m template.FuncMap, basefn *TemplateFunc)) Options
func WithHookOnCheckCmdDone ¶
func WithHookOnReloadCmdDone ¶
func WithHookOnUpdateDone ¶
func WithInterval ¶
func WithIntervalMode ¶
func WithIntervalMode() Options
func WithOnetimeMode ¶
func WithOnetimeMode() Options
func WithWatchMode ¶
func WithWatchMode() Options
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
func NewProcessor ¶
func NewProcessor() *Processor
type TemplateFunc ¶
type TemplateFunc struct { FuncMap map[string]interface{} Store *KVStore PGPPrivateKey []byte HookKeyAdjuster func(key string) (realKey string) }
func NewTemplateFunc ¶
func NewTemplateFunc( store *KVStore, pgpPrivateKey []byte, hookKeyAdjuster func(key string) (realKey string), ) *TemplateFunc
func (TemplateFunc) Add ¶
func (_ TemplateFunc) Add(a, b int) int
func (TemplateFunc) Atoi ¶
func (_ TemplateFunc) Atoi(s string) int
func (TemplateFunc) Base ¶
func (_ TemplateFunc) Base(path string) string
func (TemplateFunc) Base64Decode ¶
func (_ TemplateFunc) Base64Decode(data string) string
func (TemplateFunc) Base64Encode ¶
func (_ TemplateFunc) Base64Encode(data string) string
func (TemplateFunc) Cget ¶
func (p TemplateFunc) Cget(key string) KVPair
func (TemplateFunc) Cgets ¶
func (p TemplateFunc) Cgets(pattern string) []KVPair
func (TemplateFunc) Cgetv ¶
func (p TemplateFunc) Cgetv(key string) string
func (TemplateFunc) Cgetvs ¶
func (p TemplateFunc) Cgetvs(pattern string) []string
func (TemplateFunc) Contains ¶
func (_ TemplateFunc) Contains(s, substr string) bool
func (TemplateFunc) Datetime ¶
func (_ TemplateFunc) Datetime() time.Time
func (TemplateFunc) Dir ¶
func (_ TemplateFunc) Dir(path string) string
func (TemplateFunc) Div ¶
func (_ TemplateFunc) Div(a, b int) int
func (TemplateFunc) Exists ¶
func (p TemplateFunc) Exists(key string) bool
func (TemplateFunc) FileExists ¶
func (_ TemplateFunc) FileExists(filepath string) bool
func (TemplateFunc) Get ¶
func (p TemplateFunc) Get(key string) KVPair
func (TemplateFunc) Getenv ¶
func (_ TemplateFunc) Getenv(key string, defaultValue ...string) string
getenv retrieves the value of the environment variable named by the key. It returns the value, which will the default value if the variable is not present. If no default value was given - returns "".
func (TemplateFunc) Gets ¶
func (p TemplateFunc) Gets(pattern string) []KVPair
func (TemplateFunc) Getvs ¶
func (p TemplateFunc) Getvs(pattern string) []string
func (TemplateFunc) Json ¶
func (_ TemplateFunc) Json(data string) map[string]interface{}
func (TemplateFunc) JsonArray ¶
func (_ TemplateFunc) JsonArray(data string) []interface{}
func (TemplateFunc) LookupIP ¶
func (_ TemplateFunc) LookupIP(data string) []string
func (TemplateFunc) LookupIPV4 ¶
func (_ TemplateFunc) LookupIPV4(data string) []string
func (TemplateFunc) LookupIPV6 ¶
func (_ TemplateFunc) LookupIPV6(data string) []string
func (TemplateFunc) LookupSRV ¶
func (_ TemplateFunc) LookupSRV(service, proto, name string) []*net.SRV
func (TemplateFunc) Ls ¶
func (p TemplateFunc) Ls(filepath string) []string
func (TemplateFunc) Lsdir ¶
func (p TemplateFunc) Lsdir(filepath string) []string
func (TemplateFunc) Map ¶
func (_ TemplateFunc) Map(values ...interface{}) map[string]interface{}
Map creates a key-value map of string -> interface{} The i'th is the key and the i+1 is the value
func (TemplateFunc) Mod ¶
func (_ TemplateFunc) Mod(a, b int) int
func (TemplateFunc) Mul ¶
func (_ TemplateFunc) Mul(a, b int) int
func (TemplateFunc) ParseBool ¶
func (_ TemplateFunc) ParseBool(s string) bool
func (TemplateFunc) Reverse ¶
func (_ TemplateFunc) Reverse(values interface{}) interface{}
reverse returns the array in reversed order works with []string and []KVPair
func (TemplateFunc) Seq ¶
func (_ TemplateFunc) Seq(first, last int) []int
seq creates a sequence of integers. It's named and used as GNU's seq. seq takes the first and the last element as arguments. So Seq(3, 5) will generate [3,4,5]
func (TemplateFunc) SortByLength ¶
func (_ TemplateFunc) SortByLength(values []string) []string
func (TemplateFunc) SortKVByLength ¶
func (_ TemplateFunc) SortKVByLength(values []KVPair) []KVPair
func (TemplateFunc) Split ¶
func (_ TemplateFunc) Split(s, sep string) []string
func (TemplateFunc) Sub ¶
func (_ TemplateFunc) Sub(a, b int) int
func (TemplateFunc) ToLower ¶
func (_ TemplateFunc) ToLower(s string) string
func (TemplateFunc) ToUpper ¶
func (_ TemplateFunc) ToUpper(s string) string
func (TemplateFunc) TrimSuffix ¶
func (_ TemplateFunc) TrimSuffix(s, suffix string) string
type TemplateResource ¶
type TemplateResource struct { Src string `toml:"src" json:"src"` Dest string `toml:"dest" json:"dest"` Prefix string `toml:"prefix" json:"prefix"` Keys []string `toml:"keys" json:"keys"` Mode string `toml:"mode" json:"mode"` Gid int `toml:"gid" json:"gid"` Uid int `toml:"uid" json:"uid"` CheckCmd string `toml:"check_cmd" json:"check_cmd"` ReloadCmd string `toml:"reload_cmd" json:"reload_cmd"` FileMode os.FileMode `toml:"file_mode" json:"file_mode"` PGPPrivateKey []byte `toml:"pgp_private_key" json:"pgp_private_key"` }
TemplateResource is the representation of a parsed template resource.
func ListTemplateResource ¶
func ListTemplateResource(confdir string) ([]*TemplateResource, []string, error)
func LoadTemplateResourceFile ¶
func LoadTemplateResourceFile(confdir, name string) (*TemplateResource, error)
func (*TemplateResource) SaveFile ¶
func (p *TemplateResource) SaveFile(path string) error
func (*TemplateResource) TomlString ¶
func (p *TemplateResource) TomlString() string
type TemplateResourceProcessor ¶
type TemplateResourceProcessor struct { TemplateResource // contains filtered or unexported fields }
func MakeAllTemplateResourceProcessor ¶
func MakeAllTemplateResourceProcessor( config *Config, client BackendClient, ) ( []*TemplateResourceProcessor, error, )
func NewTemplateResourceProcessor ¶
func NewTemplateResourceProcessor( path string, config *Config, client BackendClient, res *TemplateResource, ) *TemplateResourceProcessor
NewTemplateResourceProcessor creates a NewTemplateResourceProcessor.
func (*TemplateResourceProcessor) Process ¶
func (p *TemplateResourceProcessor) Process(call *Call) (err error)
process is a convenience function that wraps calls to the three main tasks required to keep local configuration files in sync. First we gather vars from the store, then we stage a candidate configuration file, and finally sync things up. It returns an error if any.
type TomlBackend ¶
type TomlBackend struct {
TOMLFile string
}
func NewTomlBackendClient ¶
func NewTomlBackendClient(cfg *BackendConfig) *TomlBackend
func (*TomlBackend) Close ¶
func (_ *TomlBackend) Close() error
func (*TomlBackend) GetValues ¶
func (p *TomlBackend) GetValues(keys []string) (m map[string]string, err error)
func (*TomlBackend) Type ¶
func (_ *TomlBackend) Type() string
func (*TomlBackend) WatchEnabled ¶
func (_ *TomlBackend) WatchEnabled() bool