-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
111 lines (94 loc) · 2.06 KB
/
remote.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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// validateRemotePackageConf validate remote rime package.conf
func validateRemotePackageConf(str string) (string, error) {
// fillup host
if !strings.HasPrefix(str, "http") {
str = "https://github.com/" + str
}
// replace the @master bits
idx := strings.Index(str, "@")
if idx > 0 {
bits := make([]byte, len(str)-idx)
var j int
for i := idx; i < len(str); i++ {
if str[i] == '/' {
break
}
if i == len(str)-1 {
break
}
bits = append(bits, str[i])
j++
}
branch := string(bits[len(bits)-j:])
str = strings.Replace(str, branch, "/raw/"+branch[1:], 1)
}
_, err := url.Parse(str)
if err != nil {
return str, err
}
return str, nil
}
// parsePackageConf parse rime package.conf
func parsePackageConf(bits []byte) ([]string, error) {
strs := make([]string, bytes.Count(bits, []byte{'\n'}))
//[]byte("#!/bin/bash\n\npackage_list=(\n\tlotem/rime-aoyu\n lotem/rime-bopomofo-script\n)")
sep := getSep()
idx := bytes.Index(bits, sep)
if idx < 0 {
return strs, errors.New("not a valid package.conf")
}
tmp := make([]byte, 80)
var j, m int
for i := idx + len(sep); i < len(bits); i++ {
switch bits[i] {
case '\n':
if j > 0 {
strs = append(strs, string(tmp[len(tmp)-j:]))
m++
tmp = []byte{}
j = 0
}
case '\t', ' ', '\r', ')':
continue
default:
tmp = append(tmp, bits[i])
j++
}
}
return strs[len(strs)-m:], nil
}
// ParseRemotePackageConf parse remote rime package.conf
func ParseRemotePackageConf(str string) ([]string, error) {
var strs []string
url, err := validateRemotePackageConf(str)
if err != nil {
return strs, err
}
resp, err := CLIENT.Get(url)
if err != nil {
return strs, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return strs, fmt.Errorf("%s not fetched, status %d", url, resp.StatusCode)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return strs, err
}
strs, err = parsePackageConf(b)
if err != nil {
return strs, err
}
return strs, nil
}