-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
225 lines (202 loc) · 4.49 KB
/
util.go
File metadata and controls
225 lines (202 loc) · 4.49 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
func getJSON[T any](ctx context.Context, rawURL string) (T, error) {
var out T
err := getJSONInto(ctx, rawURL, &out)
return out, err
}
func getJSONInto(ctx context.Context, rawURL string, out any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return err
}
req.Header.Set("accept", "application/json")
req.Header.Set("user-agent", "CodexPlusPlus-GoManager/"+version)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("HTTP %d", resp.StatusCode)
}
return json.NewDecoder(resp.Body).Decode(out)
}
func getBytes(ctx context.Context, rawURL string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("user-agent", "CodexPlusPlus-GoManager/"+version)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
func openURL(rawURL string) error {
return openPath(rawURL)
}
func openPath(path string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", path)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", path)
default:
cmd = exec.Command("xdg-open", path)
}
hideSubprocessWindow(cmd)
return cmd.Start()
}
func promptPath(title string, directory bool) string {
if runtime.GOOS == "darwin" {
choose := "file"
if directory {
choose = "folder"
}
script := fmt.Sprintf(`POSIX path of (choose %s with prompt %q)`, choose, title)
cmd := exec.Command("osascript", "-e", script)
hideSubprocessWindow(cmd)
out, err := cmd.Output()
if err == nil {
return strings.TrimSpace(string(out))
}
}
return ""
}
func readJSON(path string, out any) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(data, out)
}
func remarshal(in any, out any) error {
data, err := json.Marshal(in)
if err != nil {
return err
}
return json.Unmarshal(data, out)
}
func mapArg(args map[string]any, key string) map[string]any {
value, _ := args[key].(map[string]any)
if value == nil {
return map[string]any{}
}
return value
}
func stringArg(args map[string]any, key string) string {
return strings.TrimSpace(stringFromAny(args[key]))
}
func boolArg(args map[string]any, key string) bool {
return boolFromAny(args[key])
}
func intArg(args map[string]any, key string, fallback int) int {
switch value := args[key].(type) {
case float64:
return int(value)
case int:
return value
case string:
if parsed, err := strconv.Atoi(value); err == nil {
return parsed
}
}
return fallback
}
func uint16Arg(args map[string]any, key string, fallback uint16) uint16 {
value := intArg(args, key, int(fallback))
if value <= 0 || value > 65535 {
return fallback
}
return uint16(value)
}
func stringFromAny(value any) string {
switch typed := value.(type) {
case string:
return typed
case fmt.Stringer:
return typed.String()
case nil:
return ""
default:
return fmt.Sprint(typed)
}
}
func boolFromAny(value any) bool {
switch typed := value.(type) {
case bool:
return typed
case string:
return typed == "true" || typed == "1"
default:
return false
}
}
func uint64FromAny(value any, fallback uint64) uint64 {
switch typed := value.(type) {
case float64:
return uint64(typed)
case uint64:
return typed
case int:
return uint64(typed)
case string:
if parsed, err := strconv.ParseUint(typed, 10, 64); err == nil {
return parsed
}
}
return fallback
}
func firstString(values ...any) string {
for _, value := range values {
if text := strings.TrimSpace(stringFromAny(value)); text != "" {
return text
}
}
return ""
}
func nullableString(value string) any {
if strings.TrimSpace(value) == "" {
return nil
}
return value
}
func minRunes(value string, max int) int {
count := 0
for range value {
if count >= max {
return count
}
count++
}
return count
}
func urlPathUnescape(value string) (string, error) {
return strings.ReplaceAll(value, "%2F", "/"), nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func isDir(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}