-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.go
95 lines (78 loc) · 2.13 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package mtproto
import (
"fmt"
"runtime"
"time"
)
const (
appConfigError = "App configuration error: %s"
defaultPingInterval = 1 * time.Minute
defaultSendInterval = 500 * time.Millisecond
)
type Configuration struct {
//Id int32
//Hash string
Version string
DeviceModel string
SystemVersion string
Language string
//SessionHome string
PingInterval time.Duration
SendInterval time.Duration
KeyPath string
}
func NewConfiguration(version, deviceModel, systemVersion, language string,
pingInterval time.Duration, sendInterval time.Duration, keyPath string) (Configuration, error) {
//appConfig := new(Configuration)
appConfig := Configuration{}
if version == "" {
return Configuration{}, fmt.Errorf(appConfigError, "version is empty")
}
appConfig.Version = version
appConfig.DeviceModel = deviceModel
if deviceModel == "" {
appConfig.DeviceModel = "Unknown"
}
appConfig.SystemVersion = systemVersion
if systemVersion == "" {
appConfig.SystemVersion = runtime.GOOS + "/" + runtime.GOARCH
}
appConfig.Language = language
if language == "" {
appConfig.Language = "en"
}
//appConfig.SessionHome = sessionFileHome
//if sessionFileHome == "" {
// usr, err := user.Current()
// if err != nil {
// appConfig.SessionHome = os.Getenv("HOME")
// } else {
// appConfig.SessionHome = usr.HomeDir
// }
//}
appConfig.KeyPath = keyPath
appConfig.PingInterval = pingInterval
if pingInterval == 0 {
appConfig.PingInterval = defaultPingInterval
}
appConfig.SendInterval = sendInterval
if sendInterval == 0 {
appConfig.SendInterval = defaultSendInterval
}
return appConfig, nil
}
func (appConfig Configuration) Check() error {
if appConfig.Version == "" {
return fmt.Errorf(appConfigError, "Configuration.Version is empty")
}
if appConfig.DeviceModel == "" {
return fmt.Errorf(appConfigError, "Configuration.DeviceModel is empty")
}
if appConfig.SystemVersion == "" {
return fmt.Errorf(appConfigError, "Configuration.SystemVersion is empty")
}
if appConfig.Language == "" {
return fmt.Errorf(appConfigError, "Configuration.Language is empty")
}
return nil
}