-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
438 lines (372 loc) · 11.2 KB
/
main.go
File metadata and controls
438 lines (372 loc) · 11.2 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
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
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/a3chron/gith/internal"
"github.com/a3chron/gith/internal/config"
ui "github.com/a3chron/gith/internal/ui"
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func getVersion() string {
// If version was set by GoReleaser, use it
if version != "dev" {
return version
}
// Fallback for go install: try to get version from build info
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "(devel)" && info.Main.Version != "" {
return info.Main.Version
}
}
return "dev"
}
func initialModel(cfg *config.Config) internal.Model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = ui.AccentStyle
return internal.Model{
CurrentStep: internal.StepLoad,
Loading: true,
ActionModel: internal.ActionModel{
Actions: []string{"Branch", "Status", "Commit", "Tag", "Remote", "Changes", "Options"},
},
BranchModel: internal.BranchModel{
Actions: []string{"Switch Branch", "Create Branch", "List Branches", "Delete Branch"},
Options: []string{"feat/", "fix/", "refactor/", "docs/", "Manual Input"},
},
CommitModel: internal.CommitModel{
Actions: []string{"Commit Staged", "Commit All", "Undo Last Commit"},
CommitPrefixes: []string{"feat", "fix", "chore", "build", "ci", "test", "perf", "refactor", "revert", "style", "docs", "Custom Prefix"},
},
TagModel: internal.TagModel{
Actions: []string{"Add Tag", "Remove Tag", "List Tags", "Push Tag"},
},
RemoteModel: internal.RemoteModel{
Actions: []string{"List Remotes", "Add Remote", "Push to Origin", "Remove Remote"},
},
ConfigModel: internal.ConfigModel{
Actions: []string{"Change Flavor", "Change Accent", "Fetch at Init Behaviour", "Reset to Defaults"},
InitBehaviours: []string{"Always fetch on Init", "Do not fetch for Quick Selects", "Never fetch"},
},
CurrentConfig: cfg,
Selected: 0,
Level: 0,
StartAt: "",
StartAtLevel: 0,
Spinner: s,
}
}
// initialModelWithStart returns the base model but allows setting a quick-start flow.
func initialModelWithStart(startAt string, startAtLevel int, cfg *config.Config) internal.Model {
m := initialModel(cfg)
m.StartAt = startAt
m.StartAtLevel = startAtLevel
return m
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run() error {
if len(os.Args) > 1 {
return handleCliArgs()
}
cfg, err := config.LoadConfig()
if err != nil {
// Fall back to defaults if config loading fails
fmt.Fprintf(os.Stderr, "Warning: failed to load config, using defaults: %v\n", err)
cfg = &config.Config{
Accent: config.DefaultConfig.Accent,
Flavor: config.DefaultConfig.Flavor,
InitBehaviour: config.DefaultConfig.InitBehaviour,
}
}
// Initialize styles with the loaded config
ui.UpdateStylesByConfig(cfg)
isRepo, err := internal.IsGitRepository()
if err != nil || !isRepo {
return fmt.Errorf("not in a git repository")
}
p := tea.NewProgram(initialModel(cfg))
if _, err := p.Run(); err != nil {
return fmt.Errorf("failed to run program: %w", err)
}
return nil
}
// runQuick starts the UI directly at a specific flow (e.g., add-tag).
func runQuick(startAt string, startAtLevel int) error {
cfg, err := config.LoadConfig()
if err != nil {
// Fall back to defaults if config loading fails
fmt.Fprintf(os.Stderr, "Warning: failed to load config, using defaults: %v\n", err)
cfg = &config.Config{
Accent: config.DefaultConfig.Accent,
Flavor: config.DefaultConfig.Flavor,
}
}
// Initialize styles with the loaded config
ui.UpdateStylesByConfig(cfg)
isRepo, err := internal.IsGitRepository()
if err != nil || !isRepo {
return fmt.Errorf("not in a git repository")
}
p := tea.NewProgram(initialModelWithStart(startAt, startAtLevel, cfg))
if _, err := p.Run(); err != nil {
return fmt.Errorf("failed to run program: %w", err)
}
return nil
}
func handleCliArgs() error {
switch os.Args[1] {
case "version", "-v", "--version":
checkForUpdate := len(os.Args) == 3 && os.Args[2] == "check"
internal.PrintVersion(checkForUpdate, getVersion(), commit, date, builtBy)
return nil
case "help", "-h", "--help":
internal.PrintHelp()
return nil
case "update":
if version == "dev" {
// installed via go install (or actual dev build lol)
err := internal.Update()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
return nil
}
fmt.Fprintf(os.Stderr, "To update via 'gith update' install gith via go\nInstuctions are linked in the repo's readme\n")
os.Exit(1)
case "config":
return handleConfigCommand()
case "completion":
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: gith completion <shell>\nSupported shells: bash, zsh, fish\n")
os.Exit(1)
}
if err := internal.GenerateCompletions(os.Args[2]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
return nil
case "add":
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: gith add remote\n")
os.Exit(1)
}
switch os.Args[2] {
case "remote":
return runQuick("add-remote", 3)
default:
os.Exit(1)
return fmt.Errorf("unknown command: %s\nUse 'gith help' for usage information", os.Args[1]+" "+os.Args[2])
}
case "tag":
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: gith tag\n")
os.Exit(1)
}
// Start UI directly at tag selection
return runQuick("add-tag", 3)
case "switch":
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: gith switch\n")
os.Exit(1)
}
return runQuick("switch-branch", 3)
case "push":
if len(os.Args) != 2 && len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: gith push [tag]\n")
os.Exit(1)
}
if len(os.Args) == 2 {
return runQuick("push-origin", 2)
}
switch os.Args[2] {
case "tag":
// Start UI directly at push tag selection
return runQuick("push-tag", 3)
default:
os.Exit(1)
return fmt.Errorf("unknown command: %s\nUse 'gith help' for usage information", os.Args[1]+" "+os.Args[2])
}
case "delete":
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: gith delete branch\n")
os.Exit(1)
}
switch os.Args[2] {
case "branch":
return runQuick("delete-branch", 3)
default:
os.Exit(1)
return fmt.Errorf("unknown command: %s\nUse 'gith help' for usage information", os.Args[1]+" "+os.Args[2])
}
case "list":
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: gith list [ branch | tag ]\n")
os.Exit(1)
}
switch os.Args[2] {
case "branch":
return runQuick("list-branch", 2)
case "tag":
return runQuick("list-tag", 2)
default:
os.Exit(1)
return fmt.Errorf("unknown command: %s\nUse 'gith help' for usage information", os.Args[1]+" "+os.Args[2])
}
case "commit":
if len(os.Args) >= 4 {
fmt.Fprintf(os.Stderr, "Usage: gith commit [ all | staged ]\n")
os.Exit(1)
}
if len(os.Args) == 2 {
return runQuick("commit-staged", 3)
}
switch os.Args[2] {
case "all":
return runQuick("commit-all", 3)
case "staged":
return runQuick("commit-staged", 3)
default:
os.Exit(1)
return fmt.Errorf("unknown command: %s\nUse 'gith help' for usage information", os.Args[1]+" "+os.Args[2])
}
case "undo":
if len(os.Args) != 3 || os.Args[2] != "commit" {
fmt.Fprintf(os.Stderr, "Usage: gith undo commit\n")
os.Exit(1)
}
return runQuick("undo-commit", 2)
case "status":
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: gith status\n")
os.Exit(1)
}
return runQuick("status", 1)
}
fmt.Fprintf(os.Stderr, "unknown command: %s\nUse 'gith help' for usage information", strings.Join(os.Args[1:], " "))
os.Exit(1)
return fmt.Errorf("unknown command")
}
func handleConfigCommand() error {
if len(os.Args) < 3 {
return printConfigUsage()
}
switch os.Args[2] {
case "show":
return showConfig()
case "reset":
return resetConfig()
case "path":
return showConfigPath()
case "update":
return updateConfig()
default:
return printConfigUsage()
}
}
func printConfigUsage() error {
helpText := `
Config commands:
gith config show - Show current configuration
gith config reset - Reset configuration to defaults
gith config path - Show configuration file path
gith config update [--flavor=<flavor>] [--accent=<accent>] [--initFetch=<initFetch>]
Update your configuration options. Flags are optional and can be combined.
--flavor=<flavor>
Set the Catppuccin flavor (case insensitive). Available options:
Latte, Frappe, Macchiato, Mocha
--accent=<accent>
Set the Catppuccin accent color (case insensitive). Available options:
Rosewater, Flamingo, Pink, Mauve, Red, Maroon, Peach, Yellow, Green,
Teal, Blue, Sapphire, Sky, Lavender, Gray
--initFetch=<initFetch>
Set fetch behavior on init. Available options:
always - always fetch on init
quick - fetch only for full load, skip for quick selects
never - never fetch on init
`
fmt.Println(helpText)
return nil
}
func showConfig() error {
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
fmt.Printf("Current configuration:\n")
fmt.Printf(" Flavor: %s\n", cfg.Flavor)
fmt.Printf(" Accent: %s\n", cfg.Accent)
fmt.Printf(" Init Behaviour: %s\n", cfg.InitBehaviour)
return nil
}
func updateConfig() error {
if len(os.Args) <= 3 {
return printConfigUsage()
}
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
args := os.Args[3:]
for _, arg := range args {
arg = strings.ToLower(arg)
switch {
case strings.HasPrefix(arg, "--flavor="):
val := strings.TrimPrefix(arg, "--flavor=")
if !config.IsValidFlavor(val) {
return fmt.Errorf("not a valid flavor: %s\nvalid flavors: %s", val, strings.Join(config.GetAvailableFlavors(), ", "))
}
cfg.Flavor = val
case strings.HasPrefix(arg, "--accent="):
val := strings.TrimPrefix(arg, "--accent=")
if !config.IsValidAccent(val) {
return fmt.Errorf("not a valid accent: %s\nvalid accents: %s", val, strings.Join(config.GetAvailableAccents(), ", "))
}
cfg.Accent = val
case strings.HasPrefix(arg, "--initfetch="):
val := strings.TrimPrefix(arg, "--initfetch=")
valid := map[string]bool{"always": true, "quick": true, "never": true}
if !valid[val] {
return fmt.Errorf("not a valid initFetch: %s\nvalid options: always, quick, never", val)
}
cfg.InitBehaviour = val
default:
return fmt.Errorf("'%s' is not a valid flag\nRun 'gith config help' to see valid flags", strings.Split(arg, "=")[0])
}
}
saveErr := config.SaveConfig(cfg)
if saveErr != nil {
return fmt.Errorf("failed to save config: %w", saveErr)
}
fmt.Println("Configuration updated")
return nil
}
func resetConfig() error {
if err := config.SaveConfig(&config.DefaultConfig); err != nil {
return fmt.Errorf("failed to reset config: %w", err)
}
fmt.Println("Configuration reset to defaults")
return nil
}
func showConfigPath() error {
path, err := config.GetConfigPath()
if err != nil {
return fmt.Errorf("failed to get config path: %w", err)
}
fmt.Println(path)
return nil
}