From eab40a5974206421705faed08e538c958806acbf Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Thu, 10 Jan 2019 15:49:06 +0000 Subject: [PATCH] cli/config: Add a helper to resolve a file within the config dir Signed-off-by: Ian Campbell --- cli/config/config.go | 5 +++++ cli/config/config_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) 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) +}