-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcli.go
More file actions
397 lines (346 loc) · 9.39 KB
/
Copy pathcli.go
File metadata and controls
397 lines (346 loc) · 9.39 KB
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
package main
import (
"encoding/json"
"fmt"
"log/slog"
"strings"
"app/backend"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// CLI flags and config
type cliConfig struct {
recursive bool
threads int
forceUpload bool
deleteFromHost bool
disableUnsupportedFilesFilter bool
setDateFromFilename bool
excludePattern string
logLevel string
configPath string
albumName string
}
// Messages for bubbletea
type uploadStartMsg struct {
total int
}
type fileProgressMsg struct {
workerID int
status string
fileName string
message string
}
type fileCompleteMsg struct {
success bool
fileName string
mediaKey string
err error
}
type uploadCompleteMsg struct{}
// Album messages
type albumProgressMsg struct {
albumName string
itemsAdded int
totalItems int
}
type albumCompleteMsg struct {
albumName string
itemsAdded int
albumKeys []string
}
type albumErrorMsg struct {
albumName string
error string
}
// Bubbletea model
type uploadModel struct {
progress progress.Model
totalFiles int
completed int
failed int
currentFiles map[int]string // workerID -> current file
workers map[int]string // workerID -> status message
results []uploadResult // Track all upload results
width int
quitting bool
// Album state
albumName string
albumItemsAdded int
albumTotalItems int
albumComplete bool
albumError string
albumKeys []string
}
type uploadResult struct {
Path string `json:"path"`
Success bool `json:"success"`
MediaKey string `json:"mediaKey,omitempty"`
Error string `json:"error,omitempty"`
}
type albumSummary struct {
Name string `json:"name,omitempty"`
ItemsAdded int `json:"itemsAdded,omitempty"`
AlbumKeys []string `json:"albumKeys,omitempty"`
Error string `json:"error,omitempty"`
}
type uploadSummary struct {
Total int `json:"total"`
Succeeded int `json:"succeeded"`
Failed int `json:"failed"`
Results []uploadResult `json:"results"`
Album *albumSummary `json:"album,omitempty"`
}
func initialModel() uploadModel {
return uploadModel{
progress: progress.New(progress.WithDefaultGradient()),
currentFiles: make(map[int]string),
workers: make(map[int]string),
results: []uploadResult{},
width: 80,
}
}
func (m uploadModel) Init() tea.Cmd {
return nil
}
func (m uploadModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.progress.Width = msg.Width - 4
return m, nil
case uploadStartMsg:
m.totalFiles = msg.total
return m, nil
case fileProgressMsg:
m.workers[msg.workerID] = fmt.Sprintf("[%d] %s: %s", msg.workerID, msg.status, msg.fileName)
if msg.fileName != "" {
m.currentFiles[msg.workerID] = msg.fileName
}
return m, nil
case fileCompleteMsg:
result := uploadResult{
Path: msg.fileName,
Success: msg.success,
MediaKey: msg.mediaKey,
}
if msg.success {
m.completed++
} else {
m.failed++
if msg.err != nil {
result.Error = msg.err.Error()
}
}
m.results = append(m.results, result)
return m, nil
case uploadCompleteMsg:
m.quitting = true
return m, tea.Quit
case albumProgressMsg:
m.albumName = msg.albumName
m.albumItemsAdded = msg.itemsAdded
m.albumTotalItems = msg.totalItems
m.albumComplete = false
return m, nil
case albumCompleteMsg:
m.albumName = msg.albumName
m.albumItemsAdded = msg.itemsAdded
m.albumComplete = true
m.albumKeys = msg.albumKeys
return m, nil
case albumErrorMsg:
m.albumName = msg.albumName
m.albumError = msg.error
return m, nil
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
m.quitting = true
return m, tea.Quit
}
}
return m, nil
}
func (m uploadModel) View() string {
if m.quitting {
return ""
}
var b strings.Builder
titleStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("99"))
b.WriteString(titleStyle.Render("Uploading to Google Photos"))
b.WriteString("\n\n")
// Progress bar
if m.totalFiles > 0 {
percent := float64(m.completed+m.failed) / float64(m.totalFiles)
b.WriteString(m.progress.ViewAs(percent))
fmt.Fprintf(&b, "\n%d/%d files", m.completed+m.failed, m.totalFiles)
fmt.Fprintf(&b, " (✓ %d success, ✗ %d failed)\n\n", m.completed, m.failed)
}
// Worker status
for i := 0; i < len(m.workers); i++ {
if status, ok := m.workers[i]; ok {
b.WriteString(status)
b.WriteString("\n")
}
}
// Album progress
if m.albumName != "" {
b.WriteString("\n")
albumStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212"))
if m.albumError != "" {
errorStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("196"))
b.WriteString(errorStyle.Render("✗ Album error: "))
b.WriteString(m.albumError)
b.WriteString("\n")
} else if m.albumComplete {
b.WriteString(albumStyle.Render("✓ Added to album: "))
b.WriteString(m.albumName)
fmt.Fprintf(&b, " (%d items)\n", m.albumItemsAdded)
} else if m.albumTotalItems > 0 {
b.WriteString(albumStyle.Render("Adding to album: "))
b.WriteString(m.albumName)
fmt.Fprintf(&b, " (%d/%d items)\n", m.albumItemsAdded, m.albumTotalItems)
}
}
b.WriteString("\n\nPress Ctrl+C to cancel\n")
return b.String()
}
// parseLogLevel converts a string log level to slog.Level
func parseLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn", "warning":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
// Default to info for CLI
return slog.LevelInfo
}
}
// CLI upload implementation
func runCLIUpload(filePaths []string, config cliConfig) error {
// Set custom config path if provided
if config.configPath != "" {
backend.ConfigPath = config.configPath
}
// Load backend config
err := backend.LoadConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Override config with CLI flags
backend.AppConfig.Recursive = config.recursive
backend.AppConfig.UploadThreads = config.threads
backend.AppConfig.ForceUpload = config.forceUpload
backend.AppConfig.DeleteFromHost = config.deleteFromHost
backend.AppConfig.DisableUnsupportedFilesFilter = config.disableUnsupportedFilesFilter
backend.AppConfig.SetDateFromFilename = config.setDateFromFilename
backend.AppConfig.ExcludePattern = config.excludePattern
// Handle album option - check for AUTO mode
if strings.ToUpper(config.albumName) == "AUTO" {
backend.AppConfig.AlbumAutoMode = true
backend.AppConfig.AlbumName = ""
} else {
backend.AppConfig.AlbumAutoMode = false
backend.AppConfig.AlbumName = config.albumName
}
// Parse log level
logLevel := parseLogLevel(config.logLevel)
// Start bubbletea program
model := initialModel()
p := tea.NewProgram(model)
// Create CLI app with event callback to bubbletea
eventCallback := func(event string, data any) {
switch event {
case "uploadStart":
if start, ok := data.(backend.UploadBatchStart); ok {
p.Send(uploadStartMsg{total: start.Total})
}
case "ThreadStatus":
if status, ok := data.(backend.ThreadStatus); ok {
fileName := status.FileName
// No truncation - show full filename
p.Send(fileProgressMsg{
workerID: status.WorkerID,
status: status.Status,
fileName: fileName,
message: status.Message,
})
}
case "FileStatus":
if result, ok := data.(backend.FileUploadResult); ok {
p.Send(fileCompleteMsg{
success: !result.IsError,
fileName: result.Path,
mediaKey: result.MediaKey,
err: result.Error,
})
}
case "uploadStop":
p.Send(uploadCompleteMsg{})
case "albumProgress":
if status, ok := data.(backend.AlbumStatus); ok {
p.Send(albumProgressMsg{
albumName: status.AlbumName,
itemsAdded: status.ItemsAdded,
totalItems: status.TotalItems,
})
}
case "albumComplete":
if status, ok := data.(backend.AlbumStatus); ok {
p.Send(albumCompleteMsg{
albumName: status.AlbumName,
itemsAdded: status.ItemsAdded,
albumKeys: status.AlbumKeys,
})
}
case "albumError":
if albumErr, ok := data.(backend.AlbumError); ok {
p.Send(albumErrorMsg{
albumName: albumErr.AlbumName,
error: albumErr.Error,
})
}
}
}
cliApp := backend.NewCLIApp(eventCallback, logLevel)
uploadManager := backend.NewUploadManager(cliApp)
// Run upload in background
go func() {
uploadManager.Upload(cliApp, filePaths)
}()
// Run the TUI
finalModel, err := p.Run()
if err != nil {
return fmt.Errorf("error running TUI: %w", err)
}
// Print JSON summary after TUI completes
if m, ok := finalModel.(uploadModel); ok {
summary := uploadSummary{
Total: m.totalFiles,
Succeeded: m.completed,
Failed: m.failed,
Results: m.results,
}
// Add album info if present
if m.albumName != "" {
summary.Album = &albumSummary{
Name: m.albumName,
ItemsAdded: m.albumItemsAdded,
AlbumKeys: m.albumKeys,
Error: m.albumError,
}
}
jsonOutput, err := json.MarshalIndent(summary, "", " ")
if err != nil {
return fmt.Errorf("error generating JSON: %w", err)
}
fmt.Println(string(jsonOutput))
}
return nil
}