-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdiff.go
45 lines (36 loc) · 765 Bytes
/
diff.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
)
func Diffb(a string, b string) []byte {
dirpath := NewSimpleTempDir("diffdir_")
defer os.RemoveAll(dirpath)
fa := SimpleTempFile(dirpath)
fmt.Fprintf(fa, "%s\n", a)
fa.Close()
fb := SimpleTempFile(dirpath)
fmt.Fprintf(fb, "%s\n", b)
fb.Close()
co, err := exec.Command("diff", "-b", fa.Name(), fb.Name()).CombinedOutput()
if err != nil {
// don't panic, diff returns 2 on differences
}
return co
}
func NewSimpleTempDir(prefix string) string {
dirpath, err := ioutil.TempDir(".", prefix)
if err != nil {
panic(err)
}
return dirpath
}
func SimpleTempFile(dirpath string) *os.File {
f, err := ioutil.TempFile(dirpath, "diff_file_")
if err != nil {
panic(err)
}
return f
}