github.com/inturn/pre-commit-gobuild@v1.0.12/hooks/go-build/go-build.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/inturn/pre-commit-gobuild/internal/helpers"
    10  )
    11  
    12  func main() {
    13  	wd, err := os.Getwd()
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	dirs := helpers.DirsWith(wd, "\\.go$")
    18  
    19  	for _, d := range dirs {
    20  		if !strings.Contains(d, "/vendor/") {
    21  			relPath := strings.Replace(d, wd, ".", 1)
    22  			cmd := exec.Command("go", "install", relPath)
    23  			out, err := cmd.CombinedOutput()
    24  			outStr := string(out)
    25  			if err != nil {
    26  				if !strings.Contains(outStr, "no non-test Go files") && !strings.Contains(outStr, "build constraints exclude all Go files") {
    27  					fmt.Println(fmt.Sprintf("building %s finished with error %s", relPath, err.Error()))
    28  					fmt.Printf(string(out))
    29  					os.Exit(1)
    30  				}
    31  			} else {
    32  				fmt.Println(fmt.Sprintf("building %s finished ok", relPath))
    33  			}
    34  		}
    35  	}
    36  }