Skip to content

Commit a21424a

Browse files
committed
Introduce environment variable for custom config dir
Signed-off-by: Reinhard Nägele <[email protected]>
1 parent 2407e7d commit a21424a

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

pkg/config/config.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package config
1717
import (
1818
"fmt"
1919
"os"
20-
"path"
20+
"path/filepath"
2121
"reflect"
2222
"strings"
2323

@@ -34,7 +34,7 @@ var (
3434
homeDir, _ = homedir.Dir()
3535
configSearchLocations = []string{
3636
".",
37-
path.Join(homeDir, ".ct"),
37+
filepath.Join(homeDir, ".ct"),
3838
"/usr/local/etc/ct",
3939
"/etc/ct",
4040
}
@@ -88,8 +88,12 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C
8888
v.SetConfigFile(cfgFile)
8989
} else {
9090
v.SetConfigName("ct")
91-
for _, searchLocation := range configSearchLocations {
92-
v.AddConfigPath(searchLocation)
91+
if cfgFile, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
92+
v.AddConfigPath(cfgFile)
93+
} else {
94+
for _, searchLocation := range configSearchLocations {
95+
v.AddConfigPath(searchLocation)
96+
}
9397
}
9498
}
9599

@@ -182,11 +186,17 @@ func printCfg(cfg *Configuration) {
182186
}
183187

184188
func findConfigFile(fileName string) (string, error) {
189+
var cfgFile string
190+
if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
191+
return filepath.Join(dir, fileName), nil
192+
}
193+
185194
for _, location := range configSearchLocations {
186-
filePath := path.Join(location, fileName)
195+
filePath := filepath.Join(location, fileName)
187196
if util.FileExists(filePath) {
188-
return filePath, nil
197+
return cfgFile, nil
189198
}
190199
}
200+
191201
return "", errors.New(fmt.Sprintf("Config file not found: %s", fileName))
192202
}

pkg/util/util.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"math/rand"
2222
"net"
2323
"os"
24-
"path"
2524
"path/filepath"
2625
"regexp"
2726
"strings"
@@ -115,7 +114,7 @@ func (l DirectoryLister) ListChildDirs(parentDir string, test func(dir string) b
115114
var dirs []string
116115
for _, dir := range fileInfos {
117116
dirName := dir.Name()
118-
parentSlashChildDir := path.Join(parentDir, dirName)
117+
parentSlashChildDir := filepath.Join(parentDir, dirName)
119118
if test(parentSlashChildDir) {
120119
dirs = append(dirs, parentSlashChildDir)
121120
}
@@ -130,8 +129,8 @@ func (u ChartUtils) LookupChartDir(chartDirs []string, dir string) (string, erro
130129
for _, chartDir := range chartDirs {
131130
currentDir := dir
132131
for {
133-
chartYaml := path.Join(currentDir, "Chart.yaml")
134-
parent := path.Dir(path.Dir(chartYaml))
132+
chartYaml := filepath.Join(currentDir, "Chart.yaml")
133+
parent := filepath.Dir(filepath.Dir(chartYaml))
135134
chartDir = strings.TrimRight(chartDir, "/") // remove any trailing slash from the dir
136135

137136
// check directory has a Chart.yaml and that it is in a
@@ -140,9 +139,9 @@ func (u ChartUtils) LookupChartDir(chartDirs []string, dir string) (string, erro
140139
return currentDir, nil
141140
}
142141

143-
currentDir = path.Dir(currentDir)
142+
currentDir = filepath.Dir(currentDir)
144143
relativeDir, _ := filepath.Rel(chartDir, currentDir)
145-
joined := path.Join(chartDir, relativeDir)
144+
joined := filepath.Join(chartDir, relativeDir)
146145
if (joined == chartDir) || strings.HasPrefix(relativeDir, "..") {
147146
break
148147
}
@@ -155,7 +154,7 @@ func (u ChartUtils) LookupChartDir(chartDirs []string, dir string) (string, erro
155154
// and return a newly allocated ChartYaml object. If no Chart.yaml is present
156155
// or there is an error unmarshaling the file contents, an error will be returned.
157156
func ReadChartYaml(dir string) (*ChartYaml, error) {
158-
yamlBytes, err := ioutil.ReadFile(path.Join(dir, "Chart.yaml"))
157+
yamlBytes, err := ioutil.ReadFile(filepath.Join(dir, "Chart.yaml"))
159158
if err != nil {
160159
return nil, errors.Wrap(err, "Could not read 'Chart.yaml'")
161160
}

0 commit comments

Comments
 (0)