-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
398 lines (348 loc) · 7.42 KB
/
app.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
goruntime "runtime"
"strings"
"time"
clip "github.com/atotto/clipboard"
cp "github.com/otiai10/copy"
rt "github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
err string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
// Perform your setup here
a.ctx = ctx
}
// domReady is called after front-end resources have been loaded
func (a App) domReady(ctx context.Context) {
//
// Run the server for getting variable information.
//
go RunServer(&a, ctx)
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
func (a *App) Quit() {
rt.Quit(a.ctx)
}
type FileParts struct {
Dir string
Name string
Extension string
}
type FileInfo struct {
Dir string
Name string
Extension string
IsDir bool
Size int64
Modtime string
}
type Environment struct {
Name string `json:"name" binding:"required"`
EnvVar []string `json:"envVar" binding:"required"`
}
type ServerResponce struct {
Error string `json:"error"`
Result string `json:"result"`
}
func (a *App) SendHTTPQuery(method string, uri string, body string) string {
var result string
switch strings.Trim(method, " ") {
case "GET":
{
resp, err := http.Get(uri)
if err != nil {
print(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
print(err)
}
result = string(body)
}
case "POST":
{
resp, err := http.Post(uri, "application/json", bytes.NewBuffer([]byte(body+"\n")))
if err != nil {
print(err)
}
defer resp.Body.Close()
repBody, errdec := ioutil.ReadAll(resp.Body)
if errdec != nil {
print(errdec)
}
result = string(repBody)
}
case "PUT":
{
client := http.Client{}
// regardless of GET or POST, we can safely add the body
req, err := http.NewRequest("PUT", uri, bytes.NewBuffer([]byte(body+"\n")))
if err != nil {
return err.Error()
}
headers := map[string]string{}
headers["Method"] = "PUT"
headers["Content-Type"] = "application/json"
// for each header passed, add the header value to the request
for k, v := range headers {
req.Header.Set(k, v)
}
// finally, do the request
res, err := client.Do(req)
if err != nil {
return err.Error()
}
if res == nil {
return fmt.Sprint("error: calling %s returned empty response", uri)
}
responseData, err := io.ReadAll(res.Body)
if err != nil {
return err.Error()
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Sprintf("error calling %s:\nstatus: %s\nresponseData: %s", uri, res.Status, responseData)
}
result = string(responseData)
}
}
return result
}
func (a *App) SystemOpenFile(prog string) {
if a.FileExists(prog) {
var ArgsArray [2]string
var sar []string
ArgsArray[1] = prog
a.RunCommandLine("/usr/bin/open", ArgsArray[:], sar, "")
}
}
func (a *App) GetExecutable() string {
ex, err := os.Executable()
if err != nil {
a.err = err.Error()
}
return ex
}
func (a *App) Getwd() string {
wd, err := os.Getwd()
if err != nil {
a.err = err.Error()
}
return wd
}
func (a *App) ReadFile(path string) string {
a.err = ""
contents, err := os.ReadFile(path)
if err != nil {
a.err = err.Error()
}
return string(contents[:])
}
func (a *App) GetHomeDir() string {
a.err = ""
hdir, err := os.UserHomeDir()
if err != nil {
a.err = err.Error()
}
return hdir
}
func (a *App) WriteFile(path string, data string) {
err := os.WriteFile(path, []byte(data), 0666)
if err != nil {
a.err = err.Error()
}
}
func (a *App) FileExists(path string) bool {
a.err = ""
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func (a *App) DirExists(path string) bool {
a.err = ""
dstat, err := os.Stat(path)
if err != nil {
a.err = err.Error()
return false
}
return dstat.IsDir()
}
func (a *App) SplitFile(path string) FileParts {
a.err = ""
var parts FileParts
parts.Dir, parts.Name = filepath.Split(path)
parts.Extension = filepath.Ext(path)
return parts
}
func (a *App) ReadDir(path string) []FileInfo {
a.err = ""
var result []FileInfo
result = make([]FileInfo, 0, 0)
files, err := ioutil.ReadDir(path)
if err != nil {
a.err = err.Error()
} else {
for _, file := range files {
var fileInfo FileInfo
fileInfo.Name = file.Name()
fileInfo.Size = file.Size()
fileInfo.IsDir = file.IsDir()
fileInfo.Modtime = file.ModTime().Format(time.ANSIC)
fileInfo.Dir = path
fileInfo.Extension = filepath.Ext(file.Name())
result = append(result, fileInfo)
}
}
return result
}
func (a *App) MakeDir(path string) {
a.err = ""
err := os.MkdirAll(path, 0755)
if err != nil {
a.err = err.Error()
}
}
func (a *App) MakeFile(path string) {
a.err = ""
a.WriteFile(path, "")
}
func (a *App) MoveEntries(from string, to string) {
a.err = ""
err := os.Rename(from, to)
if err != nil {
a.err = err.Error()
}
}
func (a *App) RenameEntry(from string, to string) {
a.err = ""
err := os.Rename(from, to)
if err != nil {
a.err = err.Error()
}
}
func (a *App) GetError() string {
return a.err
}
func (a *App) CopyEntries(from string, to string) {
a.err = ""
info, err := os.Stat(from)
if os.IsNotExist(err) {
a.err = err.Error()
return
}
if info.IsDir() {
//
// It's a directory! Do a deap copy.
//
err := cp.Copy(from, to)
if err != nil {
a.err = err.Error()
return
}
} else {
//
// It's a file. Just copy it.
//
source, err := os.Open(from)
if err != nil {
a.err = err.Error()
return
}
defer source.Close()
destination, err := os.Create(to)
if err != nil {
a.err = err.Error()
return
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
a.err = err.Error()
}
}
}
func (a *App) DeleteEntries(path string) {
a.err = ""
err := os.RemoveAll(path)
if err != nil {
a.err = err.Error()
}
}
func (a *App) RunCommandLine(cmd string, args []string, env []string, dir string) string {
a.err = ""
cmdline := exec.Command(cmd)
cmdline.Args = args
cmdline.Env = env
cmdline.Dir = dir
result, err := cmdline.CombinedOutput()
if err != nil {
a.err = err.Error()
}
return string(result[:])
}
func (a *App) GetClip() string {
result, err := clip.ReadAll()
if err != nil {
a.err = err.Error()
}
return result
}
func (a *App) SetClip(msg string) {
err := clip.WriteAll(msg)
if err != nil {
a.err = err.Error()
}
}
func (a *App) GetEnvironment() []string {
return os.Environ()
}
func (a *App) GetEnv(name string) string {
return os.Getenv(name)
}
func (a *App) AppendPath(dir string, name string) string {
return filepath.Join(dir, name)
}
func (a *App) GetOSName() string {
os := goruntime.GOOS
result := ""
switch os {
case "windows":
result = "windows"
break
case "darwin":
result = "macos"
case "linux":
result = "linux"
default:
result = fmt.Sprintf("%s", os)
}
return result
}