-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
289 lines (266 loc) · 8.9 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
// Package main is the main package of lega-commander command-line tool, containing "files", "resumables" and "uploads"
// commands implementations along with additional helper methods.
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"strings"
"text/tabwriter"
"github.com/elixir-oslo/lega-commander/files"
"github.com/elixir-oslo/lega-commander/resuming"
"github.com/elixir-oslo/lega-commander/streaming"
"github.com/jessevdk/go-flags"
aurora "github.com/logrusorgru/aurora/v3"
)
var (
version = "dev"
date = "unknown"
)
const (
inboxCommand = "inbox"
outboxCommand = "outbox"
resumablesCommand = "resumables"
uploadCommand = "upload"
downloadCommand = "download"
)
var inboxOptions struct {
List bool `short:"l" long:"list" description:"Lists uploaded files"`
Delete string `short:"d" long:"delete" description:"Deletes uploaded file by name"`
}
var inboxOptionsParser = flags.NewParser(&inboxOptions, flags.None)
var outboxOptions struct {
List bool `short:"l" long:"list" description:"Lists exported files"`
}
var outboxOptionsParser = flags.NewParser(&outboxOptions, flags.None)
var resumablesOptions struct {
List bool `short:"l" long:"list" description:"Lists resumable uploads"`
Delete string `short:"d" long:"delete" description:"Deletes resumable upload by ID"`
}
var resumablesOptionsParser = flags.NewParser(&resumablesOptions, flags.None)
var uploadingOptions struct {
FileName string `short:"f" long:"file" description:"File or folder to upload" value-name:"FILE" required:"true"`
Resume bool `short:"r" long:"resume" description:"Resumes interrupted upload"`
Straight bool `short:"b" long:"beta" description:"Upload the files without the proxy service;i.e. directly to tsd file api"`
}
var uploadingOptionsParser = flags.NewParser(&uploadingOptions, flags.None)
var downloadingOptions struct {
FileName string `short:"f" long:"file" description:"File to download\t[optional]"`
Straight bool `short:"b" long:"beta" description:"download the files without the proxy service;i.e. directly from tsd file api"`
}
var downloadingOptionsParser = flags.NewParser(&downloadingOptions, flags.None)
const (
usageString = "Usage:\n lega-commander\n"
applicationOptions = "Application Options"
)
func main() {
args := os.Args
if len(args) == 1 || args[1] == "-h" || args[1] == "--help" {
fmt.Println(generateHelpMessage())
os.Exit(0)
}
if args[1] == "-v" || args[1] == "--version" {
fmt.Println(aurora.Blue(version))
fmt.Println(aurora.Yellow(date))
os.Exit(0)
}
fileManager, err := files.NewFileManager(nil)
if err != nil {
log.Fatal(aurora.Red(err))
}
commandName := args[1]
switch commandName {
case inboxCommand:
_, err := inboxOptionsParser.Parse()
if err != nil {
log.Fatal(aurora.Red(err))
}
if inboxOptions.List {
fileList, err := fileManager.ListFiles(true)
if err != nil {
if _, ok := err.(*files.FolderNotFoundError); ok {
log.Fatal(aurora.Red("Inbox Error: The user folder is empty or does not exist yet"))
}
}
if err != nil {
log.Fatal(aurora.Red(err))
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
_, err = fmt.Fprintln(tw, aurora.Blue("File name\t File size"))
if err != nil {
log.Fatal(aurora.Red(err))
}
for _, file := range *fileList {
_, err = fmt.Fprintln(tw, aurora.Blue(file.FileName+"\t "+strconv.FormatInt(file.Size, 10)+" bytes"))
if err != nil {
log.Fatal(aurora.Red(err))
}
}
err = tw.Flush()
if err != nil {
log.Fatal(aurora.Red(err))
}
} else if inboxOptions.Delete != "" {
err = fileManager.DeleteFile(inboxOptions.Delete)
if err != nil {
log.Fatal(aurora.Red(err))
} else {
fmt.Println(aurora.Green("Success"))
}
} else {
log.Fatal(aurora.Red("none of the flags are selected"))
}
case outboxCommand:
_, err := inboxOptionsParser.Parse()
if err != nil {
log.Fatal(aurora.Red(err))
}
if inboxOptions.List {
fileList, err := fileManager.ListFiles(false)
if err != nil {
if _, ok := err.(*files.FolderNotFoundError); ok {
log.Fatal(aurora.Red("Outbox Error: No data has been staged in the outbox yet"))
}
}
if err != nil {
log.Fatal(aurora.Red(err))
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
_, err = fmt.Fprintln(tw, aurora.Blue("File name\t File size"))
if err != nil {
log.Fatal(aurora.Red(err))
}
for _, file := range *fileList {
_, err = fmt.Fprintln(tw, aurora.Blue(file.FileName+"\t "+strconv.FormatInt(file.Size, 10)+" bytes"))
if err != nil {
log.Fatal(aurora.Red(err))
}
}
err = tw.Flush()
if err != nil {
log.Fatal(aurora.Red(err))
}
} else if inboxOptions.Delete != "" {
err = fileManager.DeleteFile(inboxOptions.Delete)
if err != nil {
log.Fatal(aurora.Red(err))
} else {
fmt.Println(aurora.Green("Success"))
}
} else {
log.Fatal(aurora.Red("none of the flags are selected"))
}
case resumablesCommand:
_, err := resumablesOptionsParser.Parse()
if err != nil {
log.Fatal(aurora.Red(err))
}
resumablesManager, err := resuming.NewResumablesManager(nil)
if err != nil {
log.Fatal(aurora.Red(err))
}
if resumablesOptions.List {
resumables, err := resumablesManager.ListResumables()
if err != nil {
log.Fatal(aurora.Red(err))
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
_, err = fmt.Fprintln(tw, aurora.Blue("File name\t File size\t Resumable ID"))
if err != nil {
log.Fatal(aurora.Red(err))
}
for _, resumable := range *resumables {
_, err := fmt.Fprintln(tw, aurora.Blue(resumable.Name+"\t "+strconv.FormatInt(resumable.Size, 10)+" bytes"+"\t "+resumable.ID))
if err != nil {
log.Fatal(aurora.Red(err))
}
}
err = tw.Flush()
if err != nil {
log.Fatal(aurora.Red(err))
}
} else if resumablesOptions.Delete != "" {
err = resumablesManager.DeleteResumable(resumablesOptions.Delete)
if err != nil {
log.Fatal(aurora.Red(err))
} else {
fmt.Println(aurora.Green("Success"))
}
} else {
log.Fatal(aurora.Red("none of the flags are selected"))
}
case uploadCommand:
_, err := uploadingOptionsParser.Parse()
if err != nil {
log.Fatal(aurora.Red(err))
}
streamer, err := streaming.NewStreamer(nil, nil, nil, uploadingOptions.Straight)
if err != nil {
log.Fatal(aurora.Red(err))
}
err = streamer.Upload(uploadingOptions.FileName, uploadingOptions.Resume, uploadingOptions.Straight)
if err != nil {
log.Fatal(aurora.Red(err))
}
case downloadCommand:
_, err := downloadingOptionsParser.Parse()
if err != nil {
log.Fatal(aurora.Red(err))
}
streamer, err := streaming.NewStreamer(nil, nil, nil, downloadingOptions.Straight)
if err != nil {
log.Fatal(aurora.Red(err))
}
if downloadingOptions.FileName == "" {
fmt.Println(aurora.Blue("File to export is not specified. Downloading the whole outbox folder."))
fileList, err := fileManager.ListFiles(false)
if err != nil {
log.Fatal(aurora.Red(err))
}
for _, file := range *fileList {
err = streamer.Download(file.FileName)
if err != nil {
log.Fatal(aurora.Red(err))
}
}
} else {
err = streamer.Download(downloadingOptions.FileName)
if err != nil {
log.Fatal(aurora.Red(err))
}
}
default:
log.Fatal(aurora.Red(fmt.Sprintf("command '%v' is not recognized", commandName)))
}
}
func generateHelpMessage() string {
header := "lega-commander [inbox | outbox | resumables | upload | download] <args>\n"
buf := bytes.Buffer{}
inboxOptionsParser.WriteHelp(&buf)
inboxUsage := buf.String()
inboxUsage = strings.Replace(inboxUsage, usageString, "", 1)
inboxUsage = strings.Replace(inboxUsage, applicationOptions, " "+inboxCommand, 1)
buf.Reset()
outboxOptionsParser.WriteHelp(&buf)
outboxUsage := buf.String()
outboxUsage = strings.Replace(outboxUsage, usageString, "", 1)
outboxUsage = strings.Replace(outboxUsage, applicationOptions, " "+outboxCommand, 1)
buf.Reset()
resumablesOptionsParser.WriteHelp(&buf)
resumablesUsage := buf.String()
resumablesUsage = strings.Replace(resumablesUsage, usageString, "", 1)
resumablesUsage = strings.Replace(resumablesUsage, applicationOptions, " "+resumablesCommand, 1)
buf.Reset()
uploadingOptionsParser.WriteHelp(&buf)
uploadingUsage := buf.String()
uploadingUsage = strings.Replace(uploadingUsage, usageString, "", 1)
uploadingUsage = strings.Replace(uploadingUsage, applicationOptions, " "+uploadCommand, 1)
buf.Reset()
downloadingOptionsParser.WriteHelp(&buf)
downloadingUsage := buf.String()
downloadingUsage = strings.Replace(downloadingUsage, usageString, "", 1)
downloadingUsage = strings.Replace(downloadingUsage, applicationOptions, " "+downloadCommand, 1)
return header + inboxUsage + outboxUsage + resumablesUsage + uploadingUsage + downloadingUsage
}