-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathinit.go
More file actions
292 lines (261 loc) · 7.55 KB
/
init.go
File metadata and controls
292 lines (261 loc) · 7.55 KB
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
package init
import (
"fmt"
"strings"
"time"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/pkg/cmdutil"
"github.com/lucassabreu/clockify-cli/pkg/ui"
"github.com/lucassabreu/clockify-cli/strhlp"
"github.com/spf13/cobra"
"golang.org/x/text/language"
)
func queue(
tasks ...func() error,
) error {
for _, t := range tasks {
if err := t(); err != nil {
return err
}
}
return nil
}
// NewCmdInit executes and initialization of the config
func NewCmdInit(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Setups the CLI parameters and behavior",
Long: "Setups the CLI parameters with tokens, default workspace, " +
"user and behaviors",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
i := f.UI()
config := f.Config()
var err error
token := ""
if token, err = i.AskForText("User Generated Token:",
ui.WithDefault(config.GetString(cmdutil.CONF_TOKEN)),
ui.WithHelp("Can be generated in the following like, "+
"in the API section: "+
"https://clockify.me/user/settings#generateApiKeyBtn"),
); err != nil {
return err
}
config.SetString(cmdutil.CONF_TOKEN, token)
c, err := f.Client()
if err != nil {
return err
}
if err := queue(
func() error { return setWorkspace(c, config, i) },
func() error { return setUser(c, config, i) },
updateFlag(
i, config, cmdutil.CONF_ALLOW_NAME_FOR_ID,
"Should try to find projects/clients/users/tasks/tags by their names?",
),
func() error {
if !config.IsAllowNameForID() {
return nil
}
return updateFlag(i, config,
cmdutil.CONF_SEARCH_PROJECTS_WITH_CLIENT_NAME,
`Should search projects looking into their `+
`client's name too?`,
)()
},
updateFlag(i, config, cmdutil.CONF_INTERACTIVE,
`Should use "Interactive Mode" by default?`,
),
updateInt(i, config, cmdutil.CONF_INTERACTIVE_PAGE_SIZE,
"How many items should be shown when asking for "+
"projects, tasks or tags?"),
func() error { return setWeekdays(config, i) },
updateFlag(i, config, cmdutil.CONF_ALLOW_INCOMPLETE,
`Should allow starting time entries with incomplete data?`,
),
updateFlag(i, config, cmdutil.CONF_SHOW_TASKS,
`Should show task on time entries as a separated column?`,
),
updateFlag(i, config, cmdutil.CONF_SHOW_CLIENT,
`Should show client on time entries as a separated column?`,
),
updateFlag(i, config, cmdutil.CONF_SHOW_TOTAL_DURATION,
`Should show a line with the sum of `+
`the time entries duration?`,
),
updateFlag(i, config, cmdutil.CONF_DESCR_AUTOCOMP,
`Allow description suggestions using `+
`recent time entries' descriptions?`,
),
func() error {
if !config.GetBool(cmdutil.CONF_DESCR_AUTOCOMP) {
config.SetInt(cmdutil.CONF_DESCR_AUTOCOMP_DAYS, 0)
return nil
}
return updateInt(
i, config, cmdutil.CONF_DESCR_AUTOCOMP_DAYS,
`How many days should be used for a time entry to be `+
`"recent"?`,
)()
},
updateFlag(i, config, cmdutil.CONF_ALLOW_ARCHIVED_TAGS,
"Should suggest and allow creating time entries "+
"with archived tags?",
),
updateFlag(i, config, cmdutil.CONF_TIME_ENTRY_DEFAULTS,
"Look for default parameters for time entries per folder?",
ui.WithConfirmHelp(
"This will set the default parameters of a time "+
"entry when using `clockify-cli in` and "+
"`clockify-cli manual` to the closest "+
".clockify-defaults.yaml file looking up the "+
"current folder you were running the commands.\n"+
"For more information and examples go to "+
"https://clockify-cli.netlify.app/",
),
),
setLanguage(i, config),
setTimezone(i, config),
); err != nil {
return err
}
return config.Save()
},
}
return cmd
}
func setTimezone(i ui.UI, config cmdutil.Config) func() error {
return func() error {
tzname, err := i.AskForValidText("What is your preferred timezone:",
func(s string) error {
_, err := time.LoadLocation(s)
return err
},
ui.WithHelp("Should be 'Local' to use the systems timezone, UTC "+
"or valid TZ identifier from the IANA TZ database "+
"https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"),
ui.WithDefault(config.TimeZone().String()),
)
if err != nil {
return err
}
tz, _ := time.LoadLocation(tzname)
config.SetTimeZone(tz)
return nil
}
}
func setLanguage(i ui.UI, config cmdutil.Config) func() error {
return func() error {
suggestLanguages := []string{
language.English.String(),
language.German.String(),
language.Afrikaans.String(),
language.Chinese.String(),
language.Portuguese.String(),
}
lang, err := i.AskForValidText("What is your preferred language:",
func(s string) error {
_, err := language.Parse(s)
return err
},
ui.WithHelp("Accepts any IETF language tag "+
"https://en.wikipedia.org/wiki/IETF_language_tag"),
ui.WithSuggestion(func(toComplete string) []string {
return strhlp.Filter(
strhlp.IsSimilar(toComplete),
suggestLanguages,
)
}),
ui.WithDefault(config.Language().String()),
)
if err != nil {
return err
}
config.SetLanguage(language.MustParse(lang))
return nil
}
}
func setWeekdays(config cmdutil.Config, i ui.UI) (err error) {
workweekDays := config.GetStringSlice(cmdutil.CONF_WORKWEEK_DAYS)
if workweekDays, err = i.AskManyFromOptions(
"Which days of the week do you work?",
cmdutil.GetWeekdays(),
workweekDays,
nil,
); err != nil {
return err
}
config.SetStringSlice(cmdutil.CONF_WORKWEEK_DAYS, workweekDays)
return nil
}
func setUser(c api.Client, config cmdutil.Config, i ui.UI) error {
users, err := c.WorkspaceUsers(api.WorkspaceUsersParam{
Workspace: config.GetString(cmdutil.CONF_WORKSPACE),
PaginationParam: api.AllPages(),
})
if err != nil {
return err
}
userID := config.GetString(cmdutil.CONF_USER_ID)
dUser := ""
usersString := make([]string, len(users))
for i := range users {
usersString[i] = fmt.Sprintf("%s - %s", users[i].ID, users[i].Name)
if users[i].ID == userID {
dUser = usersString[i]
}
}
if userID, err = i.AskFromOptions(
"Choose your user:", usersString, dUser); err != nil {
return err
}
config.SetString(cmdutil.CONF_USER_ID,
strings.TrimSpace(userID[0:strings.Index(userID, " - ")]))
return nil
}
func setWorkspace(c api.Client, config cmdutil.Config, i ui.UI) error {
ws, err := c.GetWorkspaces(api.GetWorkspaces{})
if err != nil {
return err
}
dWorkspace := ""
wsString := make([]string, len(ws))
for i := range ws {
wsString[i] = fmt.Sprintf("%s - %s", ws[i].ID, ws[i].Name)
if ws[i].ID == config.GetString(cmdutil.CONF_WORKSPACE) {
dWorkspace = wsString[i]
}
}
w := ""
if w, err = i.AskFromOptions("Choose default Workspace:",
wsString, dWorkspace); err != nil {
return err
}
config.SetString(cmdutil.CONF_WORKSPACE,
strings.TrimSpace(w[0:strings.Index(w, " - ")]))
return err
}
func updateInt(ui ui.UI, config cmdutil.Config, param, desc string,
) func() error {
return func() error {
value := config.GetInt(param)
value, err := ui.AskForInt(desc, value)
if err != nil {
return err
}
config.SetInt(param, value)
return nil
}
}
func updateFlag(
ui ui.UI, config cmdutil.Config, param, description string,
opts ...ui.ConfirmOption) func() error {
return func() (err error) {
b := config.GetBool(param)
if b, err = ui.Confirm(description, b, opts...); err != nil {
return
}
config.SetBool(param, b)
return
}
}