cfg

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 13 Imported by: 2

README

一个简单的 toml/yaml 配置读取包

这是一个非常简单的配置读取包,功能非常单一,使用非常简单;

安装

gitee.com/zhucheer/cfg

使用

cfgHandler, _ := cfg.New("./config.toml") //设置配置文件地址
value := cfgHandler.GetInt("database.connection_max") //读取toml配置节点


Nacos 支持

1.首先,我们需要在项目中初始化 Nacos Client。

clientConfig := *constant.NewClientConfig(
		constant.WithNotLoadCacheAtStart(true),
		constant.WithLogDir("./nacos/log"),
		constant.WithCacheDir("./nacos/cache"),
		constant.WithRotateTime("1h"),
		constant.WithMaxAge(6),
		constant.WithLogLevel("info"),
		constant.WithNamespaceId(""),
		constant.WithUsername(),
		constant.WithPassword()
	)
	configClient, _ := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig: &clientConfig,
			ServerConfigs: []constant.ServerConfig{
				{
					IpAddr:      "106.55.145.120",
					ContextPath: "/nacos",
					Port:        8848,
					Scheme:      "http",
				},
			},
		},
	)

// clientConfig,configClient可以配置成全局变量,在需要使用nacos的地方直接调用

2.将nacos配置实例化到cfg包的handler

    handler := NewNacos(&NacosBase{
		ConfigClient: configClient,
		GroupId:      "orange10",
		DataId:       "config.toml",
	})

	key := handler2.GetString("app.name")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler added in v0.1.3

type Handler struct {
	Source   string
	ConfType string
	// contains filtered or unexported fields
}

配置对象

func New

func New(filePath string) *Handler

New default get a cfg handler by file path and not open dynamic

func NewFile added in v0.0.5

func NewFile(filePath string, dynamic bool) *Handler

NewFile get a cfg handler by file path

func NewNacos added in v0.1.5

func NewNacos(baseInfo *NacosBase) *Handler

NewNacosData get a cfg handler by toml string content nacos目前没有toml支持,暂时统一使用yaml格式

func NewTomlData added in v0.1.1

func NewTomlData(content string) *Handler

NewTomlData get a cfg handler by toml string content

func NewYamlData added in v0.1.1

func NewYamlData(content string) *Handler

NewTomlData get a cfg handler by toml string content

func ReloadTomlData added in v0.1.8

func ReloadTomlData(content string) (*Handler, error)

ReloadTomlData get a cfg handler by toml string content

func ReloadYamlData added in v0.1.7

func ReloadYamlData(content string) (*Handler, error)

ReloadYamlData get a cfg handler by toml string content

func (*Handler) Exists added in v0.1.3

func (c *Handler) Exists(parse string) bool

Exists find config node exists

func (*Handler) GetBool added in v0.1.3

func (c *Handler) GetBool(key string) bool

GetBool get config bool type

func (*Handler) GetDuration added in v0.1.3

func (c *Handler) GetDuration(key string) time.Duration

GetDuration get config time.Duration type

func (*Handler) GetInt added in v0.1.3

func (c *Handler) GetInt(key string) int

GetInt get config int type

func (*Handler) GetInt64 added in v0.1.3

func (c *Handler) GetInt64(key string) int64

GetInt64 get config int64 type

func (*Handler) GetMap added in v0.1.3

func (c *Handler) GetMap(parse string) map[string]interface{}

GetMap parse config node to map

func (*Handler) GetSliceInt added in v0.1.3

func (c *Handler) GetSliceInt(key string) []int

GetSliceInt get slice int type

func (*Handler) GetSliceInt64 added in v0.1.3

func (c *Handler) GetSliceInt64(key string) []int64

GetSliceInt64 get slice int64 type

func (*Handler) GetSliceString added in v0.1.3

func (c *Handler) GetSliceString(key string) []string

GetSliceString get slice string type

func (*Handler) GetString added in v0.1.3

func (c *Handler) GetString(key string) string

GetString get config string type

func (*Handler) ParseNode added in v0.1.3

func (c *Handler) ParseNode(parse string) (ret interface{})

ParseNode parse config node

func (*Handler) ReloadFile added in v0.2.0

func (c *Handler) ReloadFile() *Handler

ReloadFile get a cfg handler by file path reload config

func (*Handler) StartDynamic added in v0.2.0

func (c *Handler) StartDynamic() *Handler

StartDynamic after start listen config file change

func (*Handler) UnmarshalKey added in v0.2.0

func (c *Handler) UnmarshalKey(key string, rawVal interface{}) error

UnmarshalKey Unmarshal config to struct

type NacosBase added in v0.1.5

type NacosBase struct {
	ConfigClient config_client.IConfigClient
	GroupId      string
	DataId       string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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