From 199a2e37e21c30a51da6d844a32fd3402d41c8fb Mon Sep 17 00:00:00 2001 From: Andy Hunt Date: Fri, 30 Aug 2019 19:54:36 +0100 Subject: [PATCH] Allow overriding the uaa-cli config directory with an environment variable By default, uaa-cli config file will be stored in `$HOME/.uaa/config.json`, but if the environment variable `UAA_HOME` is set, then the config file will be stored there instead. This allows for some flexibility in how users of the tool use it, in the same ways that cf-cli does. --- config/config.go | 5 +++++ config/config_test.go | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 6ed5eeb..2913330 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ const ( IMPLICIT = GrantType("implicit") PASSWORD = GrantType("password") CLIENT_CREDENTIALS = GrantType("client_credentials") + UAA_HOME_ENV_VAR = "UAA_HOME" ) type Config struct { @@ -110,6 +111,10 @@ func (uc UaaContext) name() string { } func ConfigDir() string { + if path, found := os.LookupEnv(UAA_HOME_ENV_VAR); found { + return path + } + return path.Join(userHomeDir(), ".uaa") } diff --git a/config/config_test.go b/config/config_test.go index cbebf56..f54ddc6 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,6 +6,8 @@ import ( "code.cloudfoundry.org/uaa-cli/config" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "io/ioutil" + "os" ) var _ = Describe("Config", func() { @@ -52,7 +54,41 @@ var _ = Describe("Config", func() { Expect(cfg2.GetActiveContext().Token.AccessToken).To(Equal("foo-token")) }) - It("places the config file in .uaa in the home directory", func() { - Expect(config.ConfigPath()).To(HaveSuffix(`/.uaa/config.json`)) + Context("when UAA_HOME env var is not set", func() { + It("places the config file in .uaa in the home directory", func() { + homeDir := os.Getenv("HOME") + Expect(config.ConfigPath()).To(HavePrefix(homeDir)) + Expect(config.ConfigPath()).To(HaveSuffix(`/.uaa/config.json`)) + }) + }) + + Context("when UAA_HOME env var is set", func() { + var uaaHome string + + BeforeEach(func() { + var err error + uaaHome, err = ioutil.TempDir(os.TempDir(), "uaa-home") + Expect(err).ToNot(HaveOccurred()) + + err = os.Setenv("UAA_HOME", uaaHome) + Expect(err).ToNot(HaveOccurred()) + }) + + AfterEach(func() { + if uaaHome != "" { + if _, err := os.Stat(uaaHome); !os.IsNotExist(err) { + err := os.RemoveAll(uaaHome) + Expect(err).NotTo(HaveOccurred()) + } + } + + err := os.Unsetenv("UAA_HOME") + Expect(err).ToNot(HaveOccurred()) + }) + + It("places the config file in the directory pointed to by UAA_HOME", func() { + Expect(config.ConfigPath()).To(HavePrefix(uaaHome)) + Expect(config.ConfigPath()).To(HaveSuffix(`config.json`)) + }) }) })