diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index b994a31cf..1256a6fa3 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -40,7 +40,7 @@ type Config struct { } var ( - supportedFileTypes = []string{"toml", "yaml", "json", "ini", "xml"} + supportedFileTypes = []string{"toml", "yaml", "yml", "json", "ini", "xml"} resourceTryFiles = []string{"", "/", "config/", "config", "/config", "/config/"} ) diff --git a/os/gcfg/gcfg_instance.go b/os/gcfg/gcfg_instance.go index 84999034f..9eeb07905 100644 --- a/os/gcfg/gcfg_instance.go +++ b/os/gcfg/gcfg_instance.go @@ -32,10 +32,15 @@ func Instance(name ...string) *Config { } return instances.GetOrSetFuncLock(key, func() interface{} { c := New() - for _, fileType := range supportedFileTypes { - if file := fmt.Sprintf(`%s.%s`, key, fileType); c.Available(file) { - c.SetFileName(file) - break + // If it's not using default configuration or its configuration file is not available, + // it searches the possible configuration file according to the name and all supported + // file types. + if key != DefaultName || !c.Available() { + for _, fileType := range supportedFileTypes { + if file := fmt.Sprintf(`%s.%s`, key, fileType); c.Available(file) { + c.SetFileName(file) + break + } } } return c diff --git a/os/gcfg/gcfg_z_unit_instance_test.go b/os/gcfg/gcfg_z_unit_instance_test.go index 9043bf434..2cc5e3ab1 100644 --- a/os/gcfg/gcfg_z_unit_instance_test.go +++ b/os/gcfg/gcfg_z_unit_instance_test.go @@ -78,7 +78,6 @@ v4 = "1.234" "cache": "127.0.0.1:6379,1", }) t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path) - }) } @@ -89,7 +88,7 @@ func Test_Instance_AutoLocateConfigFile(t *testing.T) { // Automatically locate the configuration file with supported file extensions. gtest.C(t, func(t *gtest.T) { pwd := gfile.Pwd() - gfile.Chdir(gdebug.TestDataPath()) + t.AssertNil(gfile.Chdir(gdebug.TestDataPath())) defer gfile.Chdir(pwd) t.Assert(Instance("c1") != nil, true) t.Assert(Instance("c1").Get("my-config"), "1") @@ -98,10 +97,23 @@ func Test_Instance_AutoLocateConfigFile(t *testing.T) { // Automatically locate the configuration file with supported file extensions. gtest.C(t, func(t *gtest.T) { pwd := gfile.Pwd() - gfile.Chdir(gdebug.TestDataPath("folder1")) + t.AssertNil(gfile.Chdir(gdebug.TestDataPath("folder1"))) defer gfile.Chdir(pwd) t.Assert(Instance("c2").Get("my-config"), 2) }) + // Default configuration file. + gtest.C(t, func(t *gtest.T) { + instances.Clear() + pwd := gfile.Pwd() + t.AssertNil(gfile.Chdir(gdebug.TestDataPath("default"))) + defer gfile.Chdir(pwd) + t.Assert(Instance().Get("my-config"), 1) + + instances.Clear() + t.AssertNil(genv.Set("GF_GCFG_FILE", "config.json")) + defer genv.Set("GF_GCFG_FILE", "") + t.Assert(Instance().Get("my-config"), 2) + }) } func Test_Instance_EnvPath(t *testing.T) { diff --git a/os/gcfg/testdata/default/config.json b/os/gcfg/testdata/default/config.json new file mode 100644 index 000000000..ea5047901 --- /dev/null +++ b/os/gcfg/testdata/default/config.json @@ -0,0 +1 @@ +{"my-config": 2} \ No newline at end of file diff --git a/os/gcfg/testdata/default/config.toml b/os/gcfg/testdata/default/config.toml new file mode 100644 index 000000000..ec4ea12a8 --- /dev/null +++ b/os/gcfg/testdata/default/config.toml @@ -0,0 +1 @@ +my-config = "1" \ No newline at end of file