-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathutil.go
60 lines (51 loc) · 1.34 KB
/
util.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
package kubernetes
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
)
func diff(name, is, should string) (string, error) {
dir, err := ioutil.TempDir("", "diff")
if err != nil {
return "", err
}
defer os.RemoveAll(dir)
if err := ioutil.WriteFile(filepath.Join(dir, "LIVE-"+name), []byte(is), os.ModePerm); err != nil {
return "", err
}
if err := ioutil.WriteFile(filepath.Join(dir, "MERGED-"+name), []byte(should), os.ModePerm); err != nil {
return "", err
}
buf := bytes.Buffer{}
merged := filepath.Join(dir, "MERGED-"+name)
live := filepath.Join(dir, "LIVE-"+name)
cmd := exec.Command("diff", "-u", "-N", live, merged)
cmd.Stdout = &buf
err = cmd.Run()
// the diff utility exits with `1` if there are differences. We need to not fail there.
if exitError, ok := err.(*exec.ExitError); ok && err != nil {
if exitError.ExitCode() != 1 {
return "", err
}
}
out := buf.String()
if out != "" {
out = fmt.Sprintf("diff -u -N %s %s\n%s", live, merged, out)
}
return out, nil
}
// FilteredErr is a filtered Stderr. If one of the regular expressions match, the current input is discarded.
type FilteredErr []*regexp.Regexp
func (r FilteredErr) Write(p []byte) (n int, err error) {
for _, re := range r {
if re.Match(p) {
// silently discard
return len(p), nil
}
}
return os.Stderr.Write(p)
}