-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfile.go
55 lines (51 loc) · 1.25 KB
/
file.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
package main
import (
"io/ioutil"
"log"
"os"
"path"
)
// fileOrConst works like tryReadFile, except it returns a string
// and, if the file is not accessible, a default string.
func fileOrConst(fn string, def string) string {
pn := tryReadFile(fn)
if len(pn) > 0 {
return string(pn)
}
return def
}
// tryReadFile takes a _filename_ and uses tryFile() to find the file and
// eventually return its contents. If the files was not found or is unreadable
// returns an empty byte slice.
func tryReadFile(fn string) []byte {
pn := tryFile(fn)
contents, err := ioutil.ReadFile(pn)
if err == nil {
return contents
}
return []byte{}
}
// tryFile takes a _filename_ as an argument and tries several directories to
// find this file. In the case of success it returns the full path name,
// otherwise it returns the empty string.
func tryFile(fn string) string {
var dirs []string
cwd, err := os.Getwd()
if err == nil {
dirs = append(dirs, cwd)
} else {
log.Println("could not get working directory")
}
dirs = append(dirs, configDir)
for _, dir := range dirs {
pn := path.Join(dir, fn)
f, err := os.Open(pn)
if err == nil {
log.Printf("found %s in %s\n", fn, dir)
f.Close()
return pn
}
}
log.Println("could not find", fn)
return ""
}