@@ -26,6 +26,8 @@ func init() {
26
26
{"help" , "help <cmd> show help of a command" , help },
27
27
{"show" , "Show specific task by id" , showTask },
28
28
{"list" , "List task by status task and search criteria" , listTasks },
29
+ {"addtag" , "Add a tag to a task" , addTag },
30
+ {"rmtag" , "Remove a tag to a task" , rmTag },
29
31
{"add" , "Add a task" , addTask },
30
32
{"json" , "Print all tasks in json" , printJSON },
31
33
{"save" , "Save the database" , save },
@@ -37,7 +39,7 @@ func init() {
37
39
38
40
func execCommand (stdout io.Writer , db * JSONDb , line * liner.State , cmdLine string ) {
39
41
for _ , cmd := range commands {
40
- if strings .HasPrefix (cmdLine , cmd .Name ) {
42
+ if cmdLine == cmd . Name || strings .HasPrefix (cmdLine , cmd .Name + " " ) {
41
43
cmd .Exec (stdout , db , line , cmdLine )
42
44
return
43
45
}
@@ -46,7 +48,11 @@ func execCommand(stdout io.Writer, db *JSONDb, line *liner.State, cmdLine string
46
48
}
47
49
48
50
func tokenize (cmdLine string ) []string {
49
- return strings .Split (cmdLine , " " )
51
+ args := strings .Split (strings .TrimSpace (cmdLine ), " " )
52
+ for i , arg := range args {
53
+ args [i ] = strings .TrimSpace (arg )
54
+ }
55
+ return args
50
56
}
51
57
52
58
func help (stdout io.Writer , db * JSONDb , line * liner.State , cmdLine string ) {
@@ -158,6 +164,52 @@ func open(stdout io.Writer, db *JSONDb, line *liner.State, cmdLine string) {
158
164
}
159
165
}
160
166
167
+ func addTag (stdout io.Writer , db * JSONDb , line * liner.State , cmdLine string ) {
168
+ cmdArgs := tokenize (cmdLine )
169
+ if len (cmdArgs ) == 3 {
170
+ id , err := strconv .Atoi (cmdArgs [1 ])
171
+ if err != nil {
172
+ fmt .Println ("First arg need to be an integer" )
173
+ return
174
+ }
175
+ task := findByID (db .Tasks , id )
176
+ task .Tags = append (task .Tags , cmdArgs [2 ])
177
+ // Print result
178
+ fmt .Fprintln (stdout , task .AnsiString ())
179
+ } else {
180
+ fmt .Println ("You need to specify task id" )
181
+ }
182
+ }
183
+
184
+ func rmTag (stdout io.Writer , db * JSONDb , line * liner.State , cmdLine string ) {
185
+ cmdArgs := tokenize (cmdLine )
186
+ if len (cmdArgs ) == 3 {
187
+ id , err := strconv .Atoi (cmdArgs [1 ])
188
+ if err != nil {
189
+ fmt .Println ("First arg need to be an integer" )
190
+ return
191
+ }
192
+ task := findByID (db .Tasks , id )
193
+
194
+ foundIndex := len (task .Tags )
195
+ for i , tag := range task .Tags {
196
+ if tag == cmdArgs [2 ] {
197
+ foundIndex = i
198
+ break
199
+ }
200
+ }
201
+ if foundIndex < len (task .Tags ) {
202
+ // delete tag
203
+ task .Tags = append (task .Tags [:foundIndex ], task .Tags [foundIndex + 1 :]... )
204
+ }
205
+
206
+ // Print result
207
+ fmt .Fprintln (stdout , task .AnsiString ())
208
+ } else {
209
+ fmt .Println ("You need to specify task id" )
210
+ }
211
+ }
212
+
161
213
func save (stdout io.Writer , db * JSONDb , line * liner.State , cmdLine string ) {
162
214
backupDatabase (dbPath , "_bak" )
163
215
saveDatabase (dbPath , db )
0 commit comments