-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmilestone.go
213 lines (181 loc) · 4.63 KB
/
milestone.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"strconv"
"strings"
"time"
)
// milestone represents a project milestone
type milestone struct {
Name string
Date time.Time
Description string
PercentComplete int
}
// create a new milestone instance
func newMilestone(name string, date time.Time, description []string) *milestone {
return &milestone{
Name: name,
Date: date,
Description: strings.Join(description, " "),
PercentComplete: 0,
}
}
func printMilestoneUsageErr() {
l.Println(ErrInvalidUsage)
l.Println("usage: milestones [remove <name>] [set <name> <0-100>] [add <name> <date> [description]]")
}
// handle milestones shell command
func handleMilestonesCommand(args []string) {
if len(args) < 2 {
listMilestones()
return
}
// check sub command
switch args[1] {
case "remove":
if len(args) < 3 {
printMilestoneUsageErr()
return
}
removeMilestone(args[2])
return
case "set":
if len(args) < 4 {
printMilestoneUsageErr()
return
}
setMilestone(args[2], args[3])
return
case "add":
if len(args) < 3 {
printMilestoneUsageErr()
return
}
addMilestone(args[2:])
return
default:
printMilestoneUsageErr()
}
}
// create the status bar from the status integer
func getStatusBar(p int) string {
res := "["
for c := 1; c <= 10; c++ {
if p < c*10 {
res += " "
} else {
res += "=="
}
}
return res + "] " + strconv.Itoa(p) + "%"
}
// add a milestone to the project
func addMilestone(args []string) {
if len(args) < 2 {
printMilestoneUsageErr()
return
}
conf.Lock()
format := conf.fields.DateFormat
conf.Unlock()
// check if date is valid
t, err := time.Parse(format, args[1])
if err != nil {
Log.WithError(err).Error("failed to parse date")
return
}
// create milestone
var m *milestone
// check if theres a description
if len(args) >= 3 {
m = newMilestone(args[0], t, args[2:])
} else {
m = newMilestone(args[0], t, []string{})
}
projectData.Lock()
projectData.fields.Milestones = append(projectData.fields.Milestones, m)
projectData.Unlock()
projectData.update()
Log.Info("added milestone ", args[0])
}
// update a milestones status
// valid values are 0-100
func setMilestone(name, percent string) {
p, err := strconv.ParseInt(percent, 10, 0)
if err != nil || p > 100 || p < 0 {
printMilestoneUsageErr()
return
}
var ok bool
projectData.Lock()
for i := range projectData.fields.Milestones {
if projectData.fields.Milestones[i].Name == name {
projectData.fields.Milestones[i].PercentComplete = int(p)
ok = true
}
}
projectData.Unlock()
if !ok {
Log.Info("unknown milestone: ", name)
return
}
projectData.update()
}
// remove a milestone from project data
func removeMilestone(name string) {
if name == "" {
Log.Error("no name supplied")
return
}
projectData.Lock()
for i, m := range projectData.fields.Milestones {
if m.Name == name {
projectData.fields.Milestones = append(projectData.fields.Milestones[:i], projectData.fields.Milestones[i+1:]...)
projectData.Unlock()
projectData.update()
Log.Info("remove milestone ", name)
return
}
}
projectData.Unlock()
}
// print all milestones to stdout
func listMilestones() {
projectData.Lock()
defer projectData.Unlock()
if len(projectData.fields.Milestones) > 0 {
w := 30
l.Println(cp.Text + "Milestones")
l.Println(cp.Prompt + pad("status", 30) + pad("name", w) + pad("date", w) + "description" + cp.Text)
for _, m := range projectData.fields.Milestones {
if len(m.Description) > 0 {
l.Println(pad(getStatusBar(m.PercentComplete), 30) + pad(m.Name, w) + pad(m.Date.Format(conf.fields.DateFormat), w) + m.Description)
} else {
l.Println(pad(getStatusBar(m.PercentComplete), 30) + pad(m.Name, w) + m.Date.Format(conf.fields.DateFormat))
}
}
l.Println("")
} else {
if conf.fields.Debug {
l.Println("no milestones set.")
l.Println("")
}
}
}