-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.go
183 lines (169 loc) · 3.78 KB
/
edit.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/tobstarr/wlcli/wlclient"
)
type edit struct {
ListID int `cli:"arg"`
listID int
}
func (r *edit) Run() error {
cl, err := loadClient()
if err != nil {
return err
}
editor := os.Getenv("EDITOR")
if editor == "" {
return fmt.Errorf("EDITOR must be set in ENV")
}
listID := r.ListID
if listID == 0 {
listID = r.listID
}
tasks, err := cl.Tasks(wlclient.ListID(listID))
if err != nil {
return err
}
list, err := cl.List(listID)
if err != nil {
return err
}
tmpPath, dir, err := writeTasksToTempFile(list, tasks)
if err != nil {
return err
}
defer os.RemoveAll(dir)
c := exec.Command(editor, tmpPath)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
if err := c.Run(); err != nil {
return err
}
f, err := os.Open(tmpPath)
if err != nil {
return err
}
actions, err := extractActions(cl, listID, f)
f.Close()
if err != nil {
return err
}
for _, a := range actions {
if err := a(); err != nil {
return err
}
}
return nil
}
type action func() error
type actions []action
var l = log.New(os.Stderr, "", 0)
func extractActions(cl *wlclient.Client, listID int, in io.Reader) (out actions, err error) {
scanner := bufio.NewScanner(in)
for scanner.Scan() {
action, err := extractActionFromLine(cl, listID, scanner.Text())
if err != nil {
l.Printf("WARN: %s", err)
continue
} else if action != nil {
out = append(out, action)
}
}
return out, scanner.Err()
}
func extractActionFromLine(cl *wlclient.Client, listID int, line string) (action, error) {
if isComment(line) {
return nil, nil
}
fields := strings.Fields(line)
if len(fields) == 0 {
return nil, nil
}
if fields[0] == "*" && len(fields) > 1 {
return func() error {
_, err := cl.CreateTask(&wlclient.Task{ListID: listID, Title: strings.Join(fields[1:], " ")})
return err
}, nil
}
if len(fields) < 3 {
return nil, nil
}
action, idString := fields[0], fields[1]
id, err := strconv.Atoi(idString)
if err != nil {
return nil, fmt.Errorf("%q does not parse to int: %s", idString, err)
}
switch action {
case "r", "reword":
return func() error {
txt := strings.Join(fields[2:], " ")
dbg.Printf("updating %d to %q", id, txt)
return cl.UpdateTask(&wlclient.UpdateTask{ID: id, Title: s2p(txt)})
}, nil
case "c", "complete":
return func() error { return cl.CompleteTask(id) }, nil
case "d", "delete":
return func() error { return cl.DeleteTask(id) }, nil
case "k", "keep":
return nil, nil
default:
return nil, fmt.Errorf("action %q not supported", action)
}
}
func isComment(line string) bool {
return strings.HasPrefix(line, "# ")
}
func writeTasksToTempFile(list *wlclient.List, tasks wlclient.Tasks) (path, dir string, err error) {
buf := &bytes.Buffer{}
for _, t := range tasks {
if _, err := fmt.Fprintf(buf, "k %d %s\n", t.ID, t.Title); err != nil {
return "", "", err
}
}
if len(tasks) > 0 {
if _, err := io.WriteString(buf, "\n"); err != nil {
return "", "", err
}
}
if _, err := fmt.Fprintf(buf, "# Edit list %d: %s\n", list.ID, list.Title); err != nil {
return "", "", err
}
if _, err := io.WriteString(buf, usage); err != nil {
return "", "", err
}
d, err := ioutil.TempDir("/tmp", "wlcli-")
if err != nil {
return "", "", err
}
f, err := os.Create(filepath.Join(d, "git-rebase-todo"))
if err != nil {
return "", "", err
}
defer f.Close()
_, err = io.Copy(f, buf)
if err != nil {
return "", "", err
}
return f.Name(), d, nil
}
const usage = `# Commands
# k, keep = leave unchanged
# r, reword = use task, but edit the title
# c, complete = complete task
# d, delete = delete task
#
# to create create new tasks, add them on the bottom of the list:
# * task 1
# * task 2
`