-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
506 lines (463 loc) · 14.7 KB
/
main.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"github.com/alexflint/go-arg"
"github.com/pelletier/go-toml/v2"
"github.com/pluveto/upgit/lib/model"
"github.com/pluveto/upgit/lib/qcloudcos"
"github.com/pluveto/upgit/lib/result"
"github.com/pluveto/upgit/lib/s3"
"github.com/pluveto/upgit/lib/uploaders"
"github.com/pluveto/upgit/lib/upyun"
"github.com/pluveto/upgit/lib/xapp"
"github.com/pluveto/upgit/lib/xclipboard"
"github.com/pluveto/upgit/lib/xext"
"github.com/pluveto/upgit/lib/xgithub"
"github.com/pluveto/upgit/lib/xhttp"
"github.com/pluveto/upgit/lib/xio"
"github.com/pluveto/upgit/lib/xlog"
"github.com/pluveto/upgit/lib/xmap"
"github.com/pluveto/upgit/lib/xpath"
"github.com/pluveto/upgit/lib/xstrings"
"github.com/pluveto/upgit/lib/xzip"
"golang.design/x/clipboard"
"gopkg.in/validator.v2"
)
func main() {
result.AbortErr = xlog.AbortErr
if len(os.Args) >= 2 && os.Args[1] == "ext" {
extSubcommand()
return
}
mainCommand()
}
func mainCommand() {
// parse cli args
loadCliOpts()
// load config
loadEnvConfig(&xapp.AppCfg)
loadConfig(&xapp.AppCfg)
xlog.GVerbose.TraceStruct(xapp.AppCfg)
// handle clipboard if need
handleClipboard()
// validating args
validArgs()
// executing uploading
dispatchUploader()
if xapp.AppOpt.Wait {
fmt.Scanln()
}
}
// loadCliOpts load cli options into xapp.AppOpt
func loadCliOpts() {
arg.MustParse(&xapp.AppOpt)
xapp.AppOpt.TargetDir = strings.Trim(xapp.AppOpt.TargetDir, "/")
xapp.AppOpt.ApplicationPath = strings.Trim(xapp.AppOpt.ApplicationPath, "/")
if len(xapp.AppOpt.ApplicationPath) > 0 {
xpath.ApplicationPath = xapp.AppOpt.ApplicationPath
}
if xapp.AppOpt.SizeLimit != nil && *xapp.AppOpt.SizeLimit >= 0 {
xapp.MaxUploadSize = *xapp.AppOpt.SizeLimit
}
if false == xapp.AppOpt.NoLog {
xlog.GVerbose.LogEnabled = true
xlog.GVerbose.LogFile = xpath.MustGetApplicationPath("upgit.log")
xlog.GVerbose.LogFileMaxSize = 2 * 1024 * 1024 // 2MiB
xlog.GVerbose.Info("Started")
xlog.GVerbose.TruncatLog()
}
xlog.GVerbose.VerboseEnabled = xapp.AppOpt.Verbose
xlog.GVerbose.TraceStruct(xapp.AppOpt)
}
func onUploaded(r result.Result[*model.Task]) {
if !r.Ok() && xapp.AppOpt.OutputType == xapp.O_Stdout {
fmt.Println("Failed: " + r.Err.Error())
return
}
if xapp.AppOpt.Clean && !r.Value.Ignored {
err := os.Remove(r.Value.LocalPath)
if err != nil {
xlog.GVerbose.Info("Failed to remove %s: %s", r.Value.LocalPath, err.Error())
} else {
xlog.GVerbose.Info("Removed %s", r.Value.LocalPath)
}
}
outputLink(*r.Value)
recordHistory(*r.Value)
}
func mustMarshall(s interface{}) string {
b, err := toml.Marshal(s)
if err != nil {
return ""
}
return string(b)
}
func recordHistory(r model.Task) {
xio.AppendToFile(xpath.MustGetApplicationPath("history.log"), []byte(
`{"time":"`+time.Now().Local().String()+`","rawUrl":"`+r.RawUrl+`","url":"`+r.Url+`"}`+"\n"),
)
xlog.GVerbose.Info(mustMarshall(r))
}
func outputLink(r model.Task) {
outContent, err := outputFormat(r)
xlog.AbortErr(err)
switch xapp.AppOpt.OutputType {
case xapp.O_Stdout:
fmt.Println(outContent)
case xapp.O_Clipboard:
clipboard.Write(clipboard.FmtText, []byte(outContent))
default:
xlog.AbortErr(errors.New("unknown output type: " + string(xapp.AppOpt.OutputType)))
}
}
func outputFormat(r model.Task) (content string, err error) {
var outUrl string
if xapp.AppOpt.Raw || r.Url == "" {
outUrl = r.RawUrl
} else {
outUrl = r.Url
}
fmt := xapp.AppOpt.OutputFormat
if fmt == "" {
return outUrl, nil
}
val, ok := xapp.AppCfg.OutputFormats[fmt]
if !ok {
return "", errors.New("unknown output format: " + fmt)
}
content = strings.NewReplacer(
"{url}", outUrl,
"{urlfname}", filepath.Base(outUrl),
"{fname}", filepath.Base(r.LocalPath),
).Replace(xstrings.RemoveFmtUnderscore(val))
return
}
func validArgs() {
if errs := validator.Validate(xapp.AppCfg); errs != nil {
xlog.AbortErr(fmt.Errorf("incorrect config: " + errs.Error()))
}
for _, path := range xapp.AppOpt.LocalPaths {
if strings.HasPrefix(path, "http") {
continue
}
fs, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
xlog.AbortErr(fmt.Errorf("invalid file to upload %s: no such file", path))
}
if err != nil {
xlog.AbortErr(fmt.Errorf("invalid file to upload %s: %s", path, err.Error()))
}
if fs.Size() == 0 {
xlog.AbortErr(fmt.Errorf("invalid file to upload %s: file size is zero", path))
}
if xapp.MaxUploadSize != 0 && fs.Size() > xapp.MaxUploadSize {
xlog.AbortErr(fmt.Errorf("invalid file to upload %s: file size is larger than %d bytes", path, xapp.MaxUploadSize))
}
}
}
// loadConfig loads config from config file to xapp.AppCfg
func loadConfig(cfg *xapp.Config) {
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = ""
}
appDir := xpath.MustGetApplicationPath("")
var configFiles = map[string]bool{
filepath.Join(homeDir, ".upgit.config.toml"): false,
filepath.Join(homeDir, filepath.Join(".config", "upgitrc")): false,
filepath.Join(appDir, "config.toml"): false,
filepath.Join(appDir, "upgit.toml"): false,
}
if xapp.AppOpt.ConfigFile != "" {
configFiles[xapp.AppOpt.ConfigFile] = true
}
for configFile, required := range configFiles {
if _, err := os.Stat(configFile); err != nil {
if required {
xlog.AbortErr(fmt.Errorf("config file %s not found", configFile))
}
continue
}
optRawBytes, err := ioutil.ReadFile(configFile)
if err == nil {
err = toml.Unmarshal(optRawBytes, &cfg)
}
if err != nil {
xlog.AbortErr(fmt.Errorf("invalid config: " + err.Error()))
}
xapp.ConfigFilePath = configFile
break
}
if xapp.ConfigFilePath == "" {
xlog.AbortErr(fmt.Errorf("no config file found"))
}
// fill config
xapp.AppCfg.Rename = strings.Trim(xapp.AppCfg.Rename, "/")
xapp.AppCfg.Rename = xstrings.RemoveFmtUnderscore(xapp.AppCfg.Rename)
// -- integrated formats
if nil == xapp.AppCfg.OutputFormats {
xapp.AppCfg.OutputFormats = make(map[string]string)
}
xapp.AppCfg.OutputFormats["markdown"] = `![{url_fname}]({url})`
xapp.AppCfg.OutputFormats["url"] = `{url}`
}
// UploadAll will upload all given file to targetDir.
// If targetDir is not set, it will upload using rename rules.
func UploadAll(uploader model.Uploader, localPaths []string, targetDir string, callback func(result.Result[*model.Task])) {
for taskId, localPath := range localPaths {
var ret result.Result[*model.Task]
task := model.Task{
Status: model.TASK_CREATED,
TaskId: taskId,
LocalPath: localPath,
TargetDir: targetDir,
RawUrl: "",
Url: "",
CreateTime: time.Now(),
}
var err error
// ignore non-local path
if strings.HasPrefix(localPath, "http") {
task.Ignored = true
task.Status = model.TASK_FINISHED
} else {
err = uploader.Upload(&task)
}
if err != nil {
task.Status = model.TASK_FAILED
ret = result.Result[*model.Task]{
Err: err,
}
} else {
ret = result.Result[*model.Task]{
Value: &task,
}
}
if err == nil {
xlog.GVerbose.TraceStruct(ret.Value)
}
if nil != callback {
callback(ret)
}
}
}
func dispatchUploader() {
uploaderId := xstrings.ValueOrDefault(xapp.AppOpt.Uploader, xapp.AppCfg.DefaultUploader)
xlog.GVerbose.Info("uploader: " + uploaderId)
if uploaderId == "github" {
gCfg, err := xapp.LoadUploaderConfig[uploaders.GithubUploaderConfig](uploaderId)
xlog.AbortErr(err)
err = validator.Validate(&gCfg)
xlog.AbortErr(err)
if len(gCfg.Branch) == 0 {
gCfg.Branch = xapp.DefaultBranch
}
uploader := uploaders.GithubUploader{Config: gCfg}
UploadAll(uploader, xapp.AppOpt.LocalPaths, xapp.AppOpt.TargetDir, onUploaded)
return
}
if uploaderId == "qcloudcos" {
qCfg, err := xapp.LoadUploaderConfig[qcloudcos.COSConfig](uploaderId)
xlog.AbortErr(err)
err = validator.Validate(&qCfg)
xlog.AbortErr(err)
xlog.GVerbose.Trace("qcloudcos config: ")
xlog.GVerbose.TraceStruct(&qCfg)
uploader := qcloudcos.COSUploader{Config: qCfg}
UploadAll(uploader, xapp.AppOpt.LocalPaths, xapp.AppOpt.TargetDir, onUploaded)
return
}
if uploaderId == "upyun" {
ucfg, err := xapp.LoadUploaderConfig[upyun.UpyunConfig](uploaderId)
xlog.AbortErr(err)
err = validator.Validate(&ucfg)
xlog.AbortErr(err)
xlog.GVerbose.Trace("qcloudcos config: ")
xlog.GVerbose.TraceStruct(&ucfg)
uploader := upyun.UpyunUploader{Config: ucfg}
UploadAll(uploader, xapp.AppOpt.LocalPaths, xapp.AppOpt.TargetDir, onUploaded)
return
}
if uploaderId == "s3" {
ucfg, err := xapp.LoadUploaderConfig[s3.S3Config](uploaderId)
xlog.AbortErr(err)
err = validator.Validate(&ucfg)
xlog.AbortErr(err)
xlog.GVerbose.Trace("qcloudcos config: ")
xlog.GVerbose.TraceStruct(&ucfg)
uploader, err := s3.NewS3Uploader(ucfg)
xlog.AbortErr(err)
UploadAll(uploader, xapp.AppOpt.LocalPaths, xapp.AppOpt.TargetDir, onUploaded)
return
}
// try http simple uploader
// list file in ./extensions
extDir := xpath.MustGetApplicationPath("extensions")
info, err := ioutil.ReadDir(extDir)
xlog.AbortErr(err)
var uploader *uploaders.SimpleHttpUploader
for _, f := range info {
fname := f.Name()
xlog.GVerbose.Trace("found file %s", fname)
if !strings.HasSuffix(fname, ".json") && !strings.HasSuffix(fname, ".jsonc") {
xlog.GVerbose.Trace("ignored file %s", fname)
continue
}
// load file to json
uploaderDef, err := xext.GetExtDefinitionInterface(extDir, fname)
xlog.AbortErr(err)
if result.From[string](xmap.GetDeep[string](uploaderDef, `meta.id`)).ValueOrExit() != uploaderId {
continue
}
if result.From[string](xmap.GetDeep[string](uploaderDef, "meta.type")).ValueOrExit() != "simple-http-uploader" {
continue
}
uploader = &uploaders.SimpleHttpUploader{Definition: uploaderDef}
extConfig, err := xapp.LoadUploaderConfig[map[string]interface{}](uploaderId)
if err == nil {
uploader.Config = extConfig
xlog.GVerbose.Trace("uploader config:")
xlog.GVerbose.TraceStruct(uploader.Config)
} else {
xlog.GVerbose.Trace("no uploader config found")
}
break
}
if nil == uploader {
xlog.AbortErr(errors.New("unknown uploader: " + uploaderId))
}
UploadAll(uploader, xapp.AppOpt.LocalPaths, xapp.AppOpt.TargetDir, onUploaded)
}
func handleClipboard() {
if len(xapp.AppOpt.LocalPaths) == 1 {
label := strings.ToLower(xapp.AppOpt.LocalPaths[0])
if label == xapp.ClipboardPlaceholder {
err := clipboard.Init()
if err != nil {
xlog.AbortErr(fmt.Errorf("failed to init clipboard: " + err.Error()))
}
tmpFileName := fmt.Sprint(os.TempDir(), "/upgit_tmp_", time.Now().UnixMicro(), ".png")
buf := clipboard.Read(clipboard.FmtImage)
if nil == buf {
// try second chance for Windows user. To adapt bitmap format (compatible with Snipaste)
if runtime.GOOS == "windows" {
buf, err = xclipboard.ReadClipboardImage()
}
if err != nil {
xlog.GVerbose.Error("failed to read clipboard image: " + err.Error())
}
}
if nil == buf {
xlog.AbortErr(fmt.Errorf("failed: no image in clipboard or unsupported format"))
}
os.WriteFile(tmpFileName, buf, os.FileMode(fs.ModePerm))
xapp.AppOpt.LocalPaths[0] = tmpFileName
xapp.AppOpt.Clean = true
}
if strings.HasPrefix(label, xapp.ClipboardFilePlaceholder) {
// Must be Windows
if runtime.GOOS != "windows" {
xlog.AbortErr(fmt.Errorf("failed: clipboard file only supported on Windows"))
}
// Download latest https://github.com/pluveto/APIProxy-Win32/releases
// and put it in same directory with upgit.exe
download := func() {
downloadUrl, err := xgithub.GetLatestReleaseDownloadUrl("pluveto/APIProxy-Win32")
xlog.AbortErr(err)
xlog.GVerbose.Trace("download url: %s", downloadUrl)
saveName := xpath.MustGetApplicationPath("/apiproxy-win32.zip")
xlog.AbortErr(xhttp.DownloadFile(downloadUrl, saveName))
// Unzip
xlog.AbortErr(xzip.Unzip(saveName, xpath.MustGetApplicationPath("/")))
// Clean downloaded zip
xlog.AbortErr(os.Remove(saveName))
}
// Run
executable := xpath.MustGetApplicationPath("APIProxy.exe")
if _, err := os.Stat(executable); os.IsNotExist(err) {
println("APIProxy not found, downloading...")
download()
}
execArgs := []string{"clipboard", "GetFilePaths"}
cmd := exec.Command(executable)
cmd.Args = append(cmd.Args, execArgs...)
// Wait and fetch cmdOutput
cmdOutput, err := cmd.Output()
if err != nil {
xlog.AbortErr(fmt.Errorf("failed to run APIProxy: %s, stderr: %s", err.Error(), cmdOutput))
}
parseOutput := func(output string) []string {
/*
Type: ApplicationError
Msg: No handler name specified
HMsg:
Data: null
*/
lines := strings.Split(output, "\n")
for i, line := range lines {
lines[i] = strings.TrimSpace(line)
if len(lines[i]) == 0 {
lines = append(lines[:i], lines[i+1:]...)
}
}
var result []string
if len(lines) != 4 {
xlog.AbortErr(errors.New("unable to parse APIProxy output, unexpected line count. output: " + output))
return result
}
if !strings.HasPrefix(lines[0], "Type: Success") {
xlog.AbortErr(errors.New("got error from APIProxy output: " + output))
return result
}
// Parse data
jsonStr := lines[3][len("Data: "):]
var paths []string
xlog.AbortErr(json.Unmarshal([]byte(jsonStr), &paths))
return paths
}
xlog.GVerbose.Trace("stdout: %s", cmdOutput)
paths := parseOutput(string(cmdOutput))
if len(paths) == 0 {
xlog.AbortErr(errors.New("no file in clipboard"))
}
xapp.AppOpt.LocalPaths = paths
}
}
}
func loadEnvConfig(cfg *xapp.Config) {
if nil == cfg {
xlog.AbortErr(fmt.Errorf("unable to load env config: nil config"))
}
if rename, found := syscall.Getenv("UPGIT_RENAME"); found {
cfg.Rename = rename
}
}
func loadGithubUploaderEnvConfig(gCfg *uploaders.GithubUploaderConfig) {
// TODO: Auto generate env key name and adapt for all uploaders
if pat, found := syscall.Getenv("GITHUB_TOKEN"); found {
gCfg.PAT = pat
}
if pat, found := syscall.Getenv("UPGIT_TOKEN"); found {
gCfg.PAT = pat
}
if username, found := syscall.Getenv("UPGIT_USERNAME"); found {
gCfg.Username = username
}
if repo, found := syscall.Getenv("UPGIT_REPO"); found {
gCfg.Repo = repo
}
if branch, found := syscall.Getenv("UPGIT_BRANCH"); found {
gCfg.Branch = branch
}
}