Add fmt command
This commit is contained in:
parent
9006b6f49c
commit
db03a45165
26
magefile.go
26
magefile.go
|
@ -20,6 +20,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/magefile/mage/mg"
|
"github.com/magefile/mage/mg"
|
||||||
"os"
|
"os"
|
||||||
|
@ -45,10 +46,13 @@ var (
|
||||||
BinLocation = ""
|
BinLocation = ""
|
||||||
PkgVersion = "master"
|
PkgVersion = "master"
|
||||||
ApiPackages = []string{}
|
ApiPackages = []string{}
|
||||||
|
RootPath = ""
|
||||||
|
GoFiles = []string{}
|
||||||
|
|
||||||
// Aliases are mage aliases of targets
|
// Aliases are mage aliases of targets
|
||||||
Aliases = map[string]interface{}{
|
Aliases = map[string]interface{}{
|
||||||
"integration-test": IntegrationTest,
|
"integration-test": IntegrationTest,
|
||||||
|
"fmt-check": FmtCheck,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -114,6 +118,20 @@ func setRootPath() {
|
||||||
fmt.Printf("Error setting root path: %s\n", err)
|
fmt.Printf("Error setting root path: %s\n", err)
|
||||||
os.Exit(1)
|
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() {
|
func init() {
|
||||||
|
@ -124,6 +142,7 @@ func init() {
|
||||||
setPkgVersion()
|
setPkgVersion()
|
||||||
setApiPackages()
|
setApiPackages()
|
||||||
setRootPath()
|
setRootPath()
|
||||||
|
setGoFiles()
|
||||||
Ldflags = `-X "` + PACKAGE + `/pkg/version.VersionNumber=` + VersionNumber + `" -X "main.Tags=` + Tags + `"`
|
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 := exec.Command(cmd, args...)
|
||||||
|
|
||||||
c.Env = os.Environ()
|
c.Env = os.Environ()
|
||||||
|
c.Dir = RootPath
|
||||||
|
|
||||||
fmt.Printf("%s\n\n", c.String())
|
fmt.Printf("%s\n\n", c.String())
|
||||||
|
|
||||||
stdout, _ := c.StdoutPipe()
|
stdout, _ := c.StdoutPipe()
|
||||||
|
errbuf := bytes.Buffer{}
|
||||||
|
c.Stderr = &errbuf
|
||||||
c.Start()
|
c.Start()
|
||||||
|
|
||||||
reader := bufio.NewReader(stdout)
|
reader := bufio.NewReader(stdout)
|
||||||
|
@ -145,6 +167,7 @@ func runAndStreamOutput(cmd string, args ...string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Wait(); err != nil {
|
if err := c.Wait(); err != nil {
|
||||||
|
fmt.Printf(errbuf.String())
|
||||||
fmt.Printf("Error: %s\n", err)
|
fmt.Printf("Error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -188,7 +211,8 @@ func IntegrationTest() {
|
||||||
|
|
||||||
// Formats the code using go fmt
|
// Formats the code using go fmt
|
||||||
func Fmt() {
|
func Fmt() {
|
||||||
|
args := append([]string{"-s", "-w"}, GoFiles...)
|
||||||
|
runAndStreamOutput("gofmt", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if the code is properly formatted with go fmt
|
// Checks if the code is properly formatted with go fmt
|
||||||
|
|
Loading…
Reference in New Issue