-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_darwin.go
314 lines (270 loc) · 7.47 KB
/
detect_darwin.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//go:build darwin
// +build darwin
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/Masterminds/semver/v3"
"golang.org/x/sys/unix"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const DOCKER_DEPS_VERSION = `>=18362`
type OSInfo struct {
CurrentVersion string
ProductName string
CurrentMajorVersionNumber uint64
CurrentMinorVersionNumber uint64
ReleaseVersion string
BuildVersion string
}
func GetOSInfo() (*OSInfo, error) {
var uts unix.Utsname
if err := unix.Uname(&uts); err != nil {
return nil, err
}
sysname := unix.ByteSliceToString(uts.Sysname[:])
release := unix.ByteSliceToString(uts.Release[:])
dotPos := strings.Index(release, ".")
if dotPos == -1 {
return nil, errors.New(fmt.Sprintf("nvalid release value: %s", release))
}
major := release[:dotPos]
majorVersion, err := strconv.Atoi(major)
if err != nil {
return nil, err
}
strings.SplitN(release, ".", 2)
return &OSInfo{
CurrentVersion: release,
ProductName: sysname,
CurrentMajorVersionNumber: uint64(majorVersion),
CurrentMinorVersionNumber: uint64(majorVersion),
BuildVersion: release,
ReleaseVersion: release,
}, nil
}
func (this *OSInfo) IsWindows10() bool {
return strings.Contains(this.ProductName, `Windows 10`)
}
func (this *OSInfo) IsMacOS() bool {
return strings.Contains(this.ProductName, "Darwin")
}
func (this *OSInfo) MatchBuildVersion(Condition string) bool {
constraint, err := semver.NewConstraint(Condition)
if err != nil {
log.Error(err)
return false
}
ver := semver.MustParse(this.BuildVersion)
return constraint.Check(ver)
}
func IsScoopInstalled() (bool, string) {
PathEnv := os.Getenv("Path")
if len(PathEnv) > 0 {
pathList := strings.Split(PathEnv, `;`)
scoopShimPath := ""
for _, row := range pathList {
if strings.Contains(row, `scoop.ps1`) {
scoopShimPath = row
break
}
}
if len(scoopShimPath) > 0 {
return true, filepath.Join(scoopShimPath, `scoop`)
}
}
userDir, err := os.UserHomeDir()
if err == nil {
scoopDefault := filepath.Join(userDir, "scoop", "shims", "scoop.ps1")
if _, err := os.Stat(scoopDefault); err == nil {
return true, scoopDefault
}
}
return false, ""
}
func IsWinGetInstalled() (bool, string) {
return false, ""
}
func IsDockerInstalled() error {
_, err := os.Stat("/Applications/Docker.app")
if err != nil {
log.Error(err)
return NewLauncherError(ERROR_DOCKER_DESKTOP_NOT_INSTALLED,
"请先安装 Docker Desktop, https://doc.weixin.qq.com/doc/w2_AGUAAwb2AKY9YO5hUlhSjODSUvwi6?scode=AEwAtAeZAAkacJBfyk")
}
output, err := exec.Command("docker", "info").Output()
if err != nil {
log.Error(err)
return NewLauncherError(ERROR_DOCKER_DESKTOP_NOT_RUNNING,
"DockerDesktop 还未运行")
}
if !strings.Contains(string(output), "Server Version:") {
return NewLauncherError(ERROR_DOCKER_DESKTOP_NOT_RUNNING,
"DockerDesktop 还未运行")
}
return nil
}
func IsWindowTerminalInstalled() error {
return errors.New("OS not support")
}
const WINSERVICE_STATUS_STARTED = 4
const WINSERVICE_STATUS_STOPPED = 1
type WinService struct {
Status int `json:"Status"`
Name string `json:"Name"`
DisplayName string `json:"DisplayName"`
}
func parseService(raw string) *WinService {
item := new(WinService)
decoder := json.NewDecoder(strings.NewReader(raw))
err := decoder.Decode(item)
if err != nil {
log.Error(err)
}
return item
}
func DetectService(name string) (error, *WinService) {
return errors.New("OS not support"), nil
}
func LoadExistFrpcConfig(dir string) (string, error) {
filePath := filepath.Join(dir, "frpc.ini")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return "", errors.New("frpc.ini not found")
}
bin, err := ioutil.ReadFile(filePath)
if err != nil {
log.Error(err)
return "", err
}
r := regexp.MustCompile(`subdomain = ([^=]+)`)
found := r.FindStringSubmatch(string(bin))
if len(found) > 1 {
return strings.TrimSpace(found[1]), nil
}
return "", errors.New("sub domain not found")
}
func IsWordPressProject(dir string) bool {
projectRoot := filepath.Join(dir, "wp-load.php")
if _, err := os.Stat(projectRoot); err != nil && os.IsNotExist(err) {
return false
}
return true
}
func GetPHPDependFromWPDir(dir string) (string, error) {
includedPaths := filepath.Join(dir, "wp-includes", "version.php")
versionContent, err := ioutil.ReadFile(includedPaths)
if err != nil {
return "", err
}
r := regexp.MustCompile(`\$required_php_version = '([^']+)'`)
found := r.FindStringSubmatch(string(versionContent))
if len(found) > 1 {
return found[1], nil
}
return "", errors.New("php version can not be found")
}
type WPPHPVerion struct {
WPVersion string `json:"wp"`
PHPVersion string `json:"php"`
}
func MatchWordPressPHPVersion(wpVersion string) (string, error) {
baseVersion := "7.0"
wpPHPVersionMapping := []WPPHPVerion{
WPPHPVerion{WPVersion: "~5.6", PHPVersion: "8"},
WPPHPVerion{WPVersion: "~5.3", PHPVersion: "7.2"},
WPPHPVerion{WPVersion: "~5.2", PHPVersion: "7.2"},
WPPHPVerion{WPVersion: "~5.0", PHPVersion: "7.2"},
WPPHPVerion{WPVersion: "~4.9", PHPVersion: "7.2"},
WPPHPVerion{WPVersion: "~4.7", PHPVersion: "7.1"},
WPPHPVerion{WPVersion: "~4.4", PHPVersion: "7.0"},
WPPHPVerion{WPVersion: "~4.1", PHPVersion: "7.0"},
}
ver := semver.MustParse(wpVersion)
for _, targetConstraint := range wpPHPVersionMapping {
constraint, err := semver.NewConstraint(targetConstraint.WPVersion)
if err != nil {
return "", err
}
if constraint.Check(ver) {
return targetConstraint.PHPVersion, nil
}
}
return baseVersion, nil
}
func FindPublicDir(baseDir string) (string, error) {
if IsWordPressProject(baseDir) {
return baseDir, nil
}
baseDir, err := filepath.Abs(baseDir)
if err != nil {
return "", errors.New("不是一个合法的项目地址")
}
composerConfigPath := filepath.Join(baseDir, "composer.json")
depth := 1
tryMatch:
if _, err := os.Stat(composerConfigPath); err != nil && os.IsNotExist(err) {
baseDir = filepath.Dir(baseDir)
composerConfigPath = filepath.Join(baseDir, "composer.json")
depth++
if depth < 5 {
goto tryMatch
}
}
publicDir := filepath.Join(baseDir, "public")
if _, err := os.Stat(publicDir); err != nil && os.IsNotExist(err) {
return "", errors.New("不是一个合法的项目地址")
}
return publicDir, nil
}
func DetectPHPVersion(dir string) (string, error) {
composerConfigPath := filepath.Join(dir, "../composer.json")
if IsWordPressProject(dir) {
composerConfigPath = filepath.Join(dir, "composer.json")
wpVersion, err := GetPHPDependFromWPDir(dir)
if err != nil {
log.Error(err)
return "", err
}
phpVersion, err := MatchWordPressPHPVersion(wpVersion)
if err != nil {
log.Error(err)
return "", err
}
return phpVersion, nil
}
if _, err := os.Stat(composerConfigPath); os.IsNotExist(err) {
log.Error("未发现 composer.json")
return "", err
}
conf, err := LoadComposerJSON(composerConfigPath)
if err != nil {
log.Error(err)
return "", err
}
phpver, err := conf.MatchVersion("7.0", "7.2.99", "8.0.999", "8.1.999", "8.2.999")
if err != nil {
log.Error(err)
return "", err
}
if strings.EqualFold(phpver, "7.2.99") {
phpver = "7.2"
}
if strings.EqualFold(phpver, "8.0.999") {
phpver = "8.0"
}
if strings.EqualFold(phpver, "8.1.999") {
phpver = "8.1"
}
if strings.EqualFold(phpver, "8.2.999") {
phpver = "8.2"
}
log.Infof("配到PHP 运行版本为 %s \n", phpver)
return phpver, nil
}