Skip to content

Commit

Permalink
allow home astro config file location to be override via env var (#1122)
Browse files Browse the repository at this point in the history
* allow home astro config file location to be override via env var

* add and fix unit tests & fixed case when setting & unsetting the env var
  • Loading branch information
neel-astro authored Mar 28, 2023
1 parent 62a1978 commit 63a0700
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 1 addition & 2 deletions config/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ contexts:
last_used_workspace: ck05r3bor07h40d02y2hw4n4v
workspace: ck05r3bor07h40d02y2hw4n4v
`)
HomeConfigFile = "./test/config.yaml"
_ = afero.WriteFile(fs, "./test/config.yaml", configRaw, 0o777)
_ = afero.WriteFile(fs, HomeConfigFile, configRaw, 0o777)
InitConfig(fs)
}

Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func initHome(fs afero.Fs) {
viperHome.SetFs(fs)
viperHome.SetConfigName(ConfigFileName)
viperHome.SetConfigType(ConfigFileType)

configPath := os.Getenv("ASTRO_HOME")
if configPath != "" {
HomeConfigPath = filepath.Join(configPath, ConfigDir)
HomeConfigFile = filepath.Join(HomeConfigPath, ConfigFileNameWithExt)
} else {
HomeConfigPath = filepath.Join(HomePath, ConfigDir)
HomeConfigFile = filepath.Join(HomeConfigPath, ConfigFileNameWithExt)
}
viperHome.SetConfigFile(HomeConfigFile)

for _, cfg := range CFGStrMap {
Expand Down
14 changes: 12 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ func TestIsProjectDir(t *testing.T) {
}
}

func TestInitHome(t *testing.T) {
func TestInitHomeDefaultCase(t *testing.T) {
fs := afero.NewMemMapFs()
initHome(fs)
assert.Contains(t, viperHome.ConfigFileUsed(), "config.yaml")
homeDir, err := fileutil.GetHomeDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(homeDir, ".astro", "config.yaml"), viperHome.ConfigFileUsed())
}

func TestInitHomeConfigOverride(t *testing.T) {
fs := afero.NewMemMapFs()
os.Setenv("ASTRO_HOME", "test")
initHome(fs)
assert.Equal(t, filepath.Join("test", ".astro", "config.yaml"), viperHome.ConfigFileUsed())
os.Unsetenv("ASTRO_HOME")
}

func TestInitProject(t *testing.T) {
Expand Down

0 comments on commit 63a0700

Please sign in to comment.