-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
157 lines (123 loc) · 3.28 KB
/
version.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package version
import (
"errors"
"os"
"os/exec"
"strings"
"gopkg.in/ini.v1"
"github.com/wissemmansouri/OpenIT.one-Common/utils/file"
_ "github.com/mattn/go-sqlite3" // nolint
)
const (
LegacyOpenITServiceName = "openit.service"
configKeyUniqueToZero3x = "USBAutoMount"
configKeyDBPath = "DBPath"
)
var (
// this value will be updated at init() to actual config file path.
LegacyOpenITConfigFilePath = "/etc/openit.conf"
_configFile *ini.File
_openITBinFilePath string
)
var (
ErrLegacyVersionNotFound = errors.New("legacy version not found")
ErrVersionNotFound = errors.New("version (non-legacy) not found")
)
func init() {
serviceFilePath := file.FindFirstFile("/etc/systemd", LegacyOpenITServiceName)
if serviceFilePath == "" {
return
}
serviceFile, err := ini.Load(serviceFilePath)
if err != nil {
return
}
section, err := serviceFile.GetSection("Service")
if err != nil {
return
}
key, err := section.GetKey("ExecStart")
if err != nil {
return
}
execStart := key.Value()
texts := strings.Split(execStart, " ")
// locaste openit binary.
_openITBinFilePath = texts[0]
if _, err := os.Stat(_openITBinFilePath); os.IsNotExist(err) {
_openITBinFilePath, err = exec.LookPath("openit")
if err != nil {
return
}
}
// locate the config file
if len(texts) > 2 {
for i, text := range texts {
if text == "-c" {
LegacyOpenITConfigFilePath = texts[i+1]
break
}
}
}
if _, err := os.Stat(LegacyOpenITConfigFilePath); os.IsNotExist(err) {
return
}
_configFile, _ = ini.Load(LegacyOpenITConfigFilePath)
}
func DetectLegacyVersion() (int, int, int, error) {
if _, _, _, err := DetectVersion(); err == nil {
return -1, -1, -1, ErrLegacyVersionNotFound
}
if _configFile == nil {
return -1, -1, -1, ErrLegacyVersionNotFound
}
minorVersion, err := DetectMinorVersion()
if err != nil {
return -1, -1, -1, err
}
if minorVersion == 2 {
return 0, 2, 99, nil // 99 means we don't know the patch version.
}
configKeyDBPathExist, err := IsConfigKeyDBPathExist()
if err != nil {
return -1, -1, -1, err
}
if !configKeyDBPathExist {
return 0, 3, 0, nil // it could be 0.3.0, 0.3.1 or 0.3.2 but only one version can be returned.
}
return 0, 3, 3, nil // it could be 0.3.3 or 0.3.4 but only one version can be returned.
}
func DetectVersion() (int, int, int, error) {
cmd := exec.Command(_openITBinFilePath, "-v")
versionBytes, err := cmd.Output()
if err != nil {
return -1, -1, -1, ErrVersionNotFound
}
major, minor, patch, _, _, err := ParseVersion(string(versionBytes))
if err != nil {
return -1, -1, -1, ErrVersionNotFound
}
return major, minor, patch, nil
}
// Detect minor version of OpenIT. It returns 2 for "0.2.x" or 3 for "0.3.x"
//
// (This is often useful when failing to get version from API because OpenIT is not running.)
func DetectMinorVersion() (int, error) {
if _configFile == nil {
return -1, ErrLegacyVersionNotFound
}
if _configFile.Section("server").HasKey(configKeyUniqueToZero3x) {
return 3, nil
}
return 2, nil
}
// Check if user data is stored in database (0.3.3+)
func IsConfigKeyDBPathExist() (bool, error) {
if _configFile == nil {
return false, ErrLegacyVersionNotFound
}
if !_configFile.Section("app").HasKey(configKeyDBPath) {
return false, nil
}
return true, nil
}