-
Notifications
You must be signed in to change notification settings - Fork 155
/
appconfig.go
72 lines (61 loc) · 1.73 KB
/
appconfig.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
package gosms
import (
"errors"
"fmt"
ini "github.com/vaughan0/go-ini"
"strconv"
"strings"
)
/* ===== Application Configuration ===== */
type setting []string
func GetConfig(configFilePath string) (ini.File, error) {
appConfig, err := ini.LoadFile(configFilePath)
if err != nil {
return nil, err
}
ok, err := testConfig(appConfig)
if !ok {
return nil, err
}
return appConfig, nil
}
func testConfig(appConfig ini.File) (bool, error) {
//test if required parameters are present and are valid
requiredFields := []setting{{"SETTINGS", "SERVERHOST"},
{"SETTINGS", "SERVERPORT"},
{"SETTINGS", "RETRIES"},
{"SETTINGS", "DEVICES"},
{"SETTINGS", "BUFFERSIZE"},
{"SETTINGS", "BUFFERLOW"},
{"SETTINGS", "MSGTIMEOUT"},
{"SETTINGS", "MSGCOUNTOUT"},
{"SETTINGS", "MSGTIMEOUTLONG"},
}
for _, c := range requiredFields {
v, ok := appConfig.Get(c[0], c[1])
if !ok || strings.TrimSpace(v) == "" {
return false, errors.New("Fatal: " + c[1] + " is not set")
}
}
//now make sure all the devices are have required settings
tno, _ := appConfig.Get("SETTINGS", "DEVICES")
noOfDevices, _ := strconv.Atoi(tno)
requiredFields = []setting{}
for i := 0; i < noOfDevices; i++ {
d := fmt.Sprintf("DEVICE%v", i)
sCom := setting{d, "COMPORT"}
sBaud := setting{d, "BAUDRATE"}
sDevid := setting{d, "DEVID"}
requiredFields = append(requiredFields, sCom)
requiredFields = append(requiredFields, sBaud)
requiredFields = append(requiredFields, sDevid)
}
for _, c := range requiredFields {
v, ok := appConfig.Get(c[0], c[1])
if !ok || strings.TrimSpace(v) == "" {
return false, errors.New(strings.Join([]string{"Fatal:", c[0], c[1], "is not set"}, " "))
}
}
return true, nil
}
/* ===== Application Configuration ===== */