mirror of
https://github.com/astaxie/beego.git
synced 2024-12-24 18:20:51 +00:00
Merge pull request #4143 from flycash/fix/moveIni
Move init so it will be default implementation of config
This commit is contained in:
commit
26b016a3a4
@ -48,22 +48,39 @@ import (
|
||||
)
|
||||
|
||||
// Configer defines how to get and set value from configuration raw data.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type Configer interface {
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Set(key, val string) error //support section::key type in given key when using ini type.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
String(key string) string //support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Strings(key string) []string //get string slice
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Int(key string) (int, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Int64(key string) (int64, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Bool(key string) (bool, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
Float(key string) (float64, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultString(key string, defaultVal string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultStrings(key string, defaultVal []string) []string //get string slice
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultInt(key string, defaultVal int) int
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultInt64(key string, defaultVal int64) int64
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultBool(key string, defaultVal bool) bool
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DefaultFloat(key string, defaultVal float64) float64
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
DIY(key string) (interface{}, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
GetSection(section string) (map[string]string, error)
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
SaveConfigFile(filename string) error
|
||||
}
|
||||
|
||||
|
5
config/env/env.go
vendored
5
config/env/env.go
vendored
@ -36,6 +36,7 @@ func init() {
|
||||
|
||||
// Get returns a value by key.
|
||||
// If the key does not exist, the default value will be returned.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func Get(key string, defVal string) string {
|
||||
if val := env.Get(key); val != nil {
|
||||
return val.(string)
|
||||
@ -45,6 +46,7 @@ func Get(key string, defVal string) string {
|
||||
|
||||
// MustGet returns a value by key.
|
||||
// If the key does not exist, it will return an error.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func MustGet(key string) (string, error) {
|
||||
if val := env.Get(key); val != nil {
|
||||
return val.(string), nil
|
||||
@ -54,12 +56,14 @@ func MustGet(key string) (string, error) {
|
||||
|
||||
// Set sets a value in the ENV copy.
|
||||
// This does not affect the child process environment.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func Set(key string, value string) {
|
||||
env.Set(key, value)
|
||||
}
|
||||
|
||||
// MustSet sets a value in the ENV copy and the child process environment.
|
||||
// It returns an error in case the set operation failed.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func MustSet(key string, value string) error {
|
||||
err := os.Setenv(key, value)
|
||||
if err != nil {
|
||||
@ -70,6 +74,7 @@ func MustSet(key string, value string) error {
|
||||
}
|
||||
|
||||
// GetAll returns all keys/values in the current child process environment.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func GetAll() map[string]string {
|
||||
items := env.Items()
|
||||
envs := make(map[string]string, env.Count())
|
||||
|
@ -27,16 +27,16 @@ type fakeConfigContainer struct {
|
||||
func (c *fakeConfigContainer) getData(key string) string {
|
||||
return c.data[strings.ToLower(key)]
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Set(key, val string) error {
|
||||
c.data[strings.ToLower(key)] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) String(key string) string {
|
||||
return c.getData(key)
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -44,7 +44,7 @@ func (c *fakeConfigContainer) DefaultString(key string, defaultval string) strin
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Strings(key string) []string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -52,7 +52,7 @@ func (c *fakeConfigContainer) Strings(key string) []string {
|
||||
}
|
||||
return strings.Split(v, ";")
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||
v := c.Strings(key)
|
||||
if v == nil {
|
||||
@ -60,11 +60,11 @@ func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Int(key string) (int, error) {
|
||||
return strconv.Atoi(c.getData(key))
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
@ -72,11 +72,11 @@ func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.getData(key), 10, 64)
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
@ -84,11 +84,11 @@ func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
|
||||
return ParseBool(c.getData(key))
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
@ -96,11 +96,11 @@ func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) Float(key string) (float64, error) {
|
||||
return strconv.ParseFloat(c.getData(key), 64)
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
@ -108,18 +108,18 @@ func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
|
||||
if v, ok := c.data[strings.ToLower(key)]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, errors.New("key not find")
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
return nil, errors.New("not implement in the fakeConfigContainer")
|
||||
}
|
||||
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
|
||||
return errors.New("not implement in the fakeConfigContainer")
|
||||
}
|
||||
@ -127,6 +127,7 @@ func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
|
||||
var _ Configer = new(fakeConfigContainer)
|
||||
|
||||
// NewFakeConfig return a fake Configer
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func NewFakeConfig() Configer {
|
||||
return &fakeConfigContainer{
|
||||
data: make(map[string]string),
|
||||
|
@ -41,10 +41,12 @@ var (
|
||||
)
|
||||
|
||||
// IniConfig implements Config to parse ini file.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type IniConfig struct {
|
||||
}
|
||||
|
||||
// Parse creates a new Config and parses the file configuration from the named file.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (ini *IniConfig) Parse(name string) (Configer, error) {
|
||||
return ini.parseFile(name)
|
||||
}
|
||||
@ -208,6 +210,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
|
||||
// ParseData parse ini the data
|
||||
// When include other.conf,other.conf is either absolute directory
|
||||
// or under beego in default temporary directory(/tmp/beego[-username]).
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
|
||||
dir := "beego"
|
||||
currentUser, err := user.Current()
|
||||
@ -224,6 +227,7 @@ func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
|
||||
|
||||
// IniConfigContainer A Config represents the ini configuration.
|
||||
// When set and get value, support key as section:name type.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type IniConfigContainer struct {
|
||||
data map[string]map[string]string // section=> key:val
|
||||
sectionComment map[string]string // section : comment
|
||||
@ -232,12 +236,14 @@ type IniConfigContainer struct {
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Bool(key string) (bool, error) {
|
||||
return ParseBool(c.getdata(key))
|
||||
}
|
||||
|
||||
// DefaultBool returns the boolean value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
@ -247,12 +253,14 @@ func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Int(key string) (int, error) {
|
||||
return strconv.Atoi(c.getdata(key))
|
||||
}
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
@ -262,12 +270,14 @@ func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.getdata(key), 10, 64)
|
||||
}
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
@ -277,12 +287,14 @@ func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Float(key string) (float64, error) {
|
||||
return strconv.ParseFloat(c.getdata(key), 64)
|
||||
}
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
@ -292,12 +304,14 @@ func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float6
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) String(key string) string {
|
||||
return c.getdata(key)
|
||||
}
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -308,6 +322,7 @@ func (c *IniConfigContainer) DefaultString(key string, defaultval string) string
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Return nil if config value does not exist or is empty.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Strings(key string) []string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -318,6 +333,7 @@ func (c *IniConfigContainer) Strings(key string) []string {
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||
v := c.Strings(key)
|
||||
if v == nil {
|
||||
@ -327,6 +343,7 @@ func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []s
|
||||
}
|
||||
|
||||
// GetSection returns map for the given section
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
if v, ok := c.data[section]; ok {
|
||||
return v, nil
|
||||
@ -337,6 +354,7 @@ func (c *IniConfigContainer) GetSection(section string) (map[string]string, erro
|
||||
// SaveConfigFile save the config into file.
|
||||
//
|
||||
// BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Function.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Write configuration file by filename.
|
||||
f, err := os.Create(filename)
|
||||
@ -437,6 +455,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Set writes a new value for key.
|
||||
// if write to one section, the key need be "section::key".
|
||||
// if the section is not existed, it panics.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) Set(key, value string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
@ -465,6 +484,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
|
||||
}
|
||||
|
||||
// DIY returns the raw value by a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
if v, ok := c.data[strings.ToLower(key)]; ok {
|
||||
return v, nil
|
||||
|
@ -26,10 +26,12 @@ import (
|
||||
)
|
||||
|
||||
// JSONConfig is a json config parser and implements Config interface.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type JSONConfig struct {
|
||||
}
|
||||
|
||||
// Parse returns a ConfigContainer with parsed json config map.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (js *JSONConfig) Parse(filename string) (Configer, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
@ -45,6 +47,7 @@ func (js *JSONConfig) Parse(filename string) (Configer, error) {
|
||||
}
|
||||
|
||||
// ParseData returns a ConfigContainer with json string
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
|
||||
x := &JSONConfigContainer{
|
||||
data: make(map[string]interface{}),
|
||||
@ -66,12 +69,14 @@ func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
|
||||
|
||||
// JSONConfigContainer A Config represents the json configuration.
|
||||
// Only when get value, support key as section:name type.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type JSONConfigContainer struct {
|
||||
data map[string]interface{}
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Bool(key string) (bool, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -82,6 +87,7 @@ func (c *JSONConfigContainer) Bool(key string) (bool, error) {
|
||||
|
||||
// DefaultBool return the bool value if has no error
|
||||
// otherwise return the defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
if v, err := c.Bool(key); err == nil {
|
||||
return v
|
||||
@ -90,6 +96,7 @@ func (c *JSONConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Int(key string) (int, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -105,6 +112,7 @@ func (c *JSONConfigContainer) Int(key string) (int, error) {
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
if v, err := c.Int(key); err == nil {
|
||||
return v
|
||||
@ -113,6 +121,7 @@ func (c *JSONConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Int64(key string) (int64, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -126,6 +135,7 @@ func (c *JSONConfigContainer) Int64(key string) (int64, error) {
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
if v, err := c.Int64(key); err == nil {
|
||||
return v
|
||||
@ -134,6 +144,7 @@ func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Float(key string) (float64, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -147,6 +158,7 @@ func (c *JSONConfigContainer) Float(key string) (float64, error) {
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
if v, err := c.Float(key); err == nil {
|
||||
return v
|
||||
@ -155,6 +167,7 @@ func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) String(key string) string {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -167,6 +180,7 @@ func (c *JSONConfigContainer) String(key string) string {
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
// TODO FIXME should not use "" to replace non existence
|
||||
if v := c.String(key); v != "" {
|
||||
@ -176,6 +190,7 @@ func (c *JSONConfigContainer) DefaultString(key string, defaultval string) strin
|
||||
}
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Strings(key string) []string {
|
||||
stringVal := c.String(key)
|
||||
if stringVal == "" {
|
||||
@ -186,6 +201,7 @@ func (c *JSONConfigContainer) Strings(key string) []string {
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||
if v := c.Strings(key); v != nil {
|
||||
return v
|
||||
@ -194,6 +210,7 @@ func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []
|
||||
}
|
||||
|
||||
// GetSection returns map for the given section
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
if v, ok := c.data[section]; ok {
|
||||
return v.(map[string]string), nil
|
||||
@ -202,6 +219,7 @@ func (c *JSONConfigContainer) GetSection(section string) (map[string]string, err
|
||||
}
|
||||
|
||||
// SaveConfigFile save the config into file
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Write configuration file by filename.
|
||||
f, err := os.Create(filename)
|
||||
@ -218,6 +236,7 @@ func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
}
|
||||
|
||||
// Set writes a new value for key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) Set(key, val string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
@ -226,6 +245,7 @@ func (c *JSONConfigContainer) Set(key, val string) error {
|
||||
}
|
||||
|
||||
// DIY returns the raw value by a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
@ -235,6 +255,7 @@ func (c *JSONConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
}
|
||||
|
||||
// section.key or key
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *JSONConfigContainer) getData(key string) interface{} {
|
||||
if len(key) == 0 {
|
||||
return nil
|
||||
|
@ -46,9 +46,11 @@ import (
|
||||
// Config is a xml config parser and implements Config interface.
|
||||
// xml configurations should be included in <config></config> tag.
|
||||
// only support key/value pair as <key>value</key> as each item.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type Config struct{}
|
||||
|
||||
// Parse returns a ConfigContainer with parsed xml config map.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (xc *Config) Parse(filename string) (config.Configer, error) {
|
||||
context, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
@ -59,6 +61,7 @@ func (xc *Config) Parse(filename string) (config.Configer, error) {
|
||||
}
|
||||
|
||||
// ParseData xml data
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
x := &ConfigContainer{data: make(map[string]interface{})}
|
||||
|
||||
@ -73,12 +76,14 @@ func (xc *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
}
|
||||
|
||||
// ConfigContainer A Config represents the xml configuration.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type ConfigContainer struct {
|
||||
data map[string]interface{}
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Bool(key string) (bool, error) {
|
||||
if v := c.data[key]; v != nil {
|
||||
return config.ParseBool(v)
|
||||
@ -88,6 +93,7 @@ func (c *ConfigContainer) Bool(key string) (bool, error) {
|
||||
|
||||
// DefaultBool return the bool value if has no error
|
||||
// otherwise return the defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
@ -97,12 +103,14 @@ func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Int(key string) (int, error) {
|
||||
return strconv.Atoi(c.data[key].(string))
|
||||
}
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
@ -112,12 +120,14 @@ func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Int64(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.data[key].(string), 10, 64)
|
||||
}
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
@ -128,12 +138,14 @@ func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Float(key string) (float64, error) {
|
||||
return strconv.ParseFloat(c.data[key].(string), 64)
|
||||
}
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
@ -143,6 +155,7 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) String(key string) string {
|
||||
if v, ok := c.data[key].(string); ok {
|
||||
return v
|
||||
@ -152,6 +165,7 @@ func (c *ConfigContainer) String(key string) string {
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -161,6 +175,7 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
}
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Strings(key string) []string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -171,6 +186,7 @@ func (c *ConfigContainer) Strings(key string) []string {
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||
v := c.Strings(key)
|
||||
if v == nil {
|
||||
@ -180,6 +196,7 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
|
||||
}
|
||||
|
||||
// GetSection returns map for the given section
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
if v, ok := c.data[section].(map[string]interface{}); ok {
|
||||
mapstr := make(map[string]string)
|
||||
@ -192,6 +209,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error)
|
||||
}
|
||||
|
||||
// SaveConfigFile save the config into file
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Write configuration file by filename.
|
||||
f, err := os.Create(filename)
|
||||
@ -208,6 +226,7 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
}
|
||||
|
||||
// Set writes a new value for key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Set(key, val string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
@ -216,6 +235,7 @@ func (c *ConfigContainer) Set(key, val string) error {
|
||||
}
|
||||
|
||||
// DIY returns the raw value by a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
if v, ok := c.data[key]; ok {
|
||||
return v, nil
|
||||
|
@ -45,9 +45,11 @@ import (
|
||||
)
|
||||
|
||||
// Config is a yaml config parser and implements Config interface.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type Config struct{}
|
||||
|
||||
// Parse returns a ConfigContainer with parsed yaml config map.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
|
||||
cnf, err := ReadYmlReader(filename)
|
||||
if err != nil {
|
||||
@ -60,6 +62,7 @@ func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
|
||||
}
|
||||
|
||||
// ParseData parse yaml data
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
cnf, err := parseYML(data)
|
||||
if err != nil {
|
||||
@ -73,6 +76,7 @@ func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
|
||||
// ReadYmlReader Read yaml file to map.
|
||||
// if json like, use json package, unless goyaml2 package.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
|
||||
buf, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
@ -117,12 +121,14 @@ func parseYML(buf []byte) (cnf map[string]interface{}, err error) {
|
||||
}
|
||||
|
||||
// ConfigContainer A Config represents the yaml configuration.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
type ConfigContainer struct {
|
||||
data map[string]interface{}
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Bool(key string) (bool, error) {
|
||||
v, err := c.getData(key)
|
||||
if err != nil {
|
||||
@ -133,6 +139,7 @@ func (c *ConfigContainer) Bool(key string) (bool, error) {
|
||||
|
||||
// DefaultBool return the bool value if has no error
|
||||
// otherwise return the defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
@ -142,6 +149,7 @@ func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Int(key string) (int, error) {
|
||||
if v, err := c.getData(key); err != nil {
|
||||
return 0, err
|
||||
@ -155,6 +163,7 @@ func (c *ConfigContainer) Int(key string) (int, error) {
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
@ -164,6 +173,7 @@ func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Int64(key string) (int64, error) {
|
||||
if v, err := c.getData(key); err != nil {
|
||||
return 0, err
|
||||
@ -175,6 +185,7 @@ func (c *ConfigContainer) Int64(key string) (int64, error) {
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
@ -184,6 +195,7 @@ func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Float(key string) (float64, error) {
|
||||
if v, err := c.getData(key); err != nil {
|
||||
return 0.0, err
|
||||
@ -199,6 +211,7 @@ func (c *ConfigContainer) Float(key string) (float64, error) {
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
@ -208,6 +221,7 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) String(key string) string {
|
||||
if v, err := c.getData(key); err == nil {
|
||||
if vv, ok := v.(string); ok {
|
||||
@ -219,6 +233,7 @@ func (c *ConfigContainer) String(key string) string {
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -228,6 +243,7 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
|
||||
}
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Strings(key string) []string {
|
||||
v := c.String(key)
|
||||
if v == "" {
|
||||
@ -238,6 +254,7 @@ func (c *ConfigContainer) Strings(key string) []string {
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||
v := c.Strings(key)
|
||||
if v == nil {
|
||||
@ -247,6 +264,7 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
|
||||
}
|
||||
|
||||
// GetSection returns map for the given section
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
|
||||
if v, ok := c.data[section]; ok {
|
||||
@ -256,6 +274,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error)
|
||||
}
|
||||
|
||||
// SaveConfigFile save the config into file
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Write configuration file by filename.
|
||||
f, err := os.Create(filename)
|
||||
@ -268,6 +287,7 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
}
|
||||
|
||||
// Set writes a new value for key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) Set(key, val string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
@ -276,6 +296,7 @@ func (c *ConfigContainer) Set(key, val string) error {
|
||||
}
|
||||
|
||||
// DIY returns the raw value by a given key.
|
||||
// Deprecated: using pkg/config, we will delete this in v2.1.0
|
||||
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
return c.getData(key)
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ini
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@ -26,8 +26,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -47,7 +45,7 @@ type IniConfig struct {
|
||||
}
|
||||
|
||||
// Parse creates a new Config and parses the file configuration from the named file.
|
||||
func (ini *IniConfig) Parse(name string) (config.Configer, error) {
|
||||
func (ini *IniConfig) Parse(name string) (Configer, error) {
|
||||
return ini.parseFile(name)
|
||||
}
|
||||
|
||||
@ -197,7 +195,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
|
||||
val = bytes.Trim(val, `"`)
|
||||
}
|
||||
|
||||
cfg.data[section][key] = config.ExpandValueEnv(string(val))
|
||||
cfg.data[section][key] = ExpandValueEnv(string(val))
|
||||
if comment.Len() > 0 {
|
||||
cfg.keyComment[section+"."+key] = comment.String()
|
||||
comment.Reset()
|
||||
@ -210,7 +208,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
|
||||
// ParseData parse ini the data
|
||||
// When include other.conf,other.conf is either absolute directory
|
||||
// or under beego in default temporary directory(/tmp/beego[-username]).
|
||||
func (ini *IniConfig) ParseData(data []byte) (config.Configer, error) {
|
||||
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
|
||||
dir := "beego"
|
||||
currentUser, err := user.Current()
|
||||
if err == nil {
|
||||
@ -235,7 +233,7 @@ type IniConfigContainer struct {
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
func (c *IniConfigContainer) Bool(key string) (bool, error) {
|
||||
return config.ParseBool(c.getdata(key))
|
||||
return ParseBool(c.getdata(key))
|
||||
}
|
||||
|
||||
// DefaultBool returns the boolean value for a given key.
|
||||
@ -502,5 +500,5 @@ func (c *IniConfigContainer) getdata(key string) string {
|
||||
}
|
||||
|
||||
func init() {
|
||||
config.Register("ini", &IniConfig{})
|
||||
Register("ini", &IniConfig{})
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ini
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -20,8 +20,6 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/pkg/config"
|
||||
)
|
||||
|
||||
func TestIni(t *testing.T) {
|
||||
@ -94,7 +92,7 @@ password = ${GOPATH}
|
||||
}
|
||||
f.Close()
|
||||
defer os.Remove("testini.conf")
|
||||
iniconf, err := config.NewConfig("ini", "testini.conf")
|
||||
iniconf, err := NewConfig("ini", "testini.conf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -167,7 +165,7 @@ httpport=8080
|
||||
name=mysql
|
||||
`
|
||||
)
|
||||
cfg, err := config.NewConfigData("ini", []byte(inicontext))
|
||||
cfg, err := NewConfigData("ini", []byte(inicontext))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user