Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/viper/file.go
2875 views
1
package viper
2
3
import (
4
"fmt"
5
"os"
6
"path/filepath"
7
8
"github.com/sagikazarmark/locafero"
9
"github.com/spf13/afero"
10
)
11
12
// ExperimentalFinder tells Viper to use the new Finder interface for finding configuration files.
13
func ExperimentalFinder() Option {
14
return optionFunc(func(v *Viper) {
15
v.experimentalFinder = true
16
})
17
}
18
19
// Search for a config file.
20
func (v *Viper) findConfigFile() (string, error) {
21
finder := v.finder
22
23
if finder == nil && v.experimentalFinder {
24
var names []string
25
26
if v.configType != "" {
27
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
28
} else {
29
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
30
}
31
32
finder = locafero.Finder{
33
Paths: v.configPaths,
34
Names: names,
35
Type: locafero.FileTypeFile,
36
}
37
}
38
39
if finder != nil {
40
return v.findConfigFileWithFinder(finder)
41
}
42
43
return v.findConfigFileOld()
44
}
45
46
func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) {
47
results, err := finder.Find(v.fs)
48
if err != nil {
49
return "", err
50
}
51
52
if len(results) == 0 {
53
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
54
}
55
56
// We call clean on the final result to ensure that the path is in its canonical form.
57
// This is mostly for consistent path handling and to make sure tests pass.
58
return results[0], nil
59
}
60
61
// Search all configPaths for any config file.
62
// Returns the first path that exists (and is a config file).
63
func (v *Viper) findConfigFileOld() (string, error) {
64
v.logger.Info("searching for config in paths", "paths", v.configPaths)
65
66
for _, cp := range v.configPaths {
67
file := v.searchInPath(cp)
68
if file != "" {
69
return file, nil
70
}
71
}
72
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
73
}
74
75
func (v *Viper) searchInPath(in string) (filename string) {
76
v.logger.Debug("searching for config in path", "path", in)
77
for _, ext := range SupportedExts {
78
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
79
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
80
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
81
return filepath.Join(in, v.configName+"."+ext)
82
}
83
}
84
85
if v.configType != "" {
86
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
87
return filepath.Join(in, v.configName)
88
}
89
}
90
91
return ""
92
}
93
94
// exists checks if file exists.
95
func exists(fs afero.Fs, path string) (bool, error) {
96
stat, err := fs.Stat(path)
97
if err == nil {
98
return !stat.IsDir(), nil
99
}
100
if os.IsNotExist(err) {
101
return false, nil
102
}
103
return false, err
104
}
105
106