Skip to content

Commit

Permalink
Always use APP_ROOT and add AppRoot helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan McAulay committed Feb 1, 2017
1 parent d5b9794 commit 71f9f75
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 16 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const (
)

const (
_appRoot = "APP_ROOT"
_environment = "_ENVIRONMENT"
_datacenter = "_DATACENTER"
_configDir = "_CONFIG_DIR"
_root = "_ROOT"
_configRoot = "./config"
_baseFile = "base"
_secretsFile = "secrets"
Expand All @@ -59,6 +59,21 @@ var (
_devEnv = "development"
)

// AppRoot returns the root directory of your application. UberFx developers
// can edit this via the APP_ROOT environment variable. If the environment
// variable is not set then it will fallback to the current working directory.
// This is often used for resolving relative paths in your service.
func AppRoot() string {
if appRoot := os.Getenv(_appRoot); appRoot != "" {
return appRoot
}
if cwd, err := os.Getwd(); err != nil {
panic(fmt.Sprintf("Unable to get the current working directory: %s", err.Error()))
} else {
return cwd
}
}

func getConfigFiles() []string {
env := Environment()
dc := os.Getenv(EnvironmentPrefix() + _datacenter)
Expand Down
8 changes: 1 addition & 7 deletions config/file_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ func NewRelativeResolver(paths ...string) FileResolver {

copy(pathList, paths)

if appRoot := os.Getenv(EnvironmentPrefix() + _root); appRoot != "" {
// add the app root
pathList = append(pathList, appRoot)
} else if cwd, err := os.Getwd(); err == nil {
// add the current cwd
pathList = append(pathList, cwd)
}
pathList = append(pathList, AppRoot())

// add the exe dir
pathList = append(pathList, path.Dir(os.Args[0]))
Expand Down
2 changes: 1 addition & 1 deletion config/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestAppRoot(t *testing.T) {
cwd, err := os.Getwd()
assert.NoError(t, err)

defer env.Override(t, "TEST_ROOT", path.Join(cwd, "testdata"))()
defer env.Override(t, _appRoot, path.Join(cwd, "testdata"))()
provider := NewYAMLProviderFromFiles(false, NewRelativeResolver(), "base.yaml", "dev.yaml", "secrets.yaml")

baseValue := provider.Get("value").AsString()
Expand Down

0 comments on commit 71f9f75

Please sign in to comment.