-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
55 lines (45 loc) · 823 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 (
"github.com/pelletier/go-toml"
"github.com/pelletier/go-toml/query"
"io"
"os"
)
type Config struct {
t *toml.Tree
}
func (c *Config) Get(keys []string) interface{} {
if len(keys) == 0 {
return c.t
}
var results []interface{}
for _, v := range keys {
results = append(results, c.t.Get(v))
}
return results
}
func (c *Config) Query(q string) (interface{}, error) {
result, err := query.CompileAndExecute(q, c.t)
if err != nil {
return nil, err
}
return result.Values(), err
}
func readConfig(file string) (*Config, error) {
var r io.Reader
var err error
if file == "-" {
r = os.Stdin
} else {
r, err = os.Open(file)
if err != nil {
return nil, err
}
}
c := new(Config)
c.t, err = toml.LoadReader(r)
if err != nil {
return nil, err
}
return c, nil
}