-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskwarrior-pomodoro-beeminder.go
250 lines (216 loc) · 5.63 KB
/
taskwarrior-pomodoro-beeminder.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os/exec"
"os/user"
"strconv"
"strings"
"time"
"github.com/howeyc/gopass"
"github.com/lunixbochs/go-keychain"
"github.com/ryanuber/go-glob"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/ini.v1"
)
var (
autoincrement = kingpin.Command("autoincrement", "Auto-increment a beeminder goal")
autoincrementTaskID = autoincrement.Arg("taskId", "Task ID to increment").String()
incrementGoal = kingpin.Command("increment_goal", "Increment a beeminder goal")
incrementGoalUsername = incrementGoal.Arg("username", "Username to use").String()
incrementGoalGoal = incrementGoal.Arg("goal", "Goal name to increment").String()
incrementGoalTaskID = incrementGoal.Flag("taskId", "Task ID to increment").String()
storeAuthToken = kingpin.Command("store_auth_token", "Store password information")
storeAuthTokenUsername = storeAuthToken.Arg("username", "Username to store a password for").String()
config = kingpin.Flag("config", "Configuration file to use").Short('c').Default("~/.taskwarrior-pomodoro-beeminder.cfg").String()
taskBin = kingpin.Flag("task-bin", "Path to taskwarrior binary").Default("/usr/local/bin/task").String()
taskRc = kingpin.Flag("taskrc", "Path to taskrc file").Default("~/.taskrc").String()
)
type taskData struct {
UUID string `json:"uuid"`
Description string `json:"description"`
Project string `json:"project"`
Tags []string `json:"tags"`
}
type goalData struct {
username string
goal string
}
func main() {
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("1.0").Author("Adam Coddington")
commandName := kingpin.Parse()
*config = expandUser(*config)
*taskBin = expandUser(*taskBin)
*taskRc = expandUser(*taskRc)
cfg, err := ini.Load(*config)
if err != nil {
log.Fatalf("Unable to open configuration file at %s\n", *config)
}
switch commandName {
case "autoincrement":
doAutoincrement(cfg)
case "increment_goal":
doIncrementGoal(cfg)
case "store_auth_token":
doStoreAuthToken(cfg)
default:
fmt.Println("Please specify a command.")
}
}
func doAutoincrement(cfg *ini.File) {
task := getTaskData(*autoincrementTaskID)
matchingGoals := getMatchingGoals(cfg, task)
for _, goal := range matchingGoals {
incrementBeeminderGoal(
goal.username,
goal.goal,
task.Description,
)
}
}
func doIncrementGoal(cfg *ini.File) {
taskDescription := ""
if *incrementGoalTaskID != "" {
taskDescription = getTaskData(*incrementGoalTaskID).Description
}
incrementBeeminderGoal(
*incrementGoalUsername,
*incrementGoalGoal,
taskDescription,
)
}
func doStoreAuthToken(cfg *ini.File) {
fmt.Println(
"Taskwarrior-Pomodoro-Beeminder needs your Beeminder " +
"Personal Authentication Token to interact with your Beeminder " +
"account. You can find yours by going to " +
"https://www.beeminder.com/api/v1/auth_token.json.",
)
fmt.Printf("Auth Token: ")
pass, err := gopass.GetPasswd()
if err == nil {
keychain.Add(
"taskwarrior-pomodoro-beeminder",
*storeAuthTokenUsername,
string(pass[:]),
)
}
}
func expandUser(path string) string {
usr, _ := user.Current()
dir := usr.HomeDir
return strings.Replace(path, "~/", dir+"/", 1)
}
func getTaskData(id string) *taskData {
output, err := exec.Command(
*taskBin,
"rc:"+*taskRc,
"rc.json.array=off",
id,
"export",
).Output()
if err != nil {
log.Fatal(err)
}
taskData := taskData{}
merr := json.Unmarshal(output, &taskData)
if merr != nil {
fmt.Println("error:", merr)
}
return &taskData
}
func incrementBeeminderGoal(username string, goal string, message string) {
token, err := keychain.Find(
"taskwarrior-pomodoro-beeminder",
*storeAuthTokenUsername,
)
if err != nil {
log.Fatalf(
"Unable to find stored authentication token for %s",
username,
)
}
fullURL := "https://www.beeminder.com/api/v1/users/" + username +
"/goals/" + goal + "/datapoints.json?auth_token=" + token
resp, err := http.PostForm(
fullURL,
url.Values{
"timestamp": {strconv.FormatInt(int64(time.Now().Unix()), 10)},
"value": {"1"},
"comment": {message},
},
)
if err == nil {
if resp.StatusCode != 200 {
log.Fatal(resp)
} else {
fmt.Println(resp)
}
} else {
log.Fatal(err)
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func taskMatchesConstraints(task *taskData, constraints []*ini.Key) bool {
for _, constraint := range constraints {
if constraint.Name() == "tags" {
for _, expectedTag := range strings.Split(constraint.Value(), ",") {
if !stringInSlice(expectedTag, task.Tags) {
return false
}
}
} else if constraint.Name() == "project" {
if !glob.Glob(constraint.Value(), task.Project) {
return false
}
}
}
return true
}
func getMatchingGoals(cfg *ini.File, task *taskData) []goalData {
var goals []goalData
defaultUsername := ""
defaultSection, err := cfg.GetSection("")
if err == nil {
if defaultSection.HasKey("username") {
defaultUsername = defaultSection.Key("username").Value()
}
}
for _, section := range cfg.Sections() {
if section.Name() == "DEFAULT" {
continue
}
if taskMatchesConstraints(task, section.Keys()) {
usernameKey, err := section.GetKey("username")
username := defaultUsername
if err == nil {
username = usernameKey.Value()
}
goalName := section.Name()
if section.HasKey("goal") {
goalNameKey, err := section.GetKey("goal")
if err == nil {
goalName = goalNameKey.Value()
}
}
goals = append(
goals,
goalData{
username,
goalName,
},
)
}
}
return goals
}