-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
helper.go
102 lines (83 loc) · 1.96 KB
/
helper.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/mgutz/ansi"
)
func cliInfo() {
fmt.Println("GoAdmin CLI " + system.Version + compareVersion(system.Version))
fmt.Println()
}
func exitWithError(msg string) {
fmt.Println()
fmt.Println(ansi.Color("go-admin cli error: "+msg, "red"))
fmt.Println()
os.Exit(-1)
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func getLatestVersion() string {
http.DefaultClient.Timeout = time.Duration(time.Second * 3)
res, err := http.Get("https://goproxy.cn/github.com/!go!admin!group/go-admin/@v/list")
if err != nil || res.Body == nil {
return ""
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return ""
}
body, err := ioutil.ReadAll(res.Body)
if err != nil || body == nil {
return ""
}
versionsArr := strings.Split(string(body), "\n")
return versionsArr[len(versionsArr)-1]
}
func isRequireUpdate(srcVersion, toCompareVersion string) bool {
if toCompareVersion == "" {
return false
}
exp, _ := regexp.Compile(`-(.*)`)
srcVersion = exp.ReplaceAllString(srcVersion, "")
toCompareVersion = exp.ReplaceAllString(toCompareVersion, "")
srcVersionArr := strings.Split(strings.Replace(srcVersion, "v", "", -1), ".")
toCompareVersionArr := strings.Split(strings.Replace(toCompareVersion, "v", "", -1), ".")
for i := len(srcVersionArr) - 1; i > -1; i-- {
v, err := strconv.Atoi(srcVersionArr[i])
if err != nil {
return false
}
vv, err := strconv.Atoi(toCompareVersionArr[i])
if err != nil {
return false
}
if v < vv {
return true
} else if v > vv {
return false
} else {
continue
}
}
return false
}
func compareVersion(srcVersion string) string {
toCompareVersion := getLatestVersion()
if isRequireUpdate(srcVersion, toCompareVersion) {
return ", the latest version is " + toCompareVersion + " now."
} else {
return ""
}
}