forked from StackExchange/dnscontrol
/
validate.go
132 lines (118 loc) · 2.92 KB
/
validate.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
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"os"
"os/exec"
"strings"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
func main() {
failed := false
run := func(ctx string, preStatus string, goodStatus string, f func() error) {
setStatus(stPending, preStatus, ctx)
if err := f(); err != nil {
fmt.Println(err)
setStatus(stError, err.Error(), ctx)
failed = true
} else {
setStatus(stSuccess, goodStatus, ctx)
}
}
run("gofmt", "Checking gofmt", "gofmt ok", checkGoFmt)
run("gogen", "Checking go generate", "go generate ok", checkGoGenerate)
if failed {
os.Exit(1)
}
}
func checkGoFmt() error {
cmd := exec.Command("gofmt", "-s", "-l", ".")
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
if len(out) == 0 {
return nil
}
files := strings.Split(string(out), "\n")
fList := ""
for _, f := range files {
if strings.HasPrefix(f, "vendor") {
continue
}
if fList != "" {
fList += "\n"
}
fList += f
}
if fList == "" {
return nil
}
return errors.Errorf("The following files need to have gofmt run on them:\n%s", fList)
}
func checkGoGenerate() error {
cmd := exec.Command("go", "generate")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
modified, err := getModifiedFiles()
if err != nil {
return err
}
if len(modified) != 0 {
return errors.Errorf("ERROR: The following files are modified after go generate:\n%s", strings.Join(modified, "\n"))
}
return nil
}
func getModifiedFiles() ([]string, error) {
cmd := exec.Command("git", strings.Split("diff --name-only", " ")...)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
if len(out) == 0 {
return nil, nil
}
return strings.Split(string(out), "\n"), nil
}
const (
stPending = "pending"
stSuccess = "success"
stError = "error"
)
func setStatus(status string, desc string, ctx string) {
if commitish == "" || ctx == "" {
return
}
client.Repositories.CreateStatus(context.Background(), "StackExchange", "dnscontrol", commitish, &github.RepoStatus{
Context: &ctx,
Description: &desc,
State: &status,
})
}
var client *github.Client
var commitish string
func init() {
// not intended for security, just minimal obfuscation.
key, _ := base64.StdEncoding.DecodeString("qIOy76aRcXcxm3vb82tvZqW6JoYnpncgVKx7qej1y+4=")
iv, _ := base64.StdEncoding.DecodeString("okRtW8z6Mx04Y9yMk1cb5w==")
garb, _ := base64.StdEncoding.DecodeString("ut8AtS6re1g7m/onk0ciIq7OxNOdZ/tsQ5ay6OfxKcARnBGY0bQ+pA==")
c, _ := aes.NewCipher(key)
d := cipher.NewCFBDecrypter(c, iv)
t := make([]byte, len(garb))
d.XORKeyStream(t, garb)
hc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: string(t)}))
client = github.NewClient(hc)
// get current version if in travis build
if tc := os.Getenv("TRAVIS_COMMIT"); tc != "" {
commitish = tc
}
}