aboutsummaryrefslogtreecommitdiff
path: root/simpleconf.go
blob: 777cd53e16168036a5ec5f18197598741c98bb20 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package simpleconf

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

type Config map[string]Section
type Section map[string]string

func (c Config) addSection(section Section, name string) {
	if section != nil {
		c[name] = section
	}
}

func Load(r io.Reader) (config Config, outerr error) {
	config = make(Config)
	scanner := bufio.NewScanner(r)

	var section Section
	var sectName string

	for l := 1; scanner.Scan(); l++ {
		line := strings.TrimSpace(scanner.Text())
		if len(line) == 0 {
			continue
		}

		switch line[0] {
		case ';', '#':
			continue
		case '[':
			parts := strings.SplitN(line, "[", 2)
			parts = strings.SplitN(parts[1], "]", 2)

			if len(parts) != 2 {
				return nil, fmt.Errorf("Missing closing ']' in section header at line %d", l)
			}
			if len(parts[1]) != 0 {
				return nil, fmt.Errorf("More data after closing ']' at line %d", l)
			}

			config.addSection(section, sectName)
			section = make(Section)
		default:
			parts := strings.SplitN(line, "=", 2)
			if len(parts) != 2 {
				return nil, fmt.Errorf("Couldn't neither find a comment, a section header nor a key-value pair at line %d", l)
			}

			key := strings.TrimSpace(parts[0])
			val := strings.TrimSpace(parts[1])
			if len(key) == 0 {
				return nil, fmt.Errorf("Empty key at line %d", l)
			}

			if section == nil {
				return nil, fmt.Errorf("Found key-value pair, but no section at line %d", l)
			}

			section[key] = val
		}
	}

	config.addSection(section, sectName)

	outerr = scanner.Err()
	return
}

var (
	NotFound = errors.New("Section or key not found.")
	NotBool  = errors.New("Could not interpret value as bool.")
)

func (c Config) GetString(section, key string) (string, error) {
	s, ok := c[section]
	if !ok {
		return "", NotFound
	}
	rv, ok := s[key]
	if !ok {
		return "", NotFound
	}
	return rv, nil
}

func (c Config) GetStringDefault(d, section, key string) (string, error) {
	rv, err := c.GetString(section, key)
	if err == NotFound {
		return d, nil
	}
	return rv, err
}

func (c Config) GetInt(section, key string) (int64, error) {
	s, err := c.GetString(section, key)
	if err != nil {
		return 0, err
	}
	return strconv.ParseInt(s, 10, 64)
}

func (c Config) GetIntDefault(d int64, section, key string) (int64, error) {
	rv, err := c.GetInt(section, key)
	if err == NotFound {
		return d, nil
	}
	return rv, err
}

func (c Config) GetFloat(section, key string) (float64, error) {
	s, err := c.GetString(section, key)
	if err != nil {
		return 0, err
	}
	return strconv.ParseFloat(s, 64)
}

func (c Config) GetFloatDefault(d float64, section, key string) (float64, error) {
	rv, err := c.GetFloat(section, key)
	if err == NotFound {
		return d, nil
	}
	return rv, err
}

func (c Config) GetBool(section, key string) (bool, error) {
	s, err := c.GetString(section, key)
	if err != nil {
		return false, err
	}
	switch strings.ToLower(s) {
	case "true", "on", "yes", "y", "1":
		return true, nil
	case "false", "off", "no", "n", "0":
		return false, nil
	default:
		return false, NotBool
	}
}

func (c Config) GetBoolDefault(d bool, section, key string) (bool, error) {
	rv, err := c.GetBool(section, key)
	if err == NotFound {
		return d, nil
	}
	return rv, err
}

func (c Config) GetFile(flag int, perm os.FileMode, section, key string) (*os.File, error) {
	s, err := c.GetString(section, key)
	if err != nil {
		return nil, err
	}

	return os.OpenFile(s, flag, perm)
}

func (c Config) GetFileReadonly(section, key string) (*os.File, error) {
	return c.GetFile(os.O_RDONLY, 0, section, key)
}