mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
41
magefile.go
41
magefile.go
@@ -185,42 +185,15 @@ func TestRace() error {
|
||||
|
||||
// Run gofmt linter
|
||||
func Fmt() error {
|
||||
if !isGoLatest() {
|
||||
if !isGoLatest() && !isUnix() {
|
||||
return nil
|
||||
}
|
||||
pkgs, err := hugoPackages()
|
||||
s, err := sh.Output("./check_gofmt.sh")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
failed := false
|
||||
first := true
|
||||
for _, pkg := range pkgs {
|
||||
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, f := range files {
|
||||
// gofmt doesn't exit with non-zero when it finds unformatted code
|
||||
// so we have to explicitly look for output, and if we find any, we
|
||||
// should fail this target.
|
||||
s, err := sh.Output("gofmt", "-l", f)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
|
||||
failed = true
|
||||
}
|
||||
if s != "" {
|
||||
if first {
|
||||
fmt.Println("The following files are not gofmt'ed:")
|
||||
first = false
|
||||
}
|
||||
failed = true
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
if failed {
|
||||
return errors.New("improperly formatted go files")
|
||||
fmt.Println(s)
|
||||
return fmt.Errorf("gofmt needs to be run: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -332,6 +305,10 @@ func isGoLatest() bool {
|
||||
return strings.Contains(runtime.Version(), "1.21")
|
||||
}
|
||||
|
||||
func isUnix() bool {
|
||||
return runtime.GOOS != "windows"
|
||||
}
|
||||
|
||||
func isCI() bool {
|
||||
return os.Getenv("CI") != ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user