forked from revel/revel
/
config.go
53 lines (44 loc) · 1.18 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
package rev
import (
"github.com/kless/goconfig/config"
)
// This handles the parsing of application.conf
// It has a "preferred" section that is checked first for option queries.
// If the preferred section does not have the option, the DEFAULT section is
// checked fallback.
type MergedConfig struct {
*config.Config
section string // Check this section first, then fall back to DEFAULT
}
func LoadConfig(fname string) (*MergedConfig, error) {
conf, err := config.ReadDefault(fname)
if err != nil {
return nil, err
}
return &MergedConfig{conf, ""}, nil
}
func (c *MergedConfig) SetSection(section string) {
c.section = section
}
func (c *MergedConfig) Int(option string) (int, error) {
if r, err := c.Config.Int(c.section, option); err == nil {
return r, nil
}
return c.Config.Int("DEFAULT", option)
}
func (c *MergedConfig) String(option string) (string, error) {
if r, err := c.Config.String(c.section, option); err == nil {
return stripQuotes(r), nil
}
r, err := c.Config.String("DEFAULT", option)
return stripQuotes(r), err
}
func stripQuotes(s string) string {
if s == "" {
return s
}
if s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
return s
}