-
Notifications
You must be signed in to change notification settings - Fork 31
/
readConfig.go
80 lines (74 loc) · 2.42 KB
/
readConfig.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
// WebCall Copyright 2023 timur.mobi. All rights reserved.
package main
import (
"fmt"
"strconv"
"strings"
"gopkg.in/ini.v1" // https://pkg.go.dev/gopkg.in/go-ini/ini.v1
)
func readIniEntry(configIni *ini.File, keyword string) (string,bool) {
if configIni==nil {
return "",false
}
if !configIni.Section("").HasKey(keyword) {
return "",false
}
cfgEntry := configIni.Section("").Key(keyword).String()
commentIdx := strings.Index(cfgEntry, "#")
if commentIdx >= 0 {
cfgEntry = cfgEntry[:commentIdx]
}
return strings.TrimSpace(cfgEntry),true
}
func readIniBoolean(configIni *ini.File, cfgKeyword string, currentVal bool, defaultValue bool, init bool) bool {
newVal := defaultValue
cfgValue,ok := readIniEntry(configIni, cfgKeyword)
if ok && cfgValue!="" {
if cfgValue == "true" {
newVal = true
} else {
newVal = false
}
}
if (init && newVal != defaultValue) || (!init && newVal!=currentVal) {
isDefault:=""; if newVal==defaultValue { isDefault="*" }
fmt.Printf("%s bool %s=%v%s\n", configFileName, cfgKeyword, newVal, isDefault)
}
currentVal = newVal
return currentVal
}
func readIniInt(configIni *ini.File, cfgKeyword string, currentVal int, defaultValue int, factor int, init bool) int {
newVal := defaultValue
cfgValue,ok := readIniEntry(configIni, cfgKeyword)
if ok && cfgValue!="" {
i64, err := strconv.ParseInt(cfgValue, 10, 64)
if err != nil {
fmt.Printf("# %s int %s=%v err=%v\n", configFileName, cfgKeyword, cfgValue, err)
} else {
newVal = int(i64) * factor
}
}
if (init && newVal != defaultValue) || (!init && newVal!=currentVal) {
isDefault:=""; if newVal==defaultValue { isDefault="*" }
fmt.Printf("%s int %s=%d%s\n", configFileName, cfgKeyword, newVal, isDefault)
}
currentVal = newVal
return currentVal
}
func readIniString(configIni *ini.File, cfgKeyword string, currentVal string, defaultValue string, init bool) string {
newVal := defaultValue
cfgValue,ok := readIniEntry(configIni, cfgKeyword)
if ok && cfgValue != "" {
newVal = cfgValue
}
// do not log special keywords
if ((init && newVal != defaultValue) || (!init && newVal!=currentVal)) &&
!strings.HasSuffix(cfgKeyword, "Key") &&
!strings.HasSuffix(cfgKeyword, "Secret") &&
!strings.HasSuffix(cfgKeyword, "mastodonhandler") {
isDefault:=""; if newVal==defaultValue { isDefault="*" }
fmt.Printf("%s str %s=(%v)%s\n", configFileName, cfgKeyword, newVal, isDefault)
}
currentVal = newVal
return currentVal
}