From c6f1ae9426fe909a3547e58493eab73b6a3e468e Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 7 Jan 2021 01:17:03 +0800 Subject: [PATCH] add file name configuration from command line or environment for package gcfg --- os/gcfg/gcfg.go | 15 ++- os/gcfg/gcfg_instance.go | 4 +- ...unit_test.go => gcfg_z_unit_basic_test.go} | 86 ------------ os/gcfg/gcfg_z_unit_instance_test.go | 127 ++++++++++++++++++ os/gcfg/testdata/envfile/c6.json | 2 + os/gcfg/testdata/envpath/c3.toml | 2 + os/gcfg/testdata/envpath/c4.json | 2 + 7 files changed, 145 insertions(+), 93 deletions(-) rename os/gcfg/{gcfg_z_unit_test.go => gcfg_z_unit_basic_test.go} (81%) create mode 100644 os/gcfg/gcfg_z_unit_instance_test.go create mode 100644 os/gcfg/testdata/envfile/c6.json create mode 100644 os/gcfg/testdata/envpath/c3.toml create mode 100644 os/gcfg/testdata/envpath/c4.json diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index f0c5e0249..fd89a38cf 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -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 { diff --git a/os/gcfg/gcfg_instance.go b/os/gcfg/gcfg_instance.go index 4f3669374..84999034f 100644 --- a/os/gcfg/gcfg_instance.go +++ b/os/gcfg/gcfg_instance.go @@ -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 ( diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_basic_test.go similarity index 81% rename from os/gcfg/gcfg_z_unit_test.go rename to os/gcfg/gcfg_z_unit_basic_test.go index 3fd04683a..7677f4b47 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_basic_test.go @@ -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") diff --git a/os/gcfg/gcfg_z_unit_instance_test.go b/os/gcfg/gcfg_z_unit_instance_test.go new file mode 100644 index 000000000..9043bf434 --- /dev/null +++ b/os/gcfg/gcfg_z_unit_instance_test.go @@ -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) + }) +} diff --git a/os/gcfg/testdata/envfile/c6.json b/os/gcfg/testdata/envfile/c6.json new file mode 100644 index 000000000..ac49cc62e --- /dev/null +++ b/os/gcfg/testdata/envfile/c6.json @@ -0,0 +1,2 @@ + +{"my-config": 6} \ No newline at end of file diff --git a/os/gcfg/testdata/envpath/c3.toml b/os/gcfg/testdata/envpath/c3.toml new file mode 100644 index 000000000..13fcebc0f --- /dev/null +++ b/os/gcfg/testdata/envpath/c3.toml @@ -0,0 +1,2 @@ + +my-config = "3" \ No newline at end of file diff --git a/os/gcfg/testdata/envpath/c4.json b/os/gcfg/testdata/envpath/c4.json new file mode 100644 index 000000000..9f8a930de --- /dev/null +++ b/os/gcfg/testdata/envpath/c4.json @@ -0,0 +1,2 @@ + +{"my-config": 4} \ No newline at end of file