configer

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: Apache-2.0 Imports: 3 Imported by: 29

README

Configer

封裝 github.com/spf13/viper 的套件

如何使用

go get -u github.com/codingXiang/configer/v2

範例

建立 Configer
Config = NewConfiger()
建立 ConfigCore
// 參數依序為:
/// 1. 設定檔類型 (支援 yaml、yml、json、properties、ini、hcl、toml)
/// 2. 檔案名稱 (例如檔名為 config.yaml 就輸入 config)
/// 3. 後續皆為檔案路徑,可以支援多個路徑尋找檔案
var config = NewConfigerCore("yaml", "config", "./config", ".")
加入與取得 ConfigCore 到 Configer
// 設定 core 的 key
Config.AddCore("config", config)
// 透過 key 取得 core
Config.GetCore("config")
取得組態內容
// 判斷讀取 core 是否出現錯誤
if data, err := Config.GetCore("config").ReadConfig(); err == nil {
    // 取得組態裡面設定為 content 的資料
    fmt.Println(data.Get("content"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigPath

func GetConfigPath(key string, path ...string) string

Types

type Configer

type Configer struct {
	// contains filtered or unexported fields
}
var (
	//Config : 設定檔變數
	Config *Configer
)

func NewConfiger

func NewConfiger() *Configer

NewConfiger 初始化

func (*Configer) AddCore

func (c *Configer) AddCore(key string, handler *Core) *Configer

AddCore : 加入組態控制器

func (*Configer) GetCore

func (c *Configer) GetCore(key string) *Core

GetCore : 取得組態控制器

type Core

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

func NewCore

func NewCore(configType FileType, configName string, paths ...string) *Core

參數依序為: / 1. 設定檔類型 (支援 yaml、yml、json、properties、ini、hcl、toml) / 2. 檔案名稱 (例如檔名為 config.yaml 就輸入 config) / 3. 後續皆為檔案路徑,可以支援多個路徑尋找檔案

func NewCoreWithData

func NewCoreWithData(configType FileType, data []byte) *Core

func (*Core) AddConfigPath

func (c *Core) AddConfigPath(in string)

func (*Core) GetConfig

func (c *Core) GetConfig() *viper.Viper

func (*Core) ReadConfig

func (c *Core) ReadConfig() (*viper.Viper, error)

func (*Core) SetAutomaticEnv

func (c *Core) SetAutomaticEnv(prefix, oldStr, newStr string)

func (*Core) SetConfigName

func (c *Core) SetConfigName(in string)

func (*Core) SetConfigType

func (c *Core) SetConfigType(in string)

func (*Core) SetDefault

func (c *Core) SetDefault(key string, value interface{}) *Core

func (*Core) WriteConfig

func (c *Core) WriteConfig() error

func (*Core) WriteConfigAs

func (c *Core) WriteConfigAs(path string) error

type FileType

type FileType int
const (
	YAML FileType = iota
	JSON
	TOML
	PROPERTIES
	INI
	HCL
)

func NewFileType

func NewFileType(f string) FileType

func (FileType) String

func (t FileType) String() string

Jump to

Keyboard shortcuts

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