-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathroot.go
More file actions
73 lines (60 loc) · 2.04 KB
/
Copy pathroot.go
File metadata and controls
73 lines (60 loc) · 2.04 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
package cmd
import (
"fmt"
"strings"
"github.com/prime-run/togo/config"
"github.com/prime-run/togo/ui"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
var TodoFileName = "todos.json"
var sourceFlag string = "project"
var skipConfirmations bool
var rootCmd = &cobra.Command{
Use: "togo",
Short: "A simple todo application",
Long: `A simple todo application that lets you manage your tasks from the terminal.`,
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
if err != nil {
handleErrorAndExit(err, "Error loading config:")
}
// Flag overrides config
if !skipConfirmations {
skipConfirmations = cfg.SkipConfirmations
}
todoList := loadTodoListOrExit()
tableModel := ui.NewTodoTable(todoList)
tableModel.SetSource(sourceFlag, TodoFileName)
tableModel.SetConfig(cfg)
tableModel.SkipConfirmationsByDefault = skipConfirmations
if skipConfirmations {
tableModel.SetSkipConfirmationsStatus("on")
} else {
tableModel.SetSkipConfirmationsStatus("off")
}
finalModel, err := tea.NewProgram(tableModel, tea.WithAltScreen()).Run()
handleErrorAndExit(err, "Error running program:")
saveTodoTableAfterTUIOrExit(finalModel)
},
}
func Execute() error {
return rootCmd.Execute()
}
func init() {
rootCmd.PersistentFlags().StringVarP(&sourceFlag, "source", "s", "project", "todo source: project or global")
rootCmd.PersistentFlags().BoolVarP(&skipConfirmations, "skip-confirmations", "y", false, "skip confirmations for delete/archive")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
s := strings.ToLower(strings.TrimSpace(sourceFlag))
switch s {
case "project", "global":
sourceFlag = s
return nil
default:
return fmt.Errorf("invalid value for --source: %q (must be 'project' or 'global')", sourceFlag)
}
}
_ = rootCmd.RegisterFlagCompletionFunc("source", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"project", "global"}, cobra.ShellCompDirectiveNoFileComp
})
}