/
add.go
44 lines (35 loc) · 845 Bytes
/
add.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
package cmd
import (
"fmt"
"log"
"github.com/cuilan/todo-cli/todo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var priority int
var addCmd = &cobra.Command{
Use: "add",
Short: "Add a new todo-cli",
Long: "Add will create a new todo-cli item to the list",
Run: addRun,
}
func addRun(cmd *cobra.Command, args []string) {
items, err := todo.ReadItems(viper.GetString("datafile"))
// items, err := todo.ReadItems(".todo.json")
if err != nil {
log.Printf("%v", err)
}
for _, x := range args {
item := todo.Item{Text: x}
item.SetPriority(priority)
items = append(items, item)
}
if err := todo.SaveItems(viper.GetString("datafile"), items); err != nil {
_ = fmt.Errorf("%v", err)
}
}
func init() {
RootCmd.AddCommand(addCmd)
addCmd.Flags().IntVarP(&priority,
"priority", "p", 2, "Priority:1,2,3")
}