Skip to content

Commit

Permalink
feat(config): support multiple config file types
Browse files Browse the repository at this point in the history
Signed-off-by: Zsolt Rappi <[email protected]>
  • Loading branch information
rappizs committed Aug 15, 2023
1 parent d57ac75 commit c67555b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/trivago/tgo v1.0.7
github.com/uwu-tools/go-jira/v2 v2.0.0-20230801175343-52f822b5cb80
github.com/uwu-tools/magex v0.10.0
Expand All @@ -37,6 +38,7 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
Expand All @@ -62,6 +64,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pierrec/lz4/v4 v4.1.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM=
Expand Down
15 changes: 14 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func newViper(appName, cfgFile string) *viper.Viper {
if cfgFile != "" {
v.SetConfigFile(cfgFile)
}
v.SetConfigType("json")
v.SetConfigType(getConfigTypeFromName(cfgFile))

Check warning on line 340 in internal/config/config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/config.go#L340

Added line #L340 was not covered by tests

if err := v.ReadInConfig(); err == nil {
log.WithField("file", v.ConfigFileUsed()).Infof("config file loaded")
Expand Down Expand Up @@ -539,3 +539,16 @@ var (
func errCustomFieldIDNotFound(field string) error {
return fmt.Errorf("could not find ID custom field '%s'; check that it is named correctly", field) //nolint:goerr113
}

// getConfigTypeFromName extracts the extension from the passed in filename,
// returns `json` if it's empty

Check failure on line 544 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / verify

Comment should end in a period (godot)
func getConfigTypeFromName(filename string) string {
if filename == "" {
return "json"
}

// it's enough to rely on the type from the filename because
// viper will parse and validate the the file's content
parts := strings.Split(filename, ".")
return parts[len(parts)-1]
}
46 changes: 46 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetConfigTypeFromName(t *testing.T) {
tests := []*struct {
name, filename, expected string
}{
{
name: "empty name",
filename: "",
expected: "json",
},
{
name: "json",
filename: "config.json",
expected: "json",
},
{
name: "toml",
filename: "config.toml",
expected: "toml",
},
{
name: "yaml",
filename: "config.yaml",
expected: "yaml",
},
{
name: "any file type",
filename: "config.xyz",
expected: "xyz",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("Assert config type with %s", test.name), func(t *testing.T) {
assert.Equal(t, test.expected, getConfigTypeFromName(test.filename))
})
}
}

0 comments on commit c67555b

Please sign in to comment.