Skip to content

Commit

Permalink
OPTIM: Changed to pass the filename to the LoadConfig method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwrosewell committed Sep 5, 2021
1 parent b970f3d commit 742c487
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
// match the interface field names.
//
// paths is an array of directories to look for configuration files.
// name is the name of the configuration file without an extension. The source
// file must have the extension json.
// fileName is the name of the configuration file. The extension must be a type
// supported by the viper package (JSON, TOML, YAML, HCL, envfile and Java
// properties config).
// i is an instance of an interface to be populated from the files and
// environment variables identified.
//
Expand All @@ -40,13 +41,12 @@ import (
// SERVICE_PATH.
// The interface that is being used for the configuration needs to be consulted
// because viper passes an upper case version of the field name into the method.
func LoadConfig(paths []string, name string, i interface{}) error {
func LoadConfig(paths []string, fileName string, i interface{}) error {
v := viper.New()
for _, path := range paths {
v.AddConfigPath(path)
}
v.SetConfigName(name)
v.SetConfigType("json")
v.SetConfigFile(fileName)
v.AutomaticEnv()
v.AllowEmptyEnv(false)
for _, n := range getFields(reflect.TypeOf(i).Elem()) {
Expand Down
16 changes: 8 additions & 8 deletions configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestCamelConvertMultiple(t *testing.T) {

func TestLocalConfigurationSettings(t *testing.T) {
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.local", &c)
err := LoadConfig([]string{"."}, "appsettings.test.local.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -74,7 +74,7 @@ func TestLocalConfigurationEnvironment(t *testing.T) {
e := "TEST ENV LOCAL FILE"
t.Setenv("LOCAL_FILE", e)
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.local", &c)
err := LoadConfig([]string{"."}, "appsettings.test.local.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -95,7 +95,7 @@ func TestAwsConfigurationSettingsFalse(t *testing.T) {
func testAwsConfigurationSettings(t *testing.T, expected bool) {
e := fmt.Sprintf("%t", expected)
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.aws."+e, &c)
err := LoadConfig([]string{"."}, "appsettings.test.aws."+e+".json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -117,7 +117,7 @@ func testAwsConfigurationEnvironment(t *testing.T, expected bool) {
e := fmt.Sprintf("%t", expected)
t.Setenv("AWS_ENABLED", e)
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.none", &c)
err := LoadConfig([]string{"."}, "appsettings.test.none.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -129,7 +129,7 @@ func testAwsConfigurationEnvironment(t *testing.T, expected bool) {

func TestGcpConfigurationSettings(t *testing.T) {
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.gcp", &c)
err := LoadConfig([]string{"."}, "appsettings.test.gcp.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -143,7 +143,7 @@ func TestGcpConfigurationEnvironment(t *testing.T) {
e := "PROJECT NAME"
t.Setenv("GCP_PROJECT", e)
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.none", &c)
err := LoadConfig([]string{"."}, "appsettings.test.none.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -155,7 +155,7 @@ func TestGcpConfigurationEnvironment(t *testing.T) {

func TestAzureConfigurationSettings(t *testing.T) {
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.azure", &c)
err := LoadConfig([]string{"."}, "appsettings.test.azure.json", &c)
if err != nil {
t.Fatal(err)
}
Expand All @@ -171,7 +171,7 @@ func TestAzureConfigurationEnvironment(t *testing.T) {
t.Setenv("AZURE_STORAGE_ACCOUNT", ea)
t.Setenv("AZURE_STORAGE_ACCESS_KEY", ek)
c := Override{}
err := LoadConfig([]string{"."}, "appsettings.test.none", &c)
err := LoadConfig([]string{"."}, "appsettings.test.none.json", &c)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 742c487

Please sign in to comment.