-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
55 lines (49 loc) · 991 Bytes
/
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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/tobstarr/wlcli/Godeps/_workspace/src/github.com/BurntSushi/toml"
)
const configFileName = ".wlcli"
type Config struct {
ListID int `toml:"list_id"`
}
func loadCurrentConfig() (c *Config, err error) {
current, err := os.Getwd()
if err != nil {
return nil, err
}
for current != "" {
path := filepath.Join(current, configFileName)
dbg.Printf("testing path %s", path)
c, err := loadConfig(path)
if err != nil {
newCurrent := filepath.Dir(current)
if newCurrent == current {
break
}
current = newCurrent
} else {
dbg.Printf("found path %s", path)
return c, nil
}
}
return nil, nil
}
func loadConfig(path string) (c *Config, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
_, err = toml.Decode(string(b), &c)
if err != nil {
return nil, err
}
return c, nil
}