Add fmt command

This commit is contained in:
kolaente 2020-09-01 16:53:00 +02:00
parent 9006b6f49c
commit db03a45165
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 25 additions and 1 deletions

View File

@ -20,6 +20,7 @@ package main
import (
"bufio"
"bytes"
"fmt"
"github.com/magefile/mage/mg"
"os"
@ -45,10 +46,13 @@ var (
BinLocation = ""
PkgVersion = "master"
ApiPackages = []string{}
RootPath = ""
GoFiles = []string{}
// Aliases are mage aliases of targets
Aliases = map[string]interface{}{
"integration-test": IntegrationTest,
"fmt-check": FmtCheck,
}
)
@ -114,6 +118,20 @@ func setRootPath() {
fmt.Printf("Error setting root path: %s\n", err)
os.Exit(1)
}
RootPath = pwd
}
func setGoFiles() {
// GOFILES := $(shell find . -name "*.go" -type f ! -path "*/bindata.go")
cmd := exec.Command("find", ".", "-name", "*.go", "-type", "f", "!", "-path", "*/bindata.go")
files, err := cmd.Output()
if err != nil {
fmt.Printf("Error getting go files: %s\n", err)
os.Exit(1)
}
for _, f := range strings.Split(string(files), "\n") {
GoFiles = append(GoFiles, RootPath+strings.TrimLeft(f, "."))
}
}
func init() {
@ -124,6 +142,7 @@ func init() {
setPkgVersion()
setApiPackages()
setRootPath()
setGoFiles()
Ldflags = `-X "` + PACKAGE + `/pkg/version.VersionNumber=` + VersionNumber + `" -X "main.Tags=` + Tags + `"`
}
@ -131,10 +150,13 @@ func runAndStreamOutput(cmd string, args ...string) {
c := exec.Command(cmd, args...)
c.Env = os.Environ()
c.Dir = RootPath
fmt.Printf("%s\n\n", c.String())
stdout, _ := c.StdoutPipe()
errbuf := bytes.Buffer{}
c.Stderr = &errbuf
c.Start()
reader := bufio.NewReader(stdout)
@ -145,6 +167,7 @@ func runAndStreamOutput(cmd string, args ...string) {
}
if err := c.Wait(); err != nil {
fmt.Printf(errbuf.String())
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
@ -188,7 +211,8 @@ func IntegrationTest() {
// Formats the code using go fmt
func Fmt() {
args := append([]string{"-s", "-w"}, GoFiles...)
runAndStreamOutput("gofmt", args...)
}
// Checks if the code is properly formatted with go fmt