-
Notifications
You must be signed in to change notification settings - Fork 23
/
common.go
395 lines (320 loc) · 8.33 KB
/
common.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package cmd
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/reports"
stackedErrors "github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/AlecAivazis/survey.v1"
)
var fullTimeFormat = "2006-01-02 15:04:05"
var simplerTimeFormat = "2006-01-02 15:04"
var onlyTimeFormat = "15:04:05"
var simplerOnlyTimeFormat = "15:04"
var nowTimeFormat = "now"
func withClockifyClient(fn func(cmd *cobra.Command, args []string, c *api.Client) error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
c, err := getAPIClient()
if err != nil {
return err
}
return fn(cmd, args, c)
}
}
func convertToTime(timeString string) (t time.Time, err error) {
timeString = strings.TrimSpace(timeString)
if nowTimeFormat == strings.ToLower(timeString) {
return time.Now().In(time.Local), nil
}
if len(fullTimeFormat) != len(timeString) && len(simplerTimeFormat) != len(timeString) && len(onlyTimeFormat) != len(timeString) && len(simplerOnlyTimeFormat) != len(timeString) {
return t, fmt.Errorf(
"supported formats are: %s",
strings.Join(
[]string{fullTimeFormat, simplerTimeFormat, onlyTimeFormat, simplerOnlyTimeFormat, nowTimeFormat},
", ",
),
)
}
if len(simplerOnlyTimeFormat) == len(timeString) || len(simplerTimeFormat) == len(timeString) {
timeString = timeString + ":00"
}
if len(onlyTimeFormat) == len(timeString) {
timeString = time.Now().Format("2006-01-02") + " " + timeString
}
return time.ParseInLocation(fullTimeFormat, timeString, time.Local)
}
func getAPIClient() (*api.Client, error) {
c, err := api.NewClient(viper.GetString("token"))
if err != nil {
return c, err
}
if viper.GetBool("debug") {
c.SetDebugLogger(
log.New(os.Stdout, "DEBUG ", log.LstdFlags),
)
}
return c, err
}
func getDateTimeParam(name string, required bool, value string, convert func(string) (time.Time, error)) (*time.Time, error) {
var t time.Time
var err error
message := fmt.Sprintf("%s (leave it blank for empty):", name)
if required {
message = fmt.Sprintf("%s:", name)
}
for {
_ = survey.AskOne(
&survey.Input{
Message: message,
Default: value,
},
&value,
nil,
)
if value == "" && !required {
return nil, nil
}
if t, err = convertToTime(value); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
continue
}
return &t, err
}
}
func getProjectByNameOrId(c *api.Client, workspace, project string) (string, error) {
project = strings.ToLower(strings.TrimSpace(project))
projects, err := c.GetProjects(api.GetProjectsParam{
Workspace: workspace,
PaginationParam: api.PaginationParam{AllPages: true},
})
if err != nil {
return "", err
}
for _, p := range projects {
if strings.ToLower(p.ID) == project {
return p.ID, nil
}
if strings.Contains(strings.ToLower(p.Name), project) {
return p.ID, nil
}
}
return "", stackedErrors.Errorf("No project with id or name containing: %s", project)
}
func confirmEntryInteractively(c *api.Client, te dto.TimeEntryImpl) (dto.TimeEntryImpl, error) {
var err error
te.ProjectID, err = getProjectID(te.ProjectID, te.WorkspaceID, c)
if err != nil {
return te, err
}
if te.ProjectID == "" {
return te, errors.New("project must be informed")
}
te.Description = getDescription(te.Description)
te.TagIDs, err = getTagIDs(te.TagIDs, te.WorkspaceID, c)
if err != nil {
return te, err
}
var date *time.Time
dateString := te.TimeInterval.Start.In(time.Local).Format(fullTimeFormat)
if date, err = getDateTimeParam("Start", true, dateString, convertToTime); err != nil {
return te, err
}
te.TimeInterval.Start = *date
dateString = ""
if te.TimeInterval.End != nil {
dateString = te.TimeInterval.End.In(time.Local).Format(fullTimeFormat)
}
if date, err = getDateTimeParam("End", false, dateString, convertToTime); err != nil {
return te, err
}
te.TimeInterval.End = date
return te, nil
}
func printTimeEntryImpl(c *api.Client, tei dto.TimeEntryImpl, asJSON bool, format string) error {
fte, err := c.ConvertIntoFullTimeEntry(tei)
if err != nil {
return err
}
var reportFn func(*dto.TimeEntry, io.Writer) error
reportFn = reports.TimeEntryPrint
if asJSON {
reportFn = reports.TimeEntryJSONPrint
}
if format != "" {
reportFn = reports.TimeEntryPrintWithTemplate(format)
}
return reportFn(&fte, os.Stdout)
}
func newEntry(c *api.Client, te dto.TimeEntryImpl, interactive, allowProjectByName, autoClose bool, format string, asJSON bool) error {
var err error
if allowProjectByName && te.ProjectID != "" {
te.ProjectID, err = getProjectByNameOrId(c, te.WorkspaceID, te.ProjectID)
if err != nil && !interactive {
return err
}
}
if interactive {
te, err = confirmEntryInteractively(c, te)
if err != nil {
return err
}
} else if te.ProjectID == "" {
return errors.New("project must be informed")
}
if autoClose {
err = c.Out(api.OutParam{
Workspace: te.WorkspaceID,
End: te.TimeInterval.Start,
})
if err != nil {
return err
}
}
tei, err := c.CreateTimeEntry(api.CreateTimeEntryParam{
Workspace: te.WorkspaceID,
Billable: te.Billable,
Start: te.TimeInterval.Start,
End: te.TimeInterval.End,
ProjectID: te.ProjectID,
Description: te.Description,
TagIDs: te.TagIDs,
TaskID: te.TaskID,
})
if err != nil {
return err
}
return printTimeEntryImpl(c, tei, asJSON, format)
}
func getProjectID(projectID string, workspace string, c *api.Client) (string, error) {
projects, err := c.GetProjects(api.GetProjectsParam{
Workspace: workspace,
})
if err != nil {
return "", err
}
projectsString := make([]string, len(projects))
found := false
for i, u := range projects {
projectsString[i] = fmt.Sprintf("%s - %s", u.ID, u.Name)
if u.ID == projectID {
projectID = projectsString[i]
found = true
}
}
if !found && projectID != "" {
fmt.Printf("Project '%s' informed was not found.\n", projectID)
projectID = ""
}
err = survey.AskOne(
&survey.Select{
Message: "Choose your project:",
Options: projectsString,
Default: projectID,
},
&projectID,
nil,
)
if err != nil {
return "", nil
}
return strings.TrimSpace(projectID[0:strings.Index(projectID, " - ")]), nil
}
func getDescription(description string) string {
_ = survey.AskOne(
&survey.Input{
Message: "Description:",
Default: description,
},
&description,
nil,
)
return description
}
func getTagIDs(tagIDs []string, workspace string, c *api.Client) ([]string, error) {
if len(tagIDs) > 0 && !viper.GetBool("interactive") {
return tagIDs, nil
}
tags, err := c.GetTags(api.GetTagsParam{
Workspace: workspace,
})
if err != nil {
return nil, err
}
tagsString := make([]string, len(tags))
for i, u := range tags {
tagsString[i] = fmt.Sprintf("%s - %s", u.ID, u.Name)
}
for i, t := range tagIDs {
for _, s := range tagsString {
if strings.HasPrefix(s, t) {
tagIDs[i] = s
break
}
}
}
var newTags []string
err = survey.AskOne(
&survey.MultiSelect{
Message: "Choose your tags:",
Options: tagsString,
Default: tagIDs,
},
&newTags,
nil,
)
if err != nil {
return nil, nil
}
for i, t := range newTags {
newTags[i] = strings.TrimSpace(t[0:strings.Index(t, " - ")])
}
return newTags, nil
}
func getUserId(c *api.Client) (string, error) {
userId := viper.GetString("user.id")
if len(userId) > 0 {
return userId, nil
}
u, err := c.GetMe()
if err != nil {
return "", err
}
return u.ID, nil
}
func getTimeEntry(id, workspace, userID string, c *api.Client) (dto.TimeEntryImpl, error) {
id = strings.ToLower(id)
if id != "last" && id != "current" {
tei, err := c.GetTimeEntry(api.GetTimeEntryParam{
Workspace: workspace,
TimeEntryID: id,
})
if err != nil {
return dto.TimeEntryImpl{}, err
}
if tei == nil {
return dto.TimeEntryImpl{}, errors.New("no previous time entry found")
}
return *tei, nil
}
list, err := c.GetRecentTimeEntries(api.GetRecentTimeEntries{
Workspace: workspace,
UserID: userID,
Page: 1,
ItemsPerPage: 1,
})
if err != nil {
return dto.TimeEntryImpl{}, err
}
if len(list.TimeEntriesList) == 0 {
return dto.TimeEntryImpl{}, errors.New("there is no previous time entry")
}
return list.TimeEntriesList[0], err
}