-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
62 lines (57 loc) · 1.38 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
54
55
56
57
58
59
60
61
62
package main
import (
"bufio"
"encoding/json"
"os"
)
// LogConfig struct mirroring the config file schema
type LogConfig struct {
Name string `json:"name"`
Url string `json:"url"`
LastIndex int64 `json:"index"`
BucketSize int64 `json:"window"`
UpdatePeriod int64 `json:"limit"`
MaximumIndex int64 `json:"stop"`
HostNames []string `json:"hostnames"`
}
// Configuration "configuration", list of configs for each log we pull from
type Configuration []LogConfig
// WriteConfig dump the configuration objects to the relevant file
func (logs Configuration) WriteConfig(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for _, log := range logs {
bytes, err := json.Marshal(log)
if err != nil {
return err
}
f.Write(bytes)
f.WriteString("\n")
}
return nil
}
// NewConfiguration Create a new configuration object from a given file
func NewConfiguration(filename string) (Configuration, error) {
res := Configuration{}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == "" {
break
}
parsed := LogConfig{}
json.Unmarshal([]byte(scanner.Text()), &parsed)
res = append(res, parsed)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return res, nil
}