-
Notifications
You must be signed in to change notification settings - Fork 11
/
find.go
61 lines (49 loc) · 1.51 KB
/
find.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package config
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"github.com/charmbracelet/log"
)
const (
configFile = ".meteor.json"
)
// FindConfigFile will find the config files based in the rules below:
// 1. If the current directory contains a .meteor.json file, it will be used.
// 2. If the current directory does not contain a .meteor.json file, the parent
// 3. IF parent doesn't contain the .meteor.json file, the search will continue until the home directory is reached.
// 4. If no .meteor.json file is found, look in ~/.config/meteor/config.json
// 5. If no .meteor.json file is found, return an error
func FindConfigFile() (string, error) {
if _, err := os.Stat(configFile); err == nil {
return path.Join("./", configFile), nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error getting home dir: %w", err)
}
currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error getting current dir: %w", err)
}
for {
rel, _ := filepath.Rel(homeDir, currentDir)
if rel == ".." {
break
}
filePath := filepath.Join(currentDir, configFile)
log.Debug("checking for config file", "path", filePath)
if _, err := os.Open(filePath); err == nil {
return filePath, nil
}
currentDir += "/.."
}
xdgConfigFile := path.Join(homeDir, ".config/meteor/config.json")
log.Debug("checking for config file", "path", xdgConfigFile)
if _, err := os.Stat(xdgConfigFile); err == nil {
return xdgConfigFile, nil
}
return "", errors.New("no config file found")
}