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

Introduce environment variable for custom config dir #291

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0 h1:Rw8kxzWo1mr6FSaYXjQELRe88y2KdfynXdnK72rdjtA=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
Expand Down
13 changes: 11 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C
v.SetConfigFile(cfgFile)
} else {
v.SetConfigName("ct")
for _, searchLocation := range configSearchLocations {
v.AddConfigPath(searchLocation)
if cfgFile, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the use case here, but just checking on implementation. We have --config flag now, not --config-dir. Why not keep it consistent with CT_CONFIG path to file rather than dir?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probalby should have explained that better. CT_CONFIG_DIR overrides the default locations where config files are searched. This is not only about the config file for ct but also those for Yamale and yamllint. This will be required when we install ct into a cache location in #58, so config files are still found.

See https://github.com/helm/chart-testing-action/pull/58/files#diff-c141db7e865976e07d678c49af2c6288b4c97d4a0af34ac25905bd46dde8301aR74

v.AddConfigPath(cfgFile)
} else {
for _, searchLocation := range configSearchLocations {
v.AddConfigPath(searchLocation)
}
}
}

Expand Down Expand Up @@ -182,11 +186,16 @@ func printCfg(cfg *Configuration) {
}

func findConfigFile(fileName string) (string, error) {
if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
return filepath.Join(dir, fileName), nil
}

for _, location := range configSearchLocations {
filePath := filepath.Join(location, fileName)
if util.FileExists(filePath) {
return filePath, nil
}
}

return "", errors.New(fmt.Sprintf("Config file not found: %s", fileName))
}
56 changes: 56 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -55,3 +58,56 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
require.Equal(t, "release", cfg.ReleaseLabel)
require.Equal(t, true, cfg.ExcludeDeprecated)
}

func Test_findConfigFile(t *testing.T) {
tests := []struct {
name string
envVar string
defaultDir string
want string
wantErr bool
}{
{
name: "without env var",
defaultDir: filepath.Join("testdata", "default"),
want: filepath.Join("testdata", "default", "test.yaml"),
},
{
name: "with env var",
envVar: filepath.Join("testdata", "env"),
want: filepath.Join("testdata", "env", "test.yaml"),
},
{
name: "with env var and default location",
envVar: filepath.Join("testdata", "env"),
defaultDir: filepath.Join("testdata", "default"),
want: filepath.Join("testdata", "env", "test.yaml"),
},
{
name: "not found",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envVar != "" {
err := os.Setenv("CT_CONFIG_DIR", tt.envVar)
require.NoError(t, err)

t.Cleanup(func() {
err := os.Unsetenv("CT_CONFIG_DIR")
require.NoError(t, err)
})
}
configSearchLocations = []string{tt.defaultDir}

got, err := findConfigFile("test.yaml")
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
Empty file.
Empty file.