-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (88 loc) · 2.6 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
package main
import (
"log"
"github.com/TilliboyF/tuido/common"
"github.com/TilliboyF/tuido/handler"
"github.com/spf13/cobra"
)
var (
version = "1.0.2"
)
func NewRootCmd(handler *handler.TodoHandler) *cobra.Command {
rootCmd := &cobra.Command{
Use: "tuido",
Short: "Todo list app",
Long: `Application to store and handle your todos in one place!`,
RunE: handler.HandleMain,
}
rootCmd.AddCommand(NewAddCmd(handler))
rootCmd.AddCommand(NewCompleteCmd(handler))
rootCmd.AddCommand(NewListCmd(handler))
rootCmd.AddCommand(NewDeleteCmd(handler))
rootCmd.AddCommand(NewVersionCmd())
rootCmd.CompletionOptions.DisableDefaultCmd = true
return rootCmd
}
func NewAddCmd(handler *handler.TodoHandler) *cobra.Command {
var addCmd = &cobra.Command{
Use: "add <name>",
Short: "adding a new todo",
Long: `adding a new todo`,
DisableFlagsInUseLine: true,
PreRunE: common.ArgsCheckFunc(1),
RunE: handler.HandleAddTodo,
}
return addCmd
}
func NewCompleteCmd(handler *handler.TodoHandler) *cobra.Command {
completeCmd := &cobra.Command{
Use: "complete <id>",
Short: "Complete a task by the given id",
Long: `Complete a task by the given id`,
DisableFlagsInUseLine: true,
PreRunE: common.ArgsCheckFunc(1),
RunE: handler.HandleComplete,
}
return completeCmd
}
func NewListCmd(handler *handler.TodoHandler) *cobra.Command {
listCmd := &cobra.Command{
Use: "list",
Short: "List all todos",
Long: `List all unfinished todos, use flag -a to see all todos`,
PreRunE: common.ArgsCheckFunc(0),
RunE: handler.HandleList,
}
listCmd.Flags().BoolP("all", "a", false, "list all todos")
return listCmd
}
func NewDeleteCmd(handler *handler.TodoHandler) *cobra.Command {
deleteCmd := &cobra.Command{
Use: "delete <id>",
Short: "Delete a todo",
Long: `Delete a todo`,
DisableFlagsInUseLine: true,
PreRunE: common.ArgsCheckFunc(1),
RunE: handler.HandleDelete,
}
return deleteCmd
}
func NewVersionCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Get version of app",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.Printf("tuido app version %s\n", version)
},
}
return versionCmd
}
func main() {
handler, err := handler.NewTodoHandler(false)
if err != nil {
log.Fatal(err)
}
rootCmd := NewRootCmd(handler)
rootCmd.Execute()
}