-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (125 loc) · 3.57 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
package main
import (
"fmt"
"os"
"strings"
"github.com/jmelahman/work/client"
"github.com/spf13/cobra"
)
var (
version = "dev"
commit = "none"
databasePath string
// Command flags
days int
notify bool
quiet bool
nonWork bool
chore bool
toil bool
)
func newRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "work",
Short: "Work time tracking tool",
Version: fmt.Sprintf("%s\ncommit %s", version, commit),
}
rootCmd.PersistentFlags().StringVar(&databasePath, "database", "", "Specify a custom database")
rootCmd.AddCommand(newInstallCmd())
rootCmd.AddCommand(newListCmd())
rootCmd.AddCommand(newReportCmd())
rootCmd.AddCommand(newStatusCmd())
rootCmd.AddCommand(newStopCmd())
rootCmd.AddCommand(newTaskCmd())
rootCmd.AddCommand(newUninstallCmd())
return rootCmd
}
func newInstallCmd() *cobra.Command {
return &cobra.Command{
Use: "install",
Short: "Install reminders",
Long: "Install reminder notification services",
RunE: func(cmd *cobra.Command, args []string) error {
return client.HandleInstall(false)
},
}
}
func newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List most recent tasks",
Long: "List most recent tasks",
RunE: func(cmd *cobra.Command, args []string) error {
return client.NewTaskManager(databasePath).ListTasks(days)
},
}
cmd.Flags().IntVarP(&days, "days", "d", 1, "List task from the last N days")
return cmd
}
func newReportCmd() *cobra.Command {
return &cobra.Command{
Use: "report",
Short: "Generate a weekly report",
Long: "Generate a weekly report",
RunE: func(cmd *cobra.Command, args []string) error {
return client.NewTaskManager(databasePath).GenerateReport()
},
}
}
func newStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Print current shift and task status",
Long: "Print current shift and task status",
RunE: func(cmd *cobra.Command, args []string) error {
return client.NewTaskManager(databasePath).GetStatus(quiet, notify)
},
}
cmd.Flags().BoolVarP(¬ify, "notify", "n", false, "Send a notification if no active tasks")
cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Exit with status code")
return cmd
}
func newStopCmd() *cobra.Command {
return &cobra.Command{
Use: "stop",
Short: "Stop any previous task",
Long: "Stop any previous task",
RunE: func(cmd *cobra.Command, args []string) error {
return client.NewTaskManager(databasePath).StopCurrentTask()
},
}
}
func newTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "task [description]",
Short: "Start a new Task",
Long: "Start a new task",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if (toil && nonWork) || (toil && chore) || (nonWork && chore) {
return fmt.Errorf("task should have at most one classification")
}
return client.NewTaskManager(databasePath).CreateTask(chore, nonWork, toil, strings.Join(args, " "))
},
}
cmd.Flags().BoolVarP(&nonWork, "break", "b", false, "Classify task as non-work")
cmd.Flags().BoolVarP(&chore, "chore", "c", false, "Classify the task as a chore")
cmd.Flags().BoolVarP(&toil, "toil", "t", false, "Classify the task as toil")
return cmd
}
func newUninstallCmd() *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Uninstall reminders",
Long: "Uninstall reminder notification services",
RunE: func(cmd *cobra.Command, args []string) error {
return client.HandleInstall(true)
},
}
}
func main() {
rootCmd := newRootCmd()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}