forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_reader.go
49 lines (38 loc) · 811 Bytes
/
git_reader.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
package githttp
import (
"errors"
"io"
"regexp"
)
// GitReader scans for errors in the output of a git command
type GitReader struct {
// Underlaying reader (to relay calls to)
io.ReadCloser
// Error
GitError error
}
// Regex to detect errors
var (
gitErrorRegex = regexp.MustCompile("error: (.*)")
)
// Implement the io.Reader interface
func (g *GitReader) Read(p []byte) (n int, err error) {
// Relay call
n, err = g.ReadCloser.Read(p)
// Scan for errors
g.scan(p)
return n, err
}
func (g *GitReader) scan(data []byte) {
// Already got an error
// the main error will be the first error line
if g.GitError != nil {
return
}
matches := gitErrorRegex.FindSubmatch(data)
// Skip, no matches found
if matches == nil {
return
}
g.GitError = errors.New(string(matches[1][:]))
}