/
dev.go
214 lines (199 loc) · 4.24 KB
/
dev.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
package dev
import (
"bufio"
"errors"
"io"
"os/exec"
"strconv"
"strings"
"time"
"github.com/x-formation/pulsekit"
)
// Personal TODO(rjeczalik): document
type Personal struct {
Patch string
Project string
Stages []string
Revision string
}
var ErrTimeout = errors.New("pulsedev: waiting for command to finish has timed out")
// Exec TODO(rjeczalik): document
type Exec interface {
// LookPath TODO(rjeczalik): document
LookPath(string) (string, error)
// CombinedOutput TODO(rjeczalik): document
CombinedOutput(string, []string) (io.Writer, io.Reader, func(time.Duration) error, error)
}
type cmdExec struct{}
func (cmdExec) LookPath(file string) (string, error) {
return exec.LookPath(file)
}
func (cmdExec) CombinedOutput(file string, args []string) (stdin io.Writer,
stdout io.Reader, wait func(time.Duration) error, err error) {
cmd := exec.Command(file, args...)
if stdin, err = cmd.StdinPipe(); err != nil {
return
}
if stdout, err = cmd.StdoutPipe(); err != nil {
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
return
}
stdout = io.MultiReader(stdout, stderr)
if err = cmd.Start(); err != nil {
return
}
wait = func(d time.Duration) (err error) {
done := make(chan error)
go func() {
done <- cmd.Wait()
close(done)
}()
select {
case err, _ = <-done:
case <-time.After(d):
err = ErrTimeout
}
return
}
return
}
// DefaultExec TODO(rjeczalik): document
var DefaultExec Exec = new(cmdExec)
// Tool TODO(rjeczalik): document
type Tool interface {
// TODO(rjeczalik): document
Personal(p *Personal) (int64, error)
SetTimeout(d time.Duration)
}
type tool struct {
exec Exec
c pulse.Client
url string
user string
pass string
exe string
d time.Duration
}
func New(c pulse.Client, url, user, pass string) (Tool, error) {
var err error
t := &tool{
exec: DefaultExec,
c: c,
url: url,
user: user,
pass: pass,
d: 15 * time.Second,
}
if t.exe, err = t.exec.LookPath("pulse"); err != nil {
return nil, err
}
if err = t.valid(); err != nil {
return nil, err
}
return t, nil
}
func (t *tool) valid() error {
for _, args := range [][]string{{"help"}, {"help", "personal"}} {
_, _, wait, err := t.exec.CombinedOutput(t.exe, args)
if err != nil {
return err
}
if err = wait(t.d); err != nil {
return err
}
}
return nil
}
func (t *tool) Personal(p *Personal) (id int64, err error) {
var (
line []byte
res []byte
stages []pulse.ProjectStage
delim = byte('\n')
)
args := []string{
"personal",
"-s", t.url,
"-u", t.user,
"-p", t.pass,
"-r", p.Project,
"-t", "git",
"-f", p.Patch,
}
if p.Revision != "" {
args = append(args, "-e", p.Revision)
}
if p.Stages != nil {
stages = make([]pulse.ProjectStage, len(p.Stages))
for i, s := range p.Stages {
stages[i], err = t.c.ConfigStage(p.Project, s)
if err != nil {
return
}
stages[i].Enabled = false
if err = t.c.SetConfigStage(p.Project, stages[i]); err != nil {
return
}
}
}
in, out, wait, err := t.exec.CombinedOutput(t.exe, args)
if err != nil {
return
}
r := bufio.NewReader(out)
INTERACTIVE:
for {
line, err = r.ReadBytes(delim)
if err == io.EOF {
continue
}
if err != nil {
break INTERACTIVE
}
s := string(line)
switch delim {
case '\n':
if strings.Contains(s, "Continue anyway?") {
delim, res = '>', []byte("Yes\n")
}
if strings.Contains(s, "Synchronise now?") {
delim, res = '>', []byte("No\n")
}
if strings.Contains(s, "Choose revision to build against") {
delim, res = '>', []byte("1!\n")
}
i := strings.Index(s, "Patch accepted: personal build")
if i != -1 {
id, err = strconv.ParseInt(s[31+i:len(s)-2], 10, 64)
break INTERACTIVE
}
if strings.Contains(s, "Error") || strings.Contains(s, "Exception") {
err = errors.New(s)
break INTERACTIVE
}
case '>':
if _, err = in.Write(res); err != nil && err != io.EOF {
break INTERACTIVE
}
delim = '\n'
}
}
if e := wait(t.d); (err == io.EOF || err == nil) && e != nil {
err = e
}
if stages != nil {
for i := range stages {
stages[i].Enabled = true
if e := t.c.SetConfigStage(p.Project, stages[i]); e != nil && err == nil {
err = e
}
}
}
return
}
func (t *tool) SetTimeout(d time.Duration) {
t.d = d
}