2017-01-12 22:20:37 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-20 03:24:53 +08:00
|
|
|
"context"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"encoding/base64"
|
2017-01-12 22:20:37 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-02 23:04:42 +08:00
|
|
|
"github.com/google/go-github/v35/github"
|
2017-04-20 03:24:53 +08:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
2017-01-12 22:20:37 +08:00
|
|
|
|
|
|
|
func main() {
|
2017-04-20 03:24:53 +08:00
|
|
|
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)
|
|
|
|
}
|
2017-01-12 22:20:37 +08:00
|
|
|
}
|
2017-04-20 03:24:53 +08:00
|
|
|
|
|
|
|
run("gofmt", "Checking gofmt", "gofmt ok", checkGoFmt)
|
|
|
|
run("gogen", "Checking go generate", "go generate ok", checkGoGenerate)
|
|
|
|
if failed {
|
2017-01-12 22:29:36 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-01-12 22:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("the following files need to have gofmt run on them:\n%s", fList)
|
2017-01-12 22:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
return fmt.Errorf("ERROR: The following files are modified after go generate:\n%s", strings.Join(modified, "\n"))
|
2017-01-12 22:20:37 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2017-04-20 03:24:53 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
stPending = "pending"
|
|
|
|
stSuccess = "success"
|
|
|
|
stError = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setStatus(status string, desc string, ctx string) {
|
2017-09-13 22:00:41 +08:00
|
|
|
if commitish == "" || ctx == "" {
|
2017-04-20 03:24:53 +08:00
|
|
|
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)
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// get current version if in travis build
|
2017-04-20 03:24:53 +08:00
|
|
|
if tc := os.Getenv("TRAVIS_COMMIT"); tc != "" {
|
|
|
|
commitish = tc
|
|
|
|
}
|
|
|
|
}
|