This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
175 lines (149 loc) · 3.44 KB
/
util.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
package main
import (
"errors"
"fmt"
"mime"
"net/http"
"net/url"
"os"
"strings"
"github.com/3JoB/ulib/fsutil"
"github.com/3JoB/ulib/path"
"github.com/go-resty/resty/v2"
"github.com/spf13/cast"
)
/*func Operation[T any](e bool, trueValue, falseValue T) T {
if e {
return trueValue
}
return falseValue
}*/
func operation_1(f []string, ym map[string]any) error {
if f[3] != "then" {
ErrPrintf("GMake2: Invalid operator at %v \n", f[3])
}
if f[4] == "null" {
return nil
}
run(ym, f[4])
return nil
}
func operation_2(f []string, ym map[string]any) error {
if len(f) != 7 {
return nil
}
if f[5] != "or" {
ErrPrintf("GMake2: Invalid operator at %v \n", f[5])
}
if f[6] == "null" {
return nil
}
run(ym, f[6])
return nil
}
func request(url string) *resty.Response {
if Client == nil {
Client = http.DefaultClient
}
resp, err := resty.NewWithClient(Client).R().
SetHeader("User-Agent", UserAgent).
Get(url)
checkError(err)
return resp
}
func write(path, v string) error {
return fsutil.TruncWrite(path, v)
}
func replace(v []string) string {
return strings.ReplaceAll(strings.Trim(fmt.Sprint(v), "[]"), " ", " ")
}
func split(v, r string) []string {
return strings.Split(v, r)
}
func checkError(err error) {
if err != nil {
ErrPrintf("GMake2: %v", err.Error())
}
}
func Println(a ...any) {
fmt.Println(a...)
}
func Printf(f string, v ...any) {
fmt.Printf(f, v...)
}
func Sprintf(f string, v ...any) string {
return fmt.Sprintf(f, v...)
}
func Sprintln(v ...any) string {
return fmt.Sprintln(v...)
}
func Errorf(format string, v ...any) error {
return fmt.Errorf(format, v...)
}
func ErrPrintln(a ...any) {
if debug {
panic(Sprintln(a...))
}
Println(a...)
Exit()
}
func ErrPrintf(format string, v ...any) {
if debug {
panic(Sprintf(format, v...))
}
Printf(format, v...)
Exit()
}
func (bin BinConfig) Error(err error) {
if err != nil {
if bin.YamlData == nil {
ErrPrintln(ErrCommand(bin.CommandLine, bin.CommandGroup, err, ""))
} else {
ErrPrintln(ErrCommand(bin.CommandLine, bin.CommandGroup, err, bin.YamlDataBin+" "+replace(bin.YamlData)))
}
}
}
func Errors(errs string) error {
return errors.New(errs)
}
func ErrCommand(line int, group, msg any, command string) error {
line++
if command == "" {
return Errorf("gmake2: %v\n Errored command group: %v\n Errored row count: %v", msg, group, line)
}
return Errorf("gmake2: %v\n Errored command group: %v\n Errored command: %v\n Errored row count: %v", msg, group, command, line)
}
func Exit() {
os.Exit(0)
}
// from: https://github.com/cavaliergopher/grab/v3
func guessFilename(resp *http.Response) (string, error) {
filename := resp.Request.URL.Path
if cd := resp.Header.Get("Content-Disposition"); cd != "" {
if _, params, err := mime.ParseMediaType(cd); err == nil {
if val, ok := params["filename"]; ok {
filename = val
} // else filename directive is missing.. fallback to URL.Path
}
}
// sanitize
if filename == "" || strings.HasSuffix(filename, "/") || strings.Contains(filename, "\x00") {
return "GMakeDL.tmp", nil
}
filename = path.Base(path.Clean("/" + filename))
if filename == "" || filename == "." || filename == "/" {
return "GMakeDL.tmp", nil
}
return filename, nil
}
func ImportProxy(v any) {
if v != nil {
u, err := url.Parse(cast.ToString(cfg["proxy"]))
checkError(err)
Client = &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(u)},
}
} else {
Client = http.DefaultClient
}
}