forked from kazeburo/diff-detector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff-detector.go
162 lines (144 loc) · 3.75 KB
/
diff-detector.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
package main
import (
"fmt"
"path/filepath"
"io/ioutil"
"strings"
"regexp"
"crypto/md5"
"os"
"os/exec"
"os/user"
"github.com/jessevdk/go-flags"
)
type options struct {
OptArgs []string
OptCommand string
OptIdentifier string `long:"identifier" arg:"String" description:"indetify a file store the command result with given string"`
}
func runCmd(curFile *os.File, opts *options ) error {
cmd := exec.Command(opts.OptCommand, opts.OptArgs...)
cmd.Stdout = curFile
if err := cmd.Start(); err != nil {
return err
}
cmd.Wait()
return nil
}
func runCopy(from string, to string ) error {
cmd := exec.Command("cp", from, to)
if err := cmd.Start(); err != nil {
return err
}
cmd.Wait()
return nil
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func main() {
os.Exit(_main())
}
func _main() (st int) {
st = 1
opts := &options{OptIdentifier: ""}
parser := flags.NewParser(opts, flags.PassDoubleDash)
parser.Name = "diff-detector"
parser.Usage = "[OPTIONS] -- command args1 args2"
args, err := parser.Parse()
if err != nil || len(args) == 0 {
parser.WriteHelp(os.Stdout)
return
}
opts.OptCommand = args[0]
if len(args) > 1 {
opts.OptArgs = args[1:]
}
diffCmd, err := exec.LookPath("diff");
if err != nil {
fmt.Printf("%s", err)
return
}
tmpDir := "/edx/var/mackerel/diff-detector"
hasher := md5.New()
hasher.Write([]byte(opts.OptIdentifier))
hasher.Write([]byte(opts.OptCommand))
for _, v := range opts.OptArgs {
hasher.Write([]byte(v))
}
commandKey := fmt.Sprintf("%x",hasher.Sum(nil))
curUser, _ := user.Current()
prevPath := filepath.Join(tmpDir, curUser.Uid + "-diff-detector-" + commandKey)
// fmt.Printf("prevPath:%s diffCmd:%s\n",prevPath,diffCmd)
curFile, err := ioutil.TempFile(tmpDir, "temp")
if err != nil {
fmt.Printf("%s", err)
return
}
// fmt.Printf("curPath:%s\n",curFile.Name())
defer os.Remove(curFile.Name())
err = runCmd(curFile, opts)
if err != nil {
fmt.Printf("Error: %s",err)
return
}
if ( ! fileExists(prevPath) ) {
if ( len(opts.OptArgs) > 0 ) {
fmt.Printf("Notice: first time execution command: '%s %s'\n", opts.OptCommand, strings.Join(opts.OptArgs, " "))
} else {
fmt.Printf("Notice: first time execution command: '%s'\n", opts.OptCommand)
}
err = runCopy(curFile.Name(), prevPath)
if err != nil {
fmt.Printf("Error: %s",err)
return
}
st = 0
return
}
// diff
diffOut, diffError := exec.Command(diffCmd, "-U","1",prevPath,curFile.Name()).Output()
err = runCopy(curFile.Name(), prevPath)
if err != nil {
fmt.Printf("Error: %s",err)
return
}
// fmt.Printf("%s '%s'", diffOut, diffError);
if ( diffError == nil ) {
curOpen, err := os.Open(curFile.Name())
if err != nil {
fmt.Printf("Error: %s",err)
return
}
defer curOpen.Close()
fileinfo, _ := curOpen.Stat()
data := make([]byte, 128)
count, err := curOpen.Read(data)
if err != nil {
fmt.Printf("Error: %s",err)
return
}
cur := string(data[0:count])
cur = regexp.MustCompile("(\r\n|\r|\n)$").ReplaceAllString(cur, "")
if ( fileinfo.Size() > 128 ) {
fmt.Printf("OK: no difference: ```%s...```\n", cur)
} else {
fmt.Printf("OK: no difference: ```%s```\n", cur)
}
st = 0
} else if ( regexp.MustCompile("exit status 1").MatchString(diffError.Error()) ) {
diffRet := strings.Split(string(diffOut),"\n")
diffRetString := strings.Join(diffRet[2:],"\n")
diffRetString = regexp.MustCompile("(\r\n|\r|\n)$").ReplaceAllString(diffRetString, "")
if ( len(diffRetString) > 512 ) {
fmt.Printf("NG: detect difference: ```%s...```\n", diffRetString[0:512])
} else {
fmt.Printf("NG: detect difference: ```%s```\n", diffRetString)
}
st = 2
} else {
fmt.Printf("Error: %s\n", diffError)
}
return
}