Skip to content

Commit

Permalink
Allow overriding the uaa-cli config directory with an environment var…
Browse files Browse the repository at this point in the history
…iable

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.
  • Loading branch information
AP-Hunt committed Aug 30, 2019
1 parent 639e9e2 commit 199a2e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

Expand Down
40 changes: 38 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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`))
})
})
})

0 comments on commit 199a2e3

Please sign in to comment.