Skip to content

Commit

Permalink
Introduce environment variable for custom config dir
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus committed Nov 2, 2020
1 parent fd6720b commit 0530a1b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C
v.SetConfigFile(cfgFile)
} else {
v.SetConfigName("ct")
for _, searchLocation := range configSearchLocations {
v.AddConfigPath(searchLocation)
if cfgFile, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
v.AddConfigPath(cfgFile)
} else {
for _, searchLocation := range configSearchLocations {
v.AddConfigPath(searchLocation)
}
}
}

Expand Down Expand Up @@ -182,11 +186,17 @@ func printCfg(cfg *Configuration) {
}

func findConfigFile(fileName string) (string, error) {
var cfgFile string
if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
return filepath.Join(dir, fileName), nil
}

for _, location := range configSearchLocations {
filePath := filepath.Join(location, fileName)
if util.FileExists(filePath) {
return filePath, nil
return cfgFile, nil
}
}

return "", errors.New(fmt.Sprintf("Config file not found: %s", fileName))
}

0 comments on commit 0530a1b

Please sign in to comment.