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