add file name configuration from command line or environment for package gcfg

This commit is contained in:
John Guo
2021-01-07 01:17:03 +08:00
parent b12c909fd6
commit c6f1ae9426
7 changed files with 145 additions and 93 deletions

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -49,6 +49,11 @@ func New(file ...string) *Config {
name := DefaultConfigFile
if len(file) > 0 {
name = file[0]
} else {
// Custom default configuration file name from command line or environment.
if customFile := gcmd.GetWithEnv(fmt.Sprintf("%s.file", cmdEnvKey)).String(); customFile != "" {
name = customFile
}
}
c := &Config{
name: name,
@ -56,12 +61,12 @@ func New(file ...string) *Config {
jsons: gmap.NewStrAnyMap(true),
}
// Customized dir path from env/cmd.
if envPath := gcmd.GetWithEnv(fmt.Sprintf("%s.path", cmdEnvKey)).String(); envPath != "" {
if gfile.Exists(envPath) {
_ = c.SetPath(envPath)
if customPath := gcmd.GetWithEnv(fmt.Sprintf("%s.path", cmdEnvKey)).String(); customPath != "" {
if gfile.Exists(customPath) {
_ = c.SetPath(customPath)
} else {
if errorPrint() {
glog.Errorf("Configuration directory path does not exist: %s", envPath)
glog.Errorf("Configuration directory path does not exist: %s", customPath)
}
}
} else {

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -13,7 +13,7 @@ import (
const (
// Default group name for instance usage.
DefaultName = "default"
DefaultName = "config"
)
var (

View File

@ -12,7 +12,6 @@ import (
"os"
"testing"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/encoding/gjson"
@ -229,71 +228,6 @@ func Test_SetFileName(t *testing.T) {
})
}
func Test_Instance(t *testing.T) {
config := `
array = [1.0, 2.0, 3.0]
v1 = 1.0
v2 = "true"
v3 = "off"
v4 = "1.234"
[redis]
cache = "127.0.0.1:6379,1"
disk = "127.0.0.1:6379,0"
`
gtest.C(t, func(t *gtest.T) {
path := gcfg.DefaultConfigFile
err := gfile.PutContents(path, config)
t.Assert(err, nil)
defer func() {
t.Assert(gfile.Remove(path), nil)
}()
c := gcfg.Instance()
t.Assert(c.Get("v1"), 1)
t.AssertEQ(c.GetInt("v1"), 1)
t.AssertEQ(c.GetInt8("v1"), int8(1))
t.AssertEQ(c.GetInt16("v1"), int16(1))
t.AssertEQ(c.GetInt32("v1"), int32(1))
t.AssertEQ(c.GetInt64("v1"), int64(1))
t.AssertEQ(c.GetUint("v1"), uint(1))
t.AssertEQ(c.GetUint8("v1"), uint8(1))
t.AssertEQ(c.GetUint16("v1"), uint16(1))
t.AssertEQ(c.GetUint32("v1"), uint32(1))
t.AssertEQ(c.GetUint64("v1"), uint64(1))
t.AssertEQ(c.GetVar("v1").String(), "1")
t.AssertEQ(c.GetVar("v1").Bool(), true)
t.AssertEQ(c.GetVar("v2").String(), "true")
t.AssertEQ(c.GetVar("v2").Bool(), true)
t.AssertEQ(c.GetString("v1"), "1")
t.AssertEQ(c.GetFloat32("v4"), float32(1.234))
t.AssertEQ(c.GetFloat64("v4"), float64(1.234))
t.AssertEQ(c.GetString("v2"), "true")
t.AssertEQ(c.GetBool("v2"), true)
t.AssertEQ(c.GetBool("v3"), false)
t.AssertEQ(c.Contains("v1"), true)
t.AssertEQ(c.Contains("v2"), true)
t.AssertEQ(c.Contains("v3"), true)
t.AssertEQ(c.Contains("v4"), true)
t.AssertEQ(c.Contains("v5"), false)
t.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3})
t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3})
t.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
})
}
func TestCfg_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
os.Setenv("GF_GCFG_PATH", "config")
@ -446,26 +380,6 @@ func TestCfg_Get(t *testing.T) {
})
}
func TestCfg_Instance(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gcfg.Instance("gf") != nil, true)
})
gtest.C(t, func(t *gtest.T) {
pwd := gfile.Pwd()
gfile.Chdir(gdebug.TestDataPath())
defer gfile.Chdir(pwd)
t.Assert(gcfg.Instance("c1") != nil, true)
t.Assert(gcfg.Instance("c1").Get("my-config"), "1")
t.Assert(gcfg.Instance("folder1/c1").Get("my-config"), "2")
})
gtest.C(t, func(t *gtest.T) {
pwd := gfile.Pwd()
gfile.Chdir(gdebug.TestDataPath("folder1"))
defer gfile.Chdir(pwd)
t.Assert(gcfg.Instance("c2").Get("my-config"), 2)
})
}
func TestCfg_Config(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gcfg.SetContent("gf", "config.yml")

View File

@ -0,0 +1,127 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gcfg
import (
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/os/genv"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
"testing"
)
func Test_Instance_Basic(t *testing.T) {
config := `
array = [1.0, 2.0, 3.0]
v1 = 1.0
v2 = "true"
v3 = "off"
v4 = "1.234"
[redis]
cache = "127.0.0.1:6379,1"
disk = "127.0.0.1:6379,0"
`
gtest.C(t, func(t *gtest.T) {
path := DefaultConfigFile
err := gfile.PutContents(path, config)
t.Assert(err, nil)
defer func() {
t.Assert(gfile.Remove(path), nil)
}()
c := Instance()
t.Assert(c.Get("v1"), 1)
t.AssertEQ(c.GetInt("v1"), 1)
t.AssertEQ(c.GetInt8("v1"), int8(1))
t.AssertEQ(c.GetInt16("v1"), int16(1))
t.AssertEQ(c.GetInt32("v1"), int32(1))
t.AssertEQ(c.GetInt64("v1"), int64(1))
t.AssertEQ(c.GetUint("v1"), uint(1))
t.AssertEQ(c.GetUint8("v1"), uint8(1))
t.AssertEQ(c.GetUint16("v1"), uint16(1))
t.AssertEQ(c.GetUint32("v1"), uint32(1))
t.AssertEQ(c.GetUint64("v1"), uint64(1))
t.AssertEQ(c.GetVar("v1").String(), "1")
t.AssertEQ(c.GetVar("v1").Bool(), true)
t.AssertEQ(c.GetVar("v2").String(), "true")
t.AssertEQ(c.GetVar("v2").Bool(), true)
t.AssertEQ(c.GetString("v1"), "1")
t.AssertEQ(c.GetFloat32("v4"), float32(1.234))
t.AssertEQ(c.GetFloat64("v4"), float64(1.234))
t.AssertEQ(c.GetString("v2"), "true")
t.AssertEQ(c.GetBool("v2"), true)
t.AssertEQ(c.GetBool("v3"), false)
t.AssertEQ(c.Contains("v1"), true)
t.AssertEQ(c.Contains("v2"), true)
t.AssertEQ(c.Contains("v3"), true)
t.AssertEQ(c.Contains("v4"), true)
t.AssertEQ(c.Contains("v5"), false)
t.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3})
t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3})
t.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
})
}
func Test_Instance_AutoLocateConfigFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(Instance("gf") != nil, true)
})
// Automatically locate the configuration file with supported file extensions.
gtest.C(t, func(t *gtest.T) {
pwd := gfile.Pwd()
gfile.Chdir(gdebug.TestDataPath())
defer gfile.Chdir(pwd)
t.Assert(Instance("c1") != nil, true)
t.Assert(Instance("c1").Get("my-config"), "1")
t.Assert(Instance("folder1/c1").Get("my-config"), "2")
})
// Automatically locate the configuration file with supported file extensions.
gtest.C(t, func(t *gtest.T) {
pwd := gfile.Pwd()
gfile.Chdir(gdebug.TestDataPath("folder1"))
defer gfile.Chdir(pwd)
t.Assert(Instance("c2").Get("my-config"), 2)
})
}
func Test_Instance_EnvPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envpath"))
defer genv.Set("GF_GCFG_PATH", "")
t.Assert(Instance("c3") != nil, true)
t.Assert(Instance("c3").Get("my-config"), "3")
t.Assert(Instance("c4").Get("my-config"), "4")
instances = gmap.NewStrAnyMap(true)
})
}
func Test_Instance_EnvFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envfile"))
defer genv.Set("GF_GCFG_PATH", "")
genv.Set("GF_GCFG_FILE", "c6.json")
defer genv.Set("GF_GCFG_FILE", "")
t.Assert(Instance().Get("my-config"), "6")
instances = gmap.NewStrAnyMap(true)
})
}

2
os/gcfg/testdata/envfile/c6.json vendored Normal file
View File

@ -0,0 +1,2 @@
{"my-config": 6}

2
os/gcfg/testdata/envpath/c3.toml vendored Normal file
View File

@ -0,0 +1,2 @@
my-config = "3"

2
os/gcfg/testdata/envpath/c4.json vendored Normal file
View File

@ -0,0 +1,2 @@
{"my-config": 4}