-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
463 lines (383 loc) · 10.8 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
package main
import (
"bufio"
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/aarlin/listbucketresult-downloader/client"
utils "github.com/aarlin/listbucketresult-downloader/utils"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
cursorStyle = focusedStyle.Copy()
noStyle = lipgloss.NewStyle()
helpStyle = blurredStyle.Copy()
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
focusedButton = focusedStyle.Copy().Render("[ Submit ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
focusedCursor = focusedStyle.Copy().Render(">")
checkedSelectionStyle = focusedStyle.Copy().Render("✓")
currentUrlStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211"))
doneStyle = lipgloss.NewStyle().Margin(1, 2)
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
failMark = lipgloss.NewStyle().Foreground(lipgloss.Color("9")).SetString("X")
)
type statusMsg int
type errMsg struct {
err error
}
func (e errMsg) Error() string { return e.err.Error() }
type item struct {
text string
checked bool
}
type GotResources struct {
Err error
Resources []string
}
type DownloadResourceResp struct {
Err error
Msg string
Index int
}
func (m model) Init() tea.Cmd {
m.preloadLastInputs()
return tea.Batch(
textinput.Blink,
m.spinner.Tick,
)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
m.saveInputs()
return m, tea.Quit
case "tab", "shift+tab", "enter", "up", "down", " ":
s := msg.String()
if m.showTypingView {
// Did the user press enter while the submit button was focused?
// If so, exit.
if s == "enter" && m.focusIndex == len(m.inputs) {
m.showTypingView = false
m.showLoadingView = true
m.focusIndex = 0
m.saveInputs()
return m, tea.Batch(
spinner.Tick,
m.fetchResources(),
)
}
// Cycle indexes
if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex > len(m.inputs) {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.inputs)
}
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
// Set focused state
cmds[i] = m.inputs[i].Focus()
m.inputs[i].PromptStyle = focusedStyle
m.inputs[i].TextStyle = focusedStyle
continue
}
// Remove focused state
m.inputs[i].Blur()
m.inputs[i].PromptStyle = noStyle
m.inputs[i].TextStyle = noStyle
}
return m, nil
}
}
case GotResources:
m.showLoadingView = false
m.showDownloadingView = true
if err := msg.Err; err != nil {
m.err = err
return m, tea.Batch(
tea.Printf("[GotResources]: error occured: %s\n", err),
)
}
m.resources = msg.Resources
progressCmd := m.progress.SetPercent(float64(m.downloadCount) / float64(len(m.resources)))
return m, tea.Batch(
progressCmd,
tea.Printf("%s %s", checkMark, m.resources[m.downloadCount]), // print success message above our program
m.listenForActivity(m.sub), // generate activity
waitForActivity(m.sub), // wait for activity
)
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case progress.FrameMsg:
newModel, cmd := m.progress.Update(msg)
if newModel, ok := newModel.(progress.Model); ok {
m.progress = newModel
}
return m, cmd
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
case DownloadResourceResp:
m.downloadCount = msg.Index + 1
if m.downloadCount >= len(m.resources) {
// Everything's been installed. We're done!
m.finishedDownloading = true
return m, tea.Quit
}
progressCmd := m.progress.SetPercent(float64(m.downloadCount) / float64(len(m.resources)))
if err := msg.Err; err != nil {
return m, tea.Batch(
progressCmd,
tea.Printf("%s %s", failMark, m.resources[m.downloadCount]), // print success message above our program
waitForActivity(m.sub), // wait for activity
)
}
return m, tea.Batch(
progressCmd,
tea.Printf("%s %s", checkMark, m.resources[m.downloadCount]), // print success message above our program
waitForActivity(m.sub), // wait for activity
)
}
cmd := m.updateInputs(msg)
return m, cmd
}
func (m model) View() string {
s := ""
if m.showLoadingView {
url := m.inputs[0].Value()
keyOffset := m.inputs[3].Value()
spinnerText := fmt.Sprintf("Retrieving resource paths from: %s and key offset: %s, please wait ... \n\n", url, keyOffset)
s += "\n" +
m.spinner.View() + spinnerText
}
if m.showDownloadingView {
n := len(m.resources)
w := lipgloss.Width(fmt.Sprintf("%d", n))
if n == 0 {
return doneStyle.Render(fmt.Sprintf("There were no resources downloaded.\n"))
}
if m.finishedDownloading {
return doneStyle.Render(fmt.Sprintf("Done! Downloaded %d resources.\n", n-1))
}
resourceCount := fmt.Sprintf(" %*d/%*d", w, m.downloadCount, w, n-1)
spin := m.spinner.View() + " "
prog := m.progress.View()
cellsAvail := utils.Max(0, m.width-lipgloss.Width(spin+prog+resourceCount))
url := currentUrlStyle.Render(m.resources[m.downloadCount])
info := lipgloss.NewStyle().MaxWidth(cellsAvail).Render("Downloading " + url)
cellsRemaining := utils.Max(0, m.width-lipgloss.Width(spin+info+prog+resourceCount))
gap := strings.Repeat(" ", cellsRemaining)
s += spin + info + gap + prog + resourceCount
}
if m.showTypingView {
s += "Fill out the following\n\n"
var b strings.Builder
for i := range m.inputs {
b.WriteString(m.inputs[i].View())
if i < len(m.inputs)-1 {
b.WriteRune('\n')
}
}
button := &blurredButton
if m.focusIndex == len(m.inputs) {
button = &focusedButton
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button)
s += b.String()
}
if m.showErrorView {
s += m.err.Error()
}
s += "\nPress q to quit.\n"
return s
}
func (m *model) listenForActivity(sub chan DownloadResourceResp) tea.Cmd {
return func() tea.Msg {
for {
if m.downloadIndex >= len(m.resources)-1 {
break
}
cookieUrl := m.inputs[1].Value()
folderDir := m.inputs[5].Value()
msg, err := m.client.DownloadResource(context.Background(), m.resources[m.downloadIndex], cookieUrl, folderDir)
sub <- DownloadResourceResp{Err: err, Msg: msg, Index: m.downloadIndex}
// m.inputs[3].SetValue(path.Base(m.resources[m.downloadIndex]))
m.downloadIndex++
}
return DownloadResourceResp{Err: nil, Msg: "", Index: m.downloadIndex}
}
}
// A command that waits for the activity on the channel.
func waitForActivity(sub chan DownloadResourceResp) tea.Cmd {
return func() tea.Msg {
return DownloadResourceResp(<-sub)
}
}
func (m *model) fetchResources() tea.Cmd {
bucketUrl := m.inputs[0].Value()
if bucketUrl[len(bucketUrl)-1:] != "/" {
bucketUrl += "/"
}
cookieUrl := m.inputs[1].Value()
bucketQuery := buildBucketQuery(m.inputs)
ignoreText := m.inputs[4].Value()
return func() tea.Msg {
resources, err := m.client.SearchBucket(context.Background(), bucketUrl, bucketQuery, cookieUrl, ignoreText)
if err != nil {
return GotResources{Err: err, Resources: resources}
}
return GotResources{Resources: resources}
}
}
func (m *model) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.inputs))
// Only text inputs with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (m *model) saveInputs() {
// Create or overwrite the file
file, err := os.Create("last-inputs.txt")
if err != nil {
panic(err)
}
defer file.Close()
// Write each element of the slice in a new line
w := bufio.NewWriter(file)
for _, line := range m.inputs {
_, err := w.WriteString(line.Value() + "\n")
if err != nil {
panic(err)
}
}
w.Flush()
}
func (m *model) preloadLastInputs() {
// Open the file
file, err := os.Open("last-inputs.txt")
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNumber := 0
for scanner.Scan() {
if lineNumber < len(m.inputs) {
m.inputs[lineNumber].SetValue(scanner.Text())
lineNumber++
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
}
func buildBucketQuery(inputs []textinput.Model) string {
bucketQuery := fmt.Sprintf("?prefix=%s&marker=%s", inputs[2].Value(), inputs[3].Value())
return bucketQuery
}
type model struct {
cursor int
err error
sub chan DownloadResourceResp
focusIndex int
inputs []textinput.Model
cursorMode textinput.CursorMode
showTypingView bool
showLoadingView bool
showDownloadingView bool
showErrorView bool
finishedDownloading bool
spinner spinner.Model
resources []string
downloadIndex int
downloadCount int
lastDownloadKey string
width int
height int
progress progress.Model
client *client.Client
}
func initialModel() model {
s := spinner.NewModel()
s.Spinner = spinner.Dot
p := progress.New(
progress.WithDefaultGradient(),
progress.WithWidth(40),
progress.WithoutPercentage(),
)
m := model{
err: nil,
sub: make(chan DownloadResourceResp),
inputs: make([]textinput.Model, 6),
spinner: s,
progress: p,
showTypingView: true,
showLoadingView: false,
showDownloadingView: false,
showErrorView: false,
downloadIndex: 0,
downloadCount: 0,
lastDownloadKey: "",
client: &client.Client{HTTPClient: http.DefaultClient},
}
var t textinput.Model
for i := range m.inputs {
t = textinput.New()
t.CursorStyle = cursorStyle
t.CharLimit = 32
switch i {
case 0:
t.Placeholder = "Bucket URL"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
t.CharLimit = 150
case 1:
t.Placeholder = "Site to grab bucket cookie authorizations"
t.CharLimit = 150
case 2:
t.Placeholder = "Bucket resource prefix"
t.CharLimit = 150
case 3:
t.Placeholder = "Start download marker"
t.CharLimit = 150
case 4:
t.Placeholder = "Files to ignore regex"
t.CharLimit = 150
case 5:
t.Placeholder = "Folder to save to"
t.CharLimit = 150
}
m.inputs[i] = t
}
return m
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
fmt.Printf("There's been an error: %v", err)
os.Exit(1)
}
}