-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-crew.go
160 lines (145 loc) · 3.12 KB
/
cmd-crew.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
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"strconv"
)
type crewCmd struct {
mode string
NewName string
NewCode string
}
func (crewCmd) Command() string {
return "crew"
}
func (crewCmd) Description() string {
return "Manages crews"
}
func (crewCmd) Help(_ []string) {
output(func(print printer) {
print("crew ls – list all crews")
print("crew rm ID - deletes a crew")
print("crew new - creates a new crew")
print("crew select ID - selects the active crew")
print("crew status - updates the status of the active crew")
})
}
func (command crewCmd) Execute(args []string) error {
if len(args) == 0 {
command.Help(args)
} else {
if args[0] == "ls" {
return command.ls(args[1:])
}
if args[0] == "new" {
return command.new(args[1:])
}
if args[0] == "rm" {
return command.rm(args[1:])
}
if args[0] == "select" {
return command.selectCrew(args[1:])
}
if args[0] == "status" {
return command.status(args[1:])
}
}
return nil
}
func (command crewCmd) status(args []string) error {
command.mode = "status"
if activeCrewID == 0 {
output(func(print printer) {
print("no crew selected")
})
return nil
}
output(func(print printer) {
print("Enter new Crew Status")
})
var casted cmdlinesink = command
inputfocus = &casted
return nil
}
func (crewCmd) selectCrew(args []string) error {
activeContactID = 0
if args[0] == "_" {
activeCrewID = 0
updateSidebar()
} else {
tmp, _ := strconv.Atoi(args[0])
activeCrewID = uint(tmp)
updateSidebar()
}
return nil
}
func (crewCmd) ls(args []string) error {
var crews []crew
database.Find(&crews)
output(func(print printer) {
for _, crew := range crews {
print(fmt.Sprintf("%d: %s (%s)", crew.ID, crew.Name, crew.Code))
print(crew.Description)
}
})
return nil
}
func (crewCmd) rm(args []string) error {
var crew crew
id, _ := strconv.Atoi(args[0])
database.First(&crew, id)
database.Delete(&crew)
output(func(print printer) {
print("Crew deleted.")
})
return nil
}
func (command crewCmd) new(args []string) error {
command.mode = "new"
var casted cmdlinesink = command
inputfocus = &casted
output(func(print printer) {
print("Crew Name?")
})
return nil
}
func (command crewCmd) TextEntered(data string) error {
if command.mode == "new" {
if command.NewName == "" {
command.NewName = data
var casted cmdlinesink = command
inputfocus = &casted
output(func(print printer) {
print(data)
print("Access Code?")
})
} else if command.NewCode == "" {
command.NewCode = data
var casted cmdlinesink = command
inputfocus = &casted
output(func(print printer) {
print(data)
print("Crew Status?")
})
} else {
crew := crew{Name: command.NewName, Description: data, Code: command.NewCode}
database.Create(&crew)
output(func(print printer) {
print(data)
print(fmt.Sprintf("Crew created with ID %d", crew.ID))
})
command.NewCode = ""
command.NewName = ""
updateSidebar()
}
}
if command.mode == "status" {
var crew crew
database.First(&crew, activeCrewID)
crew.Description = data
database.Save(crew)
output(func(print printer) {
print(data)
})
}
return nil
}