mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Build scripts for generate / travis validation
This commit is contained in:
84
build/validate.go
Normal file
84
build/validate.go
Normal file
@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var status = ""
|
||||
|
||||
func appendErrorStatus(s string) {
|
||||
if status != "" {
|
||||
status += ", "
|
||||
}
|
||||
status += s
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := checkGoFmt(); err != nil {
|
||||
fmt.Println(err)
|
||||
appendErrorStatus("needs gofmt")
|
||||
}
|
||||
if err := checkGoGenerate(); err != nil {
|
||||
fmt.Println(err)
|
||||
appendErrorStatus("needs go generate")
|
||||
}
|
||||
}
|
||||
|
||||
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 fmt.Errorf("ERROR: 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 fmt.Errorf("ERROR: The following files were modified by 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
|
||||
}
|
Reference in New Issue
Block a user