configer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: Apache-2.0 Imports: 1 Imported by: 11

README

Configer

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

如何使用

go get -u github.com/codingXiang/configer

範例

建立 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

This section is empty.

Types

type Configer

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

Configer : 整體設定檔

func (*Configer) AddCore

func (this *Configer) AddCore(key string, handler CoreInterface) ConfigerInterface

AddCore : 加入組態控制器

func (*Configer) GetCore

func (this *Configer) GetCore(key string) CoreInterface

GetCore : 取得組態控制器

type ConfigerInterface

type ConfigerInterface interface {
	GetCore(key string) CoreInterface
	AddCore(key string, handler CoreInterface) ConfigerInterface
}
var (
	//Config : 設定檔變數
	Config ConfigerInterface
)

func NewConfiger

func NewConfiger() ConfigerInterface

NewConfiger 初始化

type Core

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

func (*Core) AddConfigPath

func (this *Core) AddConfigPath(in string)

func (*Core) ReadConfig

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

func (*Core) SetAutomaticEnv

func (this *Core) SetAutomaticEnv()

func (*Core) SetConfigName

func (this *Core) SetConfigName(in string)

func (*Core) SetConfigType

func (this *Core) SetConfigType(in string)

func (*Core) SetDefault added in v1.0.1

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

func (*Core) WriteConfig added in v1.0.1

func (this *Core) WriteConfig() error

func (*Core) WriteConfigAs added in v1.0.1

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

type CoreInterface

type CoreInterface interface {
	SetAutomaticEnv()
	SetDefault(key string, value interface{}) CoreInterface
	WriteConfig() error
	WriteConfigAs(path string) error
	SetConfigType(in string)
	SetConfigName(in string)
	AddConfigPath(in string)
	ReadConfig() (*viper.Viper, error)
}

func NewConfigerCore

func NewConfigerCore(configType string, configName string, paths ...string) CoreInterface

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

Jump to

Keyboard shortcuts

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