github.com/philhug/dnscontrol@v0.2.4-0.20180625181521-921fa9849001/build/validate/validate.go (about) 1 package main 2 3 import ( 4 "context" 5 "crypto/aes" 6 "crypto/cipher" 7 "encoding/base64" 8 "fmt" 9 "os" 10 "os/exec" 11 "strings" 12 13 "github.com/google/go-github/github" 14 "github.com/pkg/errors" 15 16 "golang.org/x/oauth2" 17 ) 18 19 func main() { 20 failed := false 21 22 run := func(ctx string, preStatus string, goodStatus string, f func() error) { 23 setStatus(stPending, preStatus, ctx) 24 if err := f(); err != nil { 25 fmt.Println(err) 26 setStatus(stError, err.Error(), ctx) 27 failed = true 28 } else { 29 setStatus(stSuccess, goodStatus, ctx) 30 } 31 } 32 33 run("gofmt", "Checking gofmt", "gofmt ok", checkGoFmt) 34 run("gogen", "Checking go generate", "go generate ok", checkGoGenerate) 35 if failed { 36 os.Exit(1) 37 } 38 } 39 40 func checkGoFmt() error { 41 cmd := exec.Command("gofmt", "-s", "-l", ".") 42 out, err := cmd.CombinedOutput() 43 if err != nil { 44 return err 45 } 46 if len(out) == 0 { 47 return nil 48 } 49 files := strings.Split(string(out), "\n") 50 fList := "" 51 for _, f := range files { 52 if strings.HasPrefix(f, "vendor") { 53 continue 54 } 55 if fList != "" { 56 fList += "\n" 57 } 58 fList += f 59 } 60 if fList == "" { 61 return nil 62 } 63 return errors.Errorf("The following files need to have gofmt run on them:\n%s", fList) 64 } 65 66 func checkGoGenerate() error { 67 cmd := exec.Command("go", "generate") 68 cmd.Stdout = os.Stdout 69 cmd.Stderr = os.Stderr 70 err := cmd.Run() 71 if err != nil { 72 return err 73 } 74 modified, err := getModifiedFiles() 75 if err != nil { 76 return err 77 } 78 if len(modified) != 0 { 79 return errors.Errorf("ERROR: The following files are modified after go generate:\n%s", strings.Join(modified, "\n")) 80 } 81 return nil 82 } 83 84 func getModifiedFiles() ([]string, error) { 85 cmd := exec.Command("git", strings.Split("diff --name-only", " ")...) 86 out, err := cmd.CombinedOutput() 87 if err != nil { 88 return nil, err 89 } 90 if len(out) == 0 { 91 return nil, nil 92 } 93 return strings.Split(string(out), "\n"), nil 94 } 95 96 const ( 97 stPending = "pending" 98 stSuccess = "success" 99 stError = "error" 100 ) 101 102 func setStatus(status string, desc string, ctx string) { 103 if commitish == "" || ctx == "" { 104 return 105 } 106 client.Repositories.CreateStatus(context.Background(), "StackExchange", "dnscontrol", commitish, &github.RepoStatus{ 107 Context: &ctx, 108 Description: &desc, 109 State: &status, 110 }) 111 } 112 113 var client *github.Client 114 var commitish string 115 116 func init() { 117 // not intended for security, just minimal obfuscation. 118 key, _ := base64.StdEncoding.DecodeString("qIOy76aRcXcxm3vb82tvZqW6JoYnpncgVKx7qej1y+4=") 119 iv, _ := base64.StdEncoding.DecodeString("okRtW8z6Mx04Y9yMk1cb5w==") 120 garb, _ := base64.StdEncoding.DecodeString("ut8AtS6re1g7m/onk0ciIq7OxNOdZ/tsQ5ay6OfxKcARnBGY0bQ+pA==") 121 c, _ := aes.NewCipher(key) 122 d := cipher.NewCFBDecrypter(c, iv) 123 t := make([]byte, len(garb)) 124 d.XORKeyStream(t, garb) 125 hc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: string(t)})) 126 client = github.NewClient(hc) 127 128 // get current version if in travis build 129 if tc := os.Getenv("TRAVIS_COMMIT"); tc != "" { 130 commitish = tc 131 } 132 }