-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-task.go
114 lines (91 loc) · 2.17 KB
/
next-task.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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/bplaxco/next-task/config"
"github.com/bplaxco/next-task/google"
"github.com/bplaxco/next-task/jira"
"github.com/bplaxco/next-task/tasks"
)
type ParsedArgs struct {
Reset bool
Help bool
}
const Usage string = `
NAME
next-task - snatch a next task from a source
SYNOPSIS
next-task [--help|--reset]
OPTIONS
--help Show this help text
--reset Reset the cached content
`
func ParseArgs(args []string) *ParsedArgs {
parsedArgs := &ParsedArgs{}
for _, arg := range args[1:] {
switch arg {
case "--reset":
parsedArgs.Reset = true
case "--help":
parsedArgs.Help = true
default:
fmt.Printf("%s is not a valid argument\n", arg)
parsedArgs.Help = true
break
}
}
return parsedArgs
}
func randomTask(ctx context.Context, cfg *config.Config) *tasks.Task {
// The order of things pulled here will mater given
// the capacity system. In the future it might make
// sense to pull max capacity of each list and shuffle
// the full list of tasks and then only cache
// up to the total capacity. It wastes network
// calls but ensures we're pulling from each
// source fairly evenly
if tasks.CachedTaskCount() == 0 {
if cfg.Google != nil {
for _, task := range google.FetchTasks(ctx, cfg.Google) {
task.Cache()
}
}
if cfg.Jira != nil {
for _, task := range jira.FetchTasks(cfg.Jira) {
task.Cache()
}
}
}
task, err := tasks.RandomTask()
if err != nil {
log.Fatalf("tasks.RandomTask: %v", err)
}
return task
}
func main() {
parsedArgs := ParseArgs(os.Args)
if parsedArgs.Help {
fmt.Print(Usage)
return
}
if parsedArgs.Reset {
err := os.RemoveAll(tasks.TaskCacheDir())
if err != nil {
fmt.Printf("os.RemoveAll: %s", err.Error())
}
return
}
ctx := context.Background()
cfg := config.NewConfig(ctx)
task := randomTask(ctx, cfg)
if len(task.Description) > 0 {
fmt.Printf("- %s: %s\n %s\n\n", task.Kind, task.Title, task.Description)
} else {
fmt.Printf("- %s: %s\n\n", task.Kind, task.Title)
}
if err := task.ClearCache(); err != nil {
log.Fatalf("could not clear cache for task: %s", task.Title)
}
}