-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
main.go
356 lines (332 loc) · 8.08 KB
/
main.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
package main
import (
"errors"
"fmt"
"os"
"runtime"
"encoding/csv"
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/fatih/color"
"github.com/rkoesters/xdg/basedir"
"github.com/sachaos/todoist/lib"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
var (
homePath, _ = os.UserHomeDir()
configPath = filepath.Join(basedir.ConfigHome, "todoist")
cachePath = filepath.Join(basedir.CacheHome, "todoist", "cache.json")
CommandFailed = errors.New("command failed")
IdNotFound = errors.New("specified id not found")
writer Writer
ShortDateTimeFormat = "06/01/02(Mon) 15:04"
ShortDateFormat = "06/01/02(Mon)"
)
const (
configName = "config"
configType = "json"
)
func GetClient(c *cli.Context) *todoist.Client {
return c.App.Metadata["client"].(*todoist.Client)
}
func main() {
app := cli.NewApp()
app.Name = "todoist"
app.Usage = "Todoist CLI Client"
app.Version = "0.20.0"
app.EnableBashCompletion = true
contentFlag := cli.StringFlag{
Name: "content",
Aliases: []string{"c"},
Usage: "content",
}
priorityFlag := cli.IntFlag{
Name: "priority",
Aliases: []string{"p"},
Value: 4,
Usage: "priority (1-4)",
}
labelNamesFlag := cli.StringFlag{
Name: "label-names",
Aliases: []string{"L"},
Usage: "label names (separated by ,)",
}
projectIDFlag := cli.IntFlag{
Name: "project-id",
Aliases: []string{"P"},
Usage: "project id",
}
projectNameFlag := cli.StringFlag{
Name: "project-name",
Aliases: []string{"N"},
Usage: "project name",
}
dateFlag := cli.StringFlag{
Name: "date",
Aliases: []string{"d"},
Usage: "date string (today, 2020/04/02, 2020/03/21 18:00)",
}
browseFlag := cli.BoolFlag{
Name: "browse",
Aliases: []string{"o"},
Usage: "when contain URL, open it",
}
filterFlag := cli.StringFlag{
Name: "filter",
Aliases: []string{"f"},
Usage: "filter expression",
}
sortPriorityFlag := cli.BoolFlag{
Name: "priority",
Aliases: []string{"p"},
Usage: "sort the output by priority",
}
reminderFlg := cli.BoolFlag{
Name: "reminder",
Aliases: []string{"r"},
Usage: "set reminder (only premium users)",
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "header",
Usage: "output with header",
},
&cli.BoolFlag{
Name: "color",
Usage: "colorize output",
},
&cli.BoolFlag{
Name: "csv",
Usage: "output in CSV format",
},
&cli.BoolFlag{
Name: "debug",
Usage: "output logs",
},
&cli.BoolFlag{
Name: "namespace",
Usage: "display parent task like namespace",
},
&cli.BoolFlag{
Name: "indent",
Usage: "display children task with indent",
},
&cli.BoolFlag{
Name: "project-namespace",
Usage: "display parent project like namespace",
},
}
app.Before = func(c *cli.Context) error {
var store todoist.Store
if err := LoadCache(cachePath, &store); err != nil {
return err
}
viper.SetConfigType(configType)
viper.SetConfigName(configName)
viper.AddConfigPath(configPath)
viper.AddConfigPath(".")
viper.SetEnvPrefix("todoist") // uppercased automatically by viper
viper.AutomaticEnv()
var token string
configFile := filepath.Join(configPath, configName+"."+configType)
if err := AssureExists(configFile); err != nil {
return err
}
if err := viper.ReadInConfig(); err != nil {
if _, isConfigNotFoundError := err.(viper.ConfigFileNotFoundError); !isConfigNotFoundError {
// config file was found but could not be read => not recoverable
return err
} else if !viper.IsSet("token") {
// config file not found and token missing (not provided via another source,
// such as environment variables) => ask interactively for token and store it in config file.
fmt.Printf("Input API Token: ")
fmt.Scan(&token)
viper.Set("token", token)
buf, err := json.MarshalIndent(viper.AllSettings(), "", " ")
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
err = ioutil.WriteFile(configFile, buf, 0600)
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
}
if exists, _ := Exists(configFile); exists {
// Ensure that the config file has permission 0600, because it contains
// the API token and should only be read by the user.
// This is only necessary iff the config file exists, which may not be the case
// when config is loaded from environment variables.
fi, err := os.Lstat(configFile)
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
if runtime.GOOS != "windows" && fi.Mode().Perm() != 0600 {
panic(fmt.Errorf("Config file has wrong permissions. Make sure to give permissions 600 to file %s \n", configFile))
}
}
config := &todoist.Config{AccessToken: viper.GetString("token"), DebugMode: c.Bool("debug"), Color: viper.GetBool("color"), DateFormat: viper.GetString("shortdateformat"), DateTimeFormat: viper.GetString("shortdatetimeformat")}
client := todoist.NewClient(config)
client.Store = &store
app.Metadata = map[string]interface{}{
"client": client,
"config": config,
}
if config.AccessToken != store.User.Token {
Sync(c)
if err := LoadCache(cachePath, &store); err != nil {
return err
}
}
if !c.Bool("color") && !config.Color {
color.NoColor = true
} else {
color.NoColor = false
}
if config.DateFormat != "" {
ShortDateFormat = config.DateFormat
}
if config.DateTimeFormat != "" {
ShortDateTimeFormat = config.DateTimeFormat
}
if c.Bool("csv") {
writer = csv.NewWriter(os.Stdout)
} else if runtime.GOOS == "windows" && !color.NoColor {
writer = NewTSVWriter(color.Output)
} else {
writer = NewTSVWriter(os.Stdout)
}
return nil
}
app.Commands = []*cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "Show all tasks",
Action: List,
Flags: []cli.Flag{
&filterFlag,
&sortPriorityFlag,
},
ArgsUsage: " ",
},
{
Name: "show",
Usage: "Show task detail",
Action: Show,
Flags: []cli.Flag{
&browseFlag,
},
ArgsUsage: "<Item ID>",
},
{
Name: "completed-list",
Aliases: []string{"c-l", "cl"},
Usage: "Show all completed tasks (only premium user)",
Action: CompletedList,
Flags: []cli.Flag{
&filterFlag,
},
ArgsUsage: " ",
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add task",
Action: Add,
Flags: []cli.Flag{
&priorityFlag,
&labelNamesFlag,
&projectIDFlag,
&projectNameFlag,
&dateFlag,
&reminderFlg,
},
ArgsUsage: "<Item content>",
},
{
Name: "modify",
Aliases: []string{"m"},
Usage: "Modify task",
Action: Modify,
Flags: []cli.Flag{
&contentFlag,
&priorityFlag,
&labelNamesFlag,
&projectIDFlag,
&projectNameFlag,
&dateFlag,
},
ArgsUsage: "<Item ID>",
},
{
Name: "close",
Aliases: []string{"c"},
Usage: "Close task",
Action: Close,
ArgsUsage: "<Item ID>",
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete task",
Action: Delete,
ArgsUsage: "<Item ID>",
},
{
Name: "labels",
Usage: "Show all labels",
Action: Labels,
ArgsUsage: " ",
},
{
Name: "projects",
Usage: "Show all projects",
Action: Projects,
ArgsUsage: " ",
},
{
Name: "add-project",
Aliases: []string{"ap"},
Usage: "Add new project",
Action: AddProject,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "color",
Usage: "In range 30-49",
},
&cli.IntFlag{
Name: "item-order",
Usage: "Order index",
},
},
ArgsUsage: "<Project name>",
},
{
Name: "karma",
Usage: "Show karma",
Action: Karma,
ArgsUsage: " ",
},
{
Name: "sync",
Aliases: []string{"s"},
Usage: "Sync cache",
Action: Sync,
ArgsUsage: " ",
},
{
Name: "quick",
Aliases: []string{"q"},
Usage: "Quick add a task",
Action: Quick,
ArgsUsage: "<Item content>",
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}