forked from rcowham/go-libp4
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathp4.go
More file actions
284 lines (259 loc) · 6.46 KB
/
p4.go
File metadata and controls
284 lines (259 loc) · 6.46 KB
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
/*
Package p4 wraps the Perforce Helix Core command line.
It assumes p4 or p4.exe is in the PATH.
It uses the p4 -G global option which returns Python marshalled dictionary objects.
p4 Python parsing module is based on: https://github.com/hambster/gopymarshal
*/
package p4
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os/exec"
"regexp"
"strings"
"encoding/json"
"errors"
)
// P4 - environment for P4
type P4 struct {
port string
user string
client string
}
// NewP4 - create and initialise properly
func NewP4() *P4 {
var p4 P4
return &p4
}
// NewP4Params - create and initialise with params
func NewP4Params(port string, user string, client string) *P4 {
var p4 P4
p4.port = port
p4.user = user
p4.client = client
return &p4
}
// RunBytes - runs p4 command and returns []byte output
func (p4 *P4) RunBytes(args []string) ([]byte, error) {
cmd := exec.Command("p4", args...)
data, err := cmd.CombinedOutput()
if err != nil {
return data, err
}
return data, nil
}
// Get options that go before the p4 command
func (p4 *P4) getJOptions() []string {
opts := []string{"-Mj", "-ztag"}
if p4.port != "" {
opts = append(opts, "-p", p4.port)
}
if p4.user != "" {
opts = append(opts, "-u", p4.user)
}
if p4.client != "" {
opts = append(opts, "-c", p4.client)
}
return opts
}
// Get options that go before the p4 command
func (p4 *P4) getOptionsNonMarshal() []string {
opts := []string{}
if p4.port != "" {
opts = append(opts, "-p", p4.port)
}
if p4.user != "" {
opts = append(opts, "-u", p4.user)
}
if p4.client != "" {
opts = append(opts, "-c", p4.client)
}
return opts
}
// Runner is an interface to make testing p4 commands more easily
type Runner interface {
Run([]string) ([]map[string]string, error)
}
// Run - runs p4 command and returns map
func (p4 *P4) Run(args []string) ([]map[string]string, error) {
opts := p4.getJOptions()
args = append(opts, args...)
cmd := exec.Command("p4", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
mainerr := cmd.Run()
// May not be the correct place to do this
// But we are ignoring the actual error otherwise
if stderr.Len() > 0 {
return nil, errors.New(stderr.String())
}
results := make([]map[string]string, 0)
jdecoder := json.NewDecoder(&stdout)
buf := bufio.NewReader(&stdout)
for {
line, _, _ := buf.ReadLine()
r := make(map[string]string)
err := jdecoder.Decode(&r)
if err == io.EOF {
break
}
if err == nil {
if r == nil {
// End of object
break
}
} else {
if mainerr == nil {
mainerr = err
}
// The stdout contains context regarding the error
// in some cases, e.g. password expiring so we should
// also populate the results to convey the error details
// to the caller. Otherwise they'll just see an "exit
// status 1" in the error and empty results.
if r != nil {
results = append(results, r)
}
break
}
results = append(results, r)
}
return results, mainerr
}
// parseError turns perforce error messages into go error's
func parseError(res map[string]string) error {
var err error
var e string
if v, ok := res["data"]; ok {
e = v
} else {
// I don't know if we can get in this situation
e = fmt.Sprintf("Failed to parse error %v", err)
return errors.New(e)
}
// Search for non-existent depot error
nodepot, err := regexp.Match(`must refer to client`, []byte(e))
if err != nil {
return err // Do we need to return (error, error) for real error and parsed one?
}
if nodepot {
path := strings.Split(e, " - must")[0]
return errors.New("P4Error -> No such area '" + path + "', please check your path")
}
err = fmt.Errorf("P4Error -> %s", e)
return err
}
// Assume multiline entries should be on seperate lines
func formatSpec(specContents map[string]string) string {
var output bytes.Buffer
for k, v := range specContents {
if strings.Contains(v, "\n") {
output.WriteString(fmt.Sprintf("%s:", k))
lines := strings.Split(v, "\n")
for i := range lines {
if len(strings.TrimSpace(lines[i])) > 0 {
output.WriteString(fmt.Sprintf("\n %s", lines[i]))
}
}
output.WriteString("\n\n")
} else {
output.WriteString(fmt.Sprintf("%s: %s\n\n", k, v))
}
}
return output.String()
}
// Save - runs p4 -i for specified spec returns result
func (p4 *P4) Save(specName string, specContents map[string]string, args []string) ([]map[string]string, error) {
opts := p4.getJOptions()
nargs := []string{specName, "-i"}
nargs = append(nargs, args...)
args = append(opts, nargs...)
log.Println(args)
cmd := exec.Command("p4", args...)
var stdout, stderr bytes.Buffer
stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Println("An error occured: ", err)
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
mainerr := cmd.Start()
if mainerr != nil {
fmt.Println("An error occured: ", mainerr)
}
spec := formatSpec(specContents)
log.Println(spec)
io.WriteString(stdin, spec)
stdin.Close()
cmd.Wait()
results := make([]map[string]string, 0)
for {
r := make(map[string]string)
err := json.NewDecoder(&stdout).Decode(&r)
if err == io.EOF {
break
}
if err == nil {
if r == nil {
// End of object
break
}
results = append(results, r)
} else {
if mainerr == nil {
mainerr = err
}
break
}
}
return results, mainerr
}
// The Save() func doesn't work as it needs the data marshalled instead of
// map[string]string
// This is a quick fix, the real fix is writing a marshal() function or try
// using gopymarshal
func (p4 *P4) SaveTxt(specName string, specContents map[string]string, args []string) (string, error) {
opts := p4.getOptionsNonMarshal()
nargs := []string{specName, "-i"}
nargs = append(nargs, args...)
args = append(opts, nargs...)
log.Println(args)
cmd := exec.Command("p4", args...)
var stdout, stderr bytes.Buffer
stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Println("An error occured: ", err)
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
mainerr := cmd.Start()
if mainerr != nil {
fmt.Println("An error occured: ", mainerr)
}
spec := formatSpec(specContents)
log.Println(spec)
io.WriteString(stdin, spec)
// Need to explicitly call this for the command to fire
stdin.Close()
cmd.Wait()
e, err := ioutil.ReadAll(&stderr)
if err != nil {
fmt.Println("An error occured: ", err)
}
log.Println(e)
if len(e) > 0 {
return "", errors.New(string(e))
}
x, err := ioutil.ReadAll(&stdout)
if err != nil {
fmt.Println("An error occured: ", err)
}
s := string(x)
log.Println(s)
return s, mainerr
}