-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
58 lines (49 loc) · 1.26 KB
/
config.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func marvinFile(dir, configFile string) string {
currentDirectory, _ := os.Getwd()
// make sure directory exists
if _, dirExists := os.Stat(dir); dirExists != nil {
dir = "."
}
// switch to the directory
os.Chdir(dir)
// keep on going on up
for {
if _, configExists := os.Stat(configFile); configExists == nil {
marvinFile, marvinFileError := ioutil.ReadFile(configFile)
if marvinFileError != nil {
speak("Cannot read "+configFile, true)
}
return string(marvinFile)
}
curDir, curDirError := os.Getwd()
if curDirError != nil {
// huh, never seen this before
speak("Unable to get directory information", true)
}
// finished yet?
if abs, _ := filepath.Abs(curDir); abs == "/" {
os.Chdir(currentDirectory)
break
}
// go up a dir
os.Chdir("..")
}
// lets see if a home directory exists, based on ENV
homeConfigFile := strings.TrimRight(os.Getenv("HOME"), "/") + "/marvin.yml"
if _, homeFileError := os.Stat(homeConfigFile); homeFileError == nil {
marvinFile, marvinFileError := ioutil.ReadFile(homeConfigFile)
if marvinFileError != nil {
speak("Cannot read "+homeConfigFile, true)
}
return string(marvinFile)
}
// nothing found
return ""
}