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