...
1
2
3 package viper
4
5 import (
6 "fmt"
7 "os"
8 "path/filepath"
9
10 "github.com/spf13/afero"
11 )
12
13
14
15 func (v *Viper) findConfigFile() (string, error) {
16 v.logger.Info("searching for config in paths", "paths", v.configPaths)
17
18 for _, cp := range v.configPaths {
19 file := v.searchInPath(cp)
20 if file != "" {
21 return file, nil
22 }
23 }
24 return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
25 }
26
27 func (v *Viper) searchInPath(in string) (filename string) {
28 v.logger.Debug("searching for config in path", "path", in)
29 for _, ext := range SupportedExts {
30 v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
31 if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
32 v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
33 return filepath.Join(in, v.configName+"."+ext)
34 }
35 }
36
37 if v.configType != "" {
38 if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
39 return filepath.Join(in, v.configName)
40 }
41 }
42
43 return ""
44 }
45
46
47 func exists(fs afero.Fs, path string) (bool, error) {
48 stat, err := fs.Stat(path)
49 if err == nil {
50 return !stat.IsDir(), nil
51 }
52 if os.IsNotExist(err) {
53 return false, nil
54 }
55 return false, err
56 }
57
View as plain text