-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
83 lines (71 loc) · 1.59 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package smartreboot
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/BrenekH/logange"
)
var (
checkIntervalRe *regexp.Regexp
logLevelRe *regexp.Regexp
)
func init() {
// Create Regexes and exit (panic) if they fail to compile.
// This allows them to be used for every regex search instead of needing to recompile everytime.
var err error
checkIntervalRe, err = regexp.Compile(`^*CheckInterval=(.*)`)
if err != nil {
panic(err)
}
logLevelRe, err = regexp.Compile(`^LogLevel=(.*)`)
if err != nil {
panic(err)
}
}
func ParseConfFile(filename string) (c Conf, err error) {
f, err := os.Open(filename)
if err != nil {
return
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return
}
contents := string(b)
checkIntervalMatch := checkIntervalRe.FindStringSubmatch(contents)
if len(checkIntervalMatch) > 0 {
interval, err := strconv.Atoi(checkIntervalMatch[1])
if err != nil {
return c, err
}
c.CheckInterval = interval
} else {
c.CheckInterval = 1
}
logLevelMatch := logLevelRe.FindStringSubmatch(contents)
if len(logLevelMatch) > 0 {
switch strings.ToLower(logLevelMatch[1]) {
case "trace":
c.LogLevel = logange.LevelTrace
case "debug":
c.LogLevel = logange.LevelDebug
case "info":
c.LogLevel = logange.LevelInfo
case "warning", "warn":
c.LogLevel = logange.LevelWarn
case "error":
c.LogLevel = logange.LevelWarn
case "critical":
c.LogLevel = logange.LevelCritical
default:
return c, fmt.Errorf("unknown log level '%v'", logLevelMatch[1])
}
} else {
c.LogLevel = logange.LevelWarn
}
return
}