-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.go
414 lines (388 loc) · 8.11 KB
/
utils.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"bufio"
"cfw-updater/platform"
"fmt"
"github.com/cheggaaa/pb/v3"
"github.com/eiannone/keyboard"
"github.com/gen2brain/go-unarr"
"github.com/mholt/archiver/v3"
"io"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
// 运行时调用startBackground的次数
var runIdx = 0
func timeCost(start time.Time) {
fmt.Printf("time cost: %v\n", time.Since(start).String())
}
func fullPath(basePath string) string {
return path.Join(tempPath, basePath)
}
func exit(info string) {
if IsExists(tempPath) {
os.RemoveAll(tempPath)
}
if info != "" {
fmt.Println(info)
}
if runtime.GOOS == "windows" {
fmt.Printf("\nPress any key to exit...")
b := make([]byte, 1)
os.Stdin.Read(b)
}
os.Exit(0)
}
// IsExists 检测指定路径文件或者文件夹是否存在
func IsExists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
type WriteCounter struct {
bar *pb.ProgressBar
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.bar.Add(n)
return n, nil
}
func downloadFile(url, downLoadPath string) {
var (
count int
err error
resp *http.Response
bar *pb.ProgressBar
out *os.File
)
closeDownload := func() {
if bar != nil {
bar.Finish()
}
if resp != nil {
resp.Body.Close()
}
if out != nil {
out.Close()
}
}
fmt.Println(fmt.Sprintf("正在下载: %s", url))
for {
if resp, err = http.Get(url); err != nil {
if count > 3 {
closeDownload()
exit(err.Error())
} else {
count++
fmt.Println("正在重试中(http get)..")
continue
}
}
if resp.StatusCode == 404 {
exit(fmt.Sprintf("%s: 404 Not Found", url))
}
if downLoadPath == "" {
downLoadPath = fullPath(path.Base(url))
}
out, err = os.Create(downLoadPath)
if err != nil {
closeDownload()
exit(err.Error())
}
bar = pb.StartNew(int(resp.ContentLength))
bar.Set(pb.Bytes, true)
bar.SetTemplateString(`{{counters .}} {{bar .}} {{percent .}} {{speed . "%s/s"}} {{rtime .}}`)
counter := &WriteCounter{bar}
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
closeDownload()
if count > 3 {
exit(err.Error())
} else {
count++
fmt.Println("正在重试中(io copy)..")
continue
}
}
closeDownload()
break
}
}
func searchText(r io.Reader, key string) string {
scanner := bufio.NewScanner(r)
var findStr strings.Builder
for scanner.Scan() {
if strings.Contains(scanner.Text(), key) {
findStr.WriteString(scanner.Text())
}
}
return findStr.String()
}
func httpGet(url string) *http.Response {
var (
count int
err error
resp *http.Response
)
for {
resp, err = http.Get(url)
if err == nil {
break
}
if count > 3 {
exit(err.Error())
} else {
fmt.Println("正在重试中..")
count++
}
}
return resp
}
func extExists(key string, exts ...string) bool {
for _, v := range exts {
if path.Ext(key) == v {
return true
}
}
return false
}
func webSearch(url, key string) string {
resp := httpGet(url)
defer resp.Body.Close()
return searchText(resp.Body, key)
}
func webFindUrl(url string, keys ...string) string {
var (
err error
resp *http.Response
)
for _, v := range keys {
downloadUrl := fmt.Sprintf("%s/%s", url, v)
resp, err = http.Get(downloadUrl)
if err == nil && resp.StatusCode != 404 {
return downloadUrl
}
}
return ""
}
func recentlyTag(url string) []string {
var tagStr string
searchText := webSearch(url, "archive/refs")
if searchText == "" {
exit(fmt.Sprintf("获取%s最新版本号失败!", url))
}
result := regexp.MustCompile(`[\d.]{2,}\d`).FindAllStringSubmatch(searchText, -1)
if result == nil {
exit(fmt.Sprintf("获取%s最新版本号失败!", url))
}
for _, v := range result {
if !strings.Contains(tagStr, v[0]) {
tagStr = tagStr + " " + v[0]
}
}
return strings.Split(strings.TrimPrefix(tagStr, " "), " ")
}
func extractFile(name string) {
stopCh := make(chan struct{})
closeChan := func() {
close(stopCh)
fmt.Println("")
}
go showProgress(fmt.Sprintf("解压%s中", name), stopCh)
extractPath := fullPath(strings.TrimSuffix(name, path.Ext(name)))
if !IsExists(extractPath) {
if extExists(name, ".7z", ".zip") {
a, err := unarr.NewArchive(fullPath(name))
if err != nil {
closeChan()
exit(err.Error())
}
defer a.Close()
_, err = a.Extract(extractPath)
if err != nil {
closeChan()
exit(err.Error())
}
} else if path.Ext(name) == ".rar" {
if err := archiver.Unarchive(fullPath(name), extractPath); err != nil {
closeChan()
exit(err.Error())
}
}
}
closeChan()
}
func getChar(str string) string {
err := keyboard.Open()
if err != nil {
panic(err)
}
defer keyboard.Close()
fmt.Print(str)
char, _, err := keyboard.GetKey()
fmt.Printf("%c\n", char)
if char == 0 {
return ""
} else {
return string(char)
}
}
// IsNumeric is_numeric()
func IsNumeric(val interface{}) bool {
switch val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case float32, float64, complex64, complex128:
return true
case string:
str := val.(string)
if str == "" {
return false
}
// Trim any whitespace
str = strings.TrimSpace(str)
if str[0] == '-' || str[0] == '+' {
if len(str) == 1 {
return false
}
str = str[1:]
}
// hex
if len(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') {
for _, h := range str[2:] {
if !((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) {
return false
}
}
return true
}
// 0-9, Point, Scientific
p, s, l := 0, 0, len(str)
for i, v := range str {
if v == '.' { // Point
if p > 0 || s > 0 || i+1 == l {
return false
}
p = i
} else if v == 'e' || v == 'E' { // Scientific
if i == 0 || s > 0 || i+1 == l {
return false
}
s = i
} else if v < '0' || v > '9' {
return false
}
}
return true
}
return false
}
func LoopInput(tip string, len int) int {
for {
inputString := ""
if len < 10 {
inputString = getChar(tip)
} else {
fmt.Print(tip)
_, _ = fmt.Scanln(&inputString)
}
if inputString == "" {
return -1
} else if !IsNumeric(inputString) {
fmt.Println("输入有误,请重新输入")
continue
}
number, _ := strconv.Atoi(inputString)
if number <= len && number > 0 {
return number
} else {
fmt.Println("输入数字越界,请重新输入")
}
}
}
// startBackground 把本身程序转化为后台运行
func startBackground() {
//判断子进程还是父进程
runIdx++
envIdx, err := strconv.Atoi(os.Getenv("CFW_DAEMON_IDX"))
if err != nil {
envIdx = 0
}
if runIdx <= envIdx { //子进程, 退出
return
}
//启动子进程
cmd := &exec.Cmd{
Path: os.Args[0],
Args: os.Args,
Env: append(os.Environ(), fmt.Sprintf("%s=%d", "CFW_DAEMON_IDX", runIdx)),
SysProcAttr: platform.NewSysProcAttr(),
}
if err := cmd.Start(); err != nil {
exit(err.Error())
}
}
func showProgress(tip string, stopCh chan struct{}) {
count := 1
for {
select {
case <-stopCh:
return
default:
if count > 3 {
count = 1
}
fmt.Printf("\r%s%-3s", tip, strings.Repeat(".", count))
time.Sleep(time.Millisecond * 500)
count++
}
}
}
// execCommand 运行命令并实时查看运行结果
func execCommand(command string) error {
cmd := exec.Command("zsh", "-c", command)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
fmt.Println("Error:The command is err: ", err.Error())
return err
}
ch := make(chan string, 100)
stdoutScan := bufio.NewScanner(stdout)
stderrScan := bufio.NewScanner(stderr)
go func() {
for stdoutScan.Scan() {
line := stdoutScan.Text()
ch <- line
}
}()
go func() {
for stderrScan.Scan() {
line := stderrScan.Text()
ch <- line
}
}()
var err error
go func() {
err = cmd.Wait()
if err != nil && !strings.Contains(err.Error(), "exit status") {
fmt.Println("wait:", err.Error())
}
close(ch)
}()
for line := range ch {
fmt.Println(line)
}
return err
}