Skip to content

Commit 199a2e3

Browse files
committed
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.
1 parent 639e9e2 commit 199a2e3

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

config/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
IMPLICIT = GrantType("implicit")
1717
PASSWORD = GrantType("password")
1818
CLIENT_CREDENTIALS = GrantType("client_credentials")
19+
UAA_HOME_ENV_VAR = "UAA_HOME"
1920
)
2021

2122
type Config struct {
@@ -110,6 +111,10 @@ func (uc UaaContext) name() string {
110111
}
111112

112113
func ConfigDir() string {
114+
if path, found := os.LookupEnv(UAA_HOME_ENV_VAR); found {
115+
return path
116+
}
117+
113118
return path.Join(userHomeDir(), ".uaa")
114119
}
115120

config/config_test.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"code.cloudfoundry.org/uaa-cli/config"
77
. "github.com/onsi/ginkgo"
88
. "github.com/onsi/gomega"
9+
"io/ioutil"
10+
"os"
911
)
1012

1113
var _ = Describe("Config", func() {
@@ -52,7 +54,41 @@ var _ = Describe("Config", func() {
5254
Expect(cfg2.GetActiveContext().Token.AccessToken).To(Equal("foo-token"))
5355
})
5456

55-
It("places the config file in .uaa in the home directory", func() {
56-
Expect(config.ConfigPath()).To(HaveSuffix(`/.uaa/config.json`))
57+
Context("when UAA_HOME env var is not set", func() {
58+
It("places the config file in .uaa in the home directory", func() {
59+
homeDir := os.Getenv("HOME")
60+
Expect(config.ConfigPath()).To(HavePrefix(homeDir))
61+
Expect(config.ConfigPath()).To(HaveSuffix(`/.uaa/config.json`))
62+
})
63+
})
64+
65+
Context("when UAA_HOME env var is set", func() {
66+
var uaaHome string
67+
68+
BeforeEach(func() {
69+
var err error
70+
uaaHome, err = ioutil.TempDir(os.TempDir(), "uaa-home")
71+
Expect(err).ToNot(HaveOccurred())
72+
73+
err = os.Setenv("UAA_HOME", uaaHome)
74+
Expect(err).ToNot(HaveOccurred())
75+
})
76+
77+
AfterEach(func() {
78+
if uaaHome != "" {
79+
if _, err := os.Stat(uaaHome); !os.IsNotExist(err) {
80+
err := os.RemoveAll(uaaHome)
81+
Expect(err).NotTo(HaveOccurred())
82+
}
83+
}
84+
85+
err := os.Unsetenv("UAA_HOME")
86+
Expect(err).ToNot(HaveOccurred())
87+
})
88+
89+
It("places the config file in the directory pointed to by UAA_HOME", func() {
90+
Expect(config.ConfigPath()).To(HavePrefix(uaaHome))
91+
Expect(config.ConfigPath()).To(HaveSuffix(`config.json`))
92+
})
5793
})
5894
})

0 commit comments

Comments
 (0)