Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit/cli): fix loading of .yml config files #20522

Merged
merged 4 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kit/cli/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewCommand(v *viper.Viper, p *Program) *cobra.Command {

if configPath := v.GetString("CONFIG_PATH"); configPath != "" {
switch path.Ext(configPath) {
case ".json", ".toml", ".yaml", "yml":
case ".json", ".toml", ".yaml", ".yml":
danxmoran marked this conversation as resolved.
Show resolved Hide resolved
v.SetConfigFile(configPath)
case "":
v.AddConfigPath(configPath)
Expand Down
165 changes: 107 additions & 58 deletions kit/cli/viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"testing"
"time"

"github.com/BurntSushi/toml"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
)

type customFlag bool
Expand Down Expand Up @@ -138,16 +140,14 @@ func ExampleNewCommand() {
}

func Test_NewProgram(t *testing.T) {
testFilePath, cleanup := newConfigFile(t, map[string]string{
config := map[string]string{
// config values should be same as flags
"foo": "bar",
"shoe-fly": "yadon",
"number": "2147483647",
"long-number": "9223372036854775807",
"log-level": "debug",
})
defer cleanup()
defer setEnvVar("TEST_CONFIG_PATH", testFilePath)()
}

tests := []struct {
name string
Expand Down Expand Up @@ -178,56 +178,66 @@ func Test_NewProgram(t *testing.T) {
}

for _, tt := range tests {
fn := func(t *testing.T) {
if tt.envVarVal != "" {
defer setEnvVar("TEST_FOO", tt.envVarVal)()
}
for _, writer := range configWriters {
fn := func(t *testing.T) {
testDir, err := ioutil.TempDir("", "")
require.NoError(t, err)

var testVar string
var testFly string
var testNumber int32
var testLongNumber int64
var logLevel zapcore.Level
program := &Program{
Name: "test",
Opts: []Opt{
{
DestP: &testVar,
Flag: "foo",
Required: true,
},
{
DestP: &testFly,
Flag: "shoe-fly",
},
{
DestP: &testNumber,
Flag: "number",
},
{
DestP: &testLongNumber,
Flag: "long-number",
},
{
DestP: &logLevel,
Flag: "log-level",
confFile, err := writer.writeFn(testDir, config)
require.NoError(t, err)

defer setEnvVar("TEST_CONFIG_PATH", confFile)()

if tt.envVarVal != "" {
defer setEnvVar("TEST_FOO", tt.envVarVal)()
}

var testVar string
var testFly string
var testNumber int32
var testLongNumber int64
var logLevel zapcore.Level
program := &Program{
Name: "test",
Opts: []Opt{
{
DestP: &testVar,
Flag: "foo",
Required: true,
},
{
DestP: &testFly,
Flag: "shoe-fly",
},
{
DestP: &testNumber,
Flag: "number",
},
{
DestP: &testLongNumber,
Flag: "long-number",
},
{
DestP: &logLevel,
Flag: "log-level",
},
},
},
Run: func() error { return nil },
}
Run: func() error { return nil },
}

cmd := NewCommand(viper.New(), program)
cmd.SetArgs(append([]string{}, tt.args...))
require.NoError(t, cmd.Execute())
cmd := NewCommand(viper.New(), program)
cmd.SetArgs(append([]string{}, tt.args...))
require.NoError(t, cmd.Execute())

require.Equal(t, tt.expected, testVar)
assert.Equal(t, "yadon", testFly)
assert.Equal(t, int32(math.MaxInt32), testNumber)
assert.Equal(t, int64(math.MaxInt64), testLongNumber)
assert.Equal(t, zapcore.DebugLevel, logLevel)
}
require.Equal(t, tt.expected, testVar)
assert.Equal(t, "yadon", testFly)
assert.Equal(t, int32(math.MaxInt32), testNumber)
assert.Equal(t, int64(math.MaxInt64), testLongNumber)
assert.Equal(t, zapcore.DebugLevel, logLevel)
}

t.Run(tt.name, fn)
t.Run(fmt.Sprintf("%s_%s", tt.name, writer.ext), fn)
}
}
}

Expand All @@ -239,20 +249,59 @@ func setEnvVar(key, val string) func() {
}
}

func newConfigFile(t *testing.T, config interface{}) (string, func()) {
t.Helper()
type configWriter func(dir string, config interface{}) (string, error)
type labeledWriter struct {
ext string
writeFn configWriter
}

testDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
var configWriters = []labeledWriter{
{ext: "json", writeFn: writeJsonConfig},
{ext: "toml", writeFn: writeTomlConfig},
{ext: "yml", writeFn: yamlConfigWriter(true)},
{ext: "yaml", writeFn: yamlConfigWriter(false)},
}

func writeJsonConfig(dir string, config interface{}) (string, error) {
b, err := json.Marshal(config)
require.NoError(t, err)
if err != nil {
return "", err
}
confFile := path.Join(dir, "config.json")
if err := ioutil.WriteFile(confFile, b, os.ModePerm); err != nil {
return "", err
}
return confFile, nil
}

testFile := path.Join(testDir, "config.json")
require.NoError(t, ioutil.WriteFile(testFile, b, os.ModePerm))
func writeTomlConfig(dir string, config interface{}) (string, error) {
confFile := path.Join(dir, "config.toml")
w, err := os.OpenFile(confFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
if err != nil {
return "", err
}
if err := toml.NewEncoder(w).Encode(config); err != nil {
return "", err
}
return confFile, nil
}

func yamlConfigWriter(shortExt bool) configWriter {
fileName := "config.yaml"
if shortExt {
fileName = "config.yml"
}

return testFile, func() {
os.RemoveAll(testDir)
return func(dir string, config interface{}) (string, error) {
confFile := path.Join(dir, fileName)
w, err := os.OpenFile(confFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
if err != nil {
return "", err
}
if err := yaml.NewEncoder(w).Encode(config); err != nil {
return "", err
}
return confFile, nil
}
}

Expand Down