Categorygithub.com/nextpkg/nextcfg
modulepackage
0.0.0-20241123081934-5b2a99bd3a3f
Repository: https://github.com/nextpkg/nextcfg.git
Documentation: pkg.go.dev

# README

能力

encoderloaderreadersecretssource
hclmemoryjsonboxconsul
jsonsecretboxenv
tomlfile
xmlflag
yamlmemory
rainbow
url

Import

import "github.com/nextpkg/nextcfg"

Initialize

File source

// 配置中心根据后缀名来判断用的解析器是哪个,yaml后缀名将使用yaml格式解析,json将以json格式解析
config.Init(NewConfig(), nextcfg.WithFileSource("my.yaml"))

Multi config source

// 多数据源的配置会被合并,按从前到后的顺序合并
config.Init(NewConfig(), nextcfg.WithFileSource("my.yaml"))

Load new source

s := toml.NewSource()

err := nextcfg.Load(s)
if err != nil {
    panic(err)
}

Example

// config is framework configuration
type config struct {
    // 示例:需用json作为字段的tag,但配置文件可以不用json格式
    Console  string  `json:"console"`
}

// Get 获取配置(原子操作)
func Get() *config {
    return config.GetCopy().(*config)
}

// NewConfig get framework configuration
func NewConfig() *config {
    return &config{
        // 示例:允许使用默认值,在配置中心不可用时将使用以下默认值
        Console: "abc",
    }
}

// Validate 验证配置是否有效,在配置被合理检查后才会被更新(未导出变量无法被复制)
func (c *config) Validate() error {
    // 如果返回值不是nil,则不会触发配置更新
    return nil
}

// Revoke 在新配置生效后,旧配置会调用Revoke取消
func (c *config) Revoke() {
		log.Info("revoke changed? ", c.Console)
}

# Packages

No description provided by the author
Package encoder handles source encoding formats.
Package loader manages loading from multiple sources.
Package reader parses change sets and provides config values.
No description provided by the author
Package secrets is an interface for encrypting and decrypting secrets.
Package source is the interface for sources.

# Functions

Bytes Return config as raw json.
Get a value from the config.
GetCopy returns config with watching.
GetOnce returns config once.
Init inits configurator.
InitLoader 初始化配置加载器.
Load config sources.
Map Return config as a map.
NewConfig returns new config.
Reload reload config.
Scan values to a go type.
Sync Force a source ChangeSet sync.
Watch a value for changes.
WithContext attach context.
WithLoader sets the loader for manager config.
WithReader sets the config reader.
WithScanner sets custom scanner.
WithSource appends a source to list of sources.

# Variables

Default Config Manager.

# Structs

Loaders 配置加载器.
Options ...

# Interfaces

Config is an interface abstraction for dynamic configuration.
Revoke 配置撤销.
Validate 验证配置.
Watcher is the config watcher.

# Type aliases

Loader 配置加载器.
Option ...
Scanner scans the config into struct.