-
Notifications
You must be signed in to change notification settings - Fork 23
/
common.go
284 lines (231 loc) · 5.7 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
package cmd
import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"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)) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
c, err := getAPIClient()
if err != nil {
printError(err)
return
}
fn(cmd, args, c)
}
}
func printError(err error) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
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 newEntry(c *api.Client, te dto.TimeEntryImpl, interactive, autoClose bool) (dto.TimeEntryImpl, error) {
var err error
if interactive {
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")
}
if interactive {
te.Description = getDescription(te.Description)
}
if interactive {
te.TagIDs, err = getTagIDs(te.TagIDs, te.WorkspaceID, c)
if err != nil {
return te, err
}
var date *time.Time
dateString := te.TimeInterval.Start.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.Format(fullTimeFormat)
}
if date, err = getDateTimeParam("End", false, dateString, convertToTime); err != nil {
return te, err
}
te.TimeInterval.End = date
}
if autoClose {
err = c.Out(api.OutParam{
Workspace: te.WorkspaceID,
End: te.TimeInterval.Start,
})
if err != nil {
return te, err
}
}
return c.CreateTimeEntry(api.CreateTimeEntryParam{
Workspace: te.WorkspaceID,
Billable: !notBillable,
Start: te.TimeInterval.Start,
End: te.TimeInterval.End,
ProjectID: te.ProjectID,
Description: te.Description,
TagIDs: te.TagIDs,
TaskID: te.TaskID,
})
}
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))
for i, u := range projects {
projectsString[i] = fmt.Sprintf("%s - %s", u.ID, u.Name)
if u.ID == projectID {
projectID = projectsString[i]
}
}
println(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 {
return tagIDs, nil
}
if !viper.GetBool("interactive") {
return nil, 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)
}
err = survey.AskOne(
&survey.MultiSelect{
Message: "Choose your tags:",
Options: tagsString,
},
&tagIDs,
nil,
)
if err != nil {
return nil, nil
}
for i, t := range tagIDs {
tagIDs[i] = strings.TrimSpace(t[0:strings.Index(t, " - ")])
}
return tagIDs, 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
}