-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (136 loc) · 3.75 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
145
146
147
148
149
150
151
152
package main
import (
"flag"
"fmt"
)
func main() {
// Flags must go before any commands
// Flag for "all items"
var all bool
flag.BoolVar(&all, "a", false, "all items (shortcut)")
flag.BoolVar(&all, "all", false, "all items")
// Flag for "item name"
var name string
flag.StringVar(&name, "n", "", "item name (shortcut)")
flag.StringVar(&name, "name", "", "item name")
// Flag for "item id"
var id string
flag.StringVar(&id, "id", "", "item id")
// Flag for "item description"
var description string
flag.StringVar(&description, "d", "", "item description (shortcut)")
flag.StringVar(&description, "description", "", "item description")
// Flag for "item status"
var status string
flag.StringVar(&status, "s", "", "item status (shortcut)")
flag.StringVar(&status, "status", "", "item status")
// Flag for "allow empty"
var allowEmpty bool
flag.BoolVar(&allowEmpty, "e", false, "allow empty (shortcut)")
flag.BoolVar(&allowEmpty, "allow-empty", false, "allow empty values to be passed in for the item name or description when updating an item")
// Flag for "showing archived items"
var showArchived bool
flag.BoolVar(&showArchived, "include-archived", false, "show archived items")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
args = append(args, "help")
}
// TODO::
// - [x] Extract flags from args
// - [x] Add help command
// - [x] Add list command
// - [x] Add create command
// - [x] Add update command
// - [x] Add delete command
// - [x] Add some sort of data store. Probably just going to use a static CSV file for now.
// - [x] Add todo list-type functionality
// - [] Bulk actions
// Add CRUD endpoints for todo list items
store, err := GetStore()
if err != nil {
if err.Error() == "open store.csv: no such file or directory" {
CreateStore()
store, err = GetStore()
} else {
fmt.Println(err)
return
}
}
cmd, err := StringToCommand(args[0])
if err != nil {
fmt.Println(err)
return
}
switch cmd {
case Help:
var cmds []Command
if len(args) == 1 {
cmds = []Command{Help, List, CreateTestStore, Create, Delete, Update, Progress, Archive, ChangeStatus}
} else {
for _, arg := range args[1:] {
cmd, err := StringToCommand(arg)
if err != nil {
fmt.Println(err)
return
}
cmds = append(cmds, cmd)
}
}
for _, cmd := range cmds {
fmt.Println(GetUsage(cmd))
}
case List:
if name != "" {
item, err := store.GetItemByName(name)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(item)
}
} else if id != "" {
item, err := store.GetItemById(id)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(item)
}
} else if all || len(args) == 1 {
store.ListItems(showArchived)
} else {
fmt.Println("Too many arguments for list command")
}
case CreateTestStore:
store.CreateTestStore()
case Create:
if name == "" {
fmt.Println("Please provide a name for the item to create")
return
}
store.CreateItem(name, description, status)
case Delete:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to delete")
return
}
store.DeleteItem(name, id)
case Update:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to update. If you'd like to update the name, you must provide the id.")
return
}
store.UpdateItem(id, name, description, status, allowEmpty)
case Progress:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to progress")
return
}
store.ProgressItem(id, name)
case Archive:
store.UpdateItem(id, name, "", "archived", false)
case ChangeStatus:
store.UpdateItem(id, name, "", status, false)
default:
fmt.Println("Invalid command: " + args[0] + ". Use 'help' to see available commands.")
}
}