-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
225 lines (192 loc) · 6 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
package main
import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/alexflint/go-arg"
"github.com/pelletier/go-toml/v2"
"golang.design/x/clipboard"
"gopkg.in/validator.v2"
)
type OutputType string
const (
O_Stdout OutputType = "stdout"
O_Clipboard = "clipboard"
O_ClipboardMarkdown = "clipboard-markdown"
)
const kDefaultBranch = "master"
const kClipboardPlaceholder = ":clipboard"
const kRepoURL = "https://github.com/pluveto/upgit"
var maxUploadSize = int64(5 * 1024 * 1024)
type CLIOptions struct {
LocalPaths []string `arg:"positional, required" placeholder:"FILE" help:"local file path to upload. :clipboard for uploading clipboard image"`
TargetDir string `arg:"-t,--target-dir" help:"upload file with original name to given directory. if not set, will use renaming rules"`
Verbose bool `arg:"-V,--verbose" help:"when set, output more details to help developers"`
SizeLimit *int64 `arg:"-s,--size-limit" help:"in bytes. overwrite default size limit (5MiB). 0 means no limit"`
Wait bool `arg:"-w,--wait" help:"when set, not exit after upload, util user press any key"`
Clean bool `arg:"-c,--clean" help:"when set, remove local file after upload"`
Raw bool `arg:"-r,--raw" help:"when set, output non-replaced raw url"`
OutputType OutputType `arg:"-o,--output-type" help:"output type, supports stdout, clipboard, clipboard-markdown" default:"stdout"`
}
func (CLIOptions) Description() string {
return "\n" +
"Upload anything to github repo and then get its link.\n" +
"For more information: " + kRepoURL + "\n"
}
type Config struct {
PAT string `toml:"pat" validate:"nonzero"`
Rename string `toml:"rename,omitempty"`
Replacements map[string]string `toml:"replacements,omitempty"`
Username string `toml:"username" validate:"nonzero"`
Repo string `toml:"repo" validate:"nonzero"`
Branch string `toml:"branch,omitempty"`
}
var opt CLIOptions
var cfg Config = Config{Branch: kDefaultBranch}
func main() {
// parse cli args
arg.MustParse(&opt)
opt.TargetDir = strings.Trim(opt.TargetDir, "/")
if opt.SizeLimit != nil && *opt.SizeLimit >= 0 {
maxUploadSize = *opt.SizeLimit
}
GVerbose.Enabled = opt.Verbose
GVerbose.TraceStruct(opt)
// load config
loadEnvConfig(&cfg)
loadTomlConfig(&cfg)
cfg.Rename = strings.Trim(cfg.Rename, "/")
cfg.Rename = RemoveFmtUnderscore(cfg.Rename)
GVerbose.TraceStruct(cfg)
// handle clipboard
loadClipboard(&opt)
// validating args
validArgs(cfg, opt)
// executing uploading
uploader := GithubUploader{Config: cfg, OnUploaded: onUploaded}
uploader.UploadAll(opt.LocalPaths, opt.TargetDir)
if opt.Wait {
fmt.Scanln()
}
return
}
func onUploaded(r Result[UploadRet]) {
if !r.Ok() && opt.OutputType == O_Stdout {
fmt.Println("Failed: " + r.err.Error())
return
}
if opt.Clean && r.value.Status != Ignored {
_ = os.Remove(r.value.LocalPath)
}
outputLink(r.value)
}
func outputLink(r UploadRet) {
var outUrl string
if opt.Raw {
outUrl = r.RawUrl
} else {
outUrl = r.Url
}
switch opt.OutputType {
case O_Stdout:
fmt.Println(outUrl)
case O_Clipboard:
clipboard.Write(clipboard.FmtText, []byte(outUrl))
case O_ClipboardMarkdown:
clipboard.Write(clipboard.FmtText, []byte(
fmt.Sprint("![", filepath.Base(outUrl), "](", outUrl, ")"),
))
}
}
func validArgs(cfg Config, opt CLIOptions) {
if errs := validator.Validate(cfg); errs != nil {
abortErr(fmt.Errorf("incorrect config: " + errs.Error()))
}
for _, path := range opt.LocalPaths {
if strings.HasPrefix(path, "http") {
continue
}
fs, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
abortErr(fmt.Errorf("invalid file to upload %s: no such file", path))
}
if err != nil {
abortErr(fmt.Errorf("invalid file to upload %s: %s", path, err.Error()))
}
if fs.Size() == 0 {
abortErr(fmt.Errorf("invalid file to upload %s: file size is zero", path))
}
if maxUploadSize != 0 && fs.Size() > maxUploadSize {
abortErr(fmt.Errorf("invalid file to upload %s: file size is larger than %d bytes", path, maxUploadSize))
}
}
}
func loadTomlConfig(cfg *Config) {
homeDir, err := os.UserHomeDir()
panicErr(err)
appDir, err := GetApplicationPath()
panicErr(err)
var configFiles = []string{
filepath.Join(homeDir, ".upgit.config.toml"),
filepath.Join(homeDir, ".upgit.toml"),
filepath.Join(appDir, "config.toml"),
}
for _, configFile := range configFiles {
if _, err := os.Stat(configFile); err != nil {
continue
}
optRawBytes, err := ioutil.ReadFile(configFile)
if err == nil {
err = toml.Unmarshal(optRawBytes, &cfg)
}
if err != nil {
abortErr(fmt.Errorf("invalid config: " + err.Error()))
}
return
}
}
func loadClipboard(opt *CLIOptions) {
if len(opt.LocalPaths) == 1 && strings.ToLower(opt.LocalPaths[0]) == kClipboardPlaceholder {
err := clipboard.Init()
if err != nil {
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 {
abortErr(fmt.Errorf("failed: no image in clipboard or unsupported format"))
}
os.WriteFile(tmpFileName, buf, os.FileMode(fs.ModePerm))
opt.LocalPaths[0] = tmpFileName
opt.Clean = true
}
}
func loadEnvConfig(cfg *Config) {
if nil == cfg {
abortErr(fmt.Errorf("unable to load env config: nil config"))
}
if pat, found := syscall.Getenv("GITHUB_TOKEN"); found {
cfg.PAT = pat
}
if pat, found := syscall.Getenv("UPGIT_TOKEN"); found {
cfg.PAT = pat
}
if rename, found := syscall.Getenv("UPGIT_RENAME"); found {
cfg.Rename = rename
}
if username, found := syscall.Getenv("UPGIT_USERNAME"); found {
cfg.Username = username
}
if repo, found := syscall.Getenv("UPGIT_REPO"); found {
cfg.Repo = repo
}
if branch, found := syscall.Getenv("UPGIT_BRANCH"); found {
cfg.Branch = branch
}
}