-
Notifications
You must be signed in to change notification settings - Fork 31
/
parse.go
84 lines (79 loc) · 2.54 KB
/
parse.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
// Copyright (c) 2020-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
)
func (nm *Nami) Parse(input string) (string, string, string, error) {
if strings.HasSuffix(input, ".tengo") && (strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) {
n := input[strings.LastIndex(input, "/")+1:]
n = strings.TrimSuffix(n, ".tengo")
res, err := http.Get(input)
if err != nil {
return "", "", "", err
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return "", "", "", err
}
if res.StatusCode != 200 {
return "", "", "", errors.New("Package not found")
}
return n, string(b), "", nil
}
if strings.HasSuffix(input, ".tengo") && !(strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) {
n := input[strings.LastIndex(input, "/")+1:]
n = strings.TrimSuffix(n, ".tengo")
b, err := ioutil.ReadFile(input)
if err != nil {
return "", "", "", err
}
return n, string(b), "", nil
}
if strings.HasSuffix(input, ".js") {
n := input[strings.LastIndex(input, "/")+1:]
n = strings.TrimSuffix(n, ".js")
return n, "", input, nil
}
res0, err := http.Get("https://raw.githubusercontent.com/txthinking/nami/master/package/" + input + ".js")
if err != nil {
return "", "", "", err
}
defer res0.Body.Close()
io.Copy(ioutil.Discard, res0.Body)
if res0.StatusCode == 200 {
return input, "", "https://raw.githubusercontent.com/txthinking/nami/master/package/" + input + ".js", nil
}
if res0.StatusCode != 404 {
return "", "", "", errors.New(res0.Status)
}
res, err := http.Get("https://raw.githubusercontent.com/txthinking/nami/master/package/" + input + ".tengo")
if err != nil {
return "", "", "", err
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return "", "", "", err
}
if res.StatusCode != 200 {
return "", "", "", errors.New(res.Status + ": " + string(b))
}
return input, string(b), "", nil
}