diff --git a/cli/config/config.go b/cli/config/config.go index 64f8d3b49c..f773abee40 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -46,6 +46,11 @@ func SetDir(dir string) { configDir = dir } +// Path returns the path to a file relative to the config dir +func Path(p ...string) string { + return filepath.Join(append([]string{Dir()}, p...)...) +} + // LegacyLoadFromReader is a convenience function that creates a ConfigFile object from // a non-nested reader func LegacyLoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) { diff --git a/cli/config/config_test.go b/cli/config/config_test.go index 11c8dd6de5..85288291b4 100644 --- a/cli/config/config_test.go +++ b/cli/config/config_test.go @@ -548,3 +548,17 @@ func TestLoadDefaultConfigFile(t *testing.T) { assert.Check(t, is.DeepEqual(expected, configFile)) } + +func TestConfigPath(t *testing.T) { + oldDir := Dir() + + SetDir("dummy1") + f1 := Path("a", "b") + assert.Equal(t, f1, filepath.Join("dummy1", "a", "b")) + + SetDir("dummy2") + f2 := Path("c", "d") + assert.Equal(t, f2, filepath.Join("dummy2", "c", "d")) + + SetDir(oldDir) +}