-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
71 lines (63 loc) · 1.6 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
63
64
65
66
67
68
69
70
71
package logsum
import (
"io/ioutil"
"strings"
"errors"
"strconv"
"log"
)
type configStruc struct {
Version versionStruc
LogFilePath string
PS1Thresh float64
PS5Thresh float64
NrLines int64
}
type versionStruc struct{
Major string
Minor string
}
func Split(r rune) bool {
return r == '\n' || r == '='
}
func Read(fileName string) (configStruc, error){
var conf configStruc
var err error
/*open summary file*/
content, err := ioutil.ReadFile(fileName)
if err == nil {
result := string(content)
temp := strings.FieldsFunc(result,Split)
if len(temp) != 11 {
err = errors.New("Corrupt config file")
} else {
conf.Version.Major, conf.Version.Minor = versionParse(temp[2][0:len(temp[2])-1])
conf.LogFilePath = temp[4][0:len(temp[4])-1]
conf.PS1Thresh, err = strconv.ParseFloat(temp[6][0:len(temp[6])-1], 64)
checkError("conv", err)
conf.PS5Thresh, err = strconv.ParseFloat(temp[8][0:len(temp[8])-1], 64)
checkError("conv", err)
conf.NrLines, err = strconv.ParseInt(temp[10], 10, 64)
checkError("conv", err)
}
}
return conf, err
}
func versionParse(str string) (string, string){
tmp := strings.SplitN(str, ".", 2)
var major string
var minor string
if len(tmp)>1 {
major = tmp[0]
minor = tmp[1]
} else {
major = "0"
minor = "0"
}
return major, minor
}
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}