-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.go
81 lines (64 loc) · 1.31 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package zmage
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/magefile/mage/sh"
)
var goFileData = struct {
data []string
err error
once sync.Once
}{}
func goFiles() ([]string, error) {
goFileData.err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if path == "." {
return nil
}
base := filepath.Base(path)
if base == vendor {
return filepath.SkipDir
}
if strings.HasPrefix(base, ".") {
return filepath.SkipDir
}
if strings.HasPrefix(base, "_") {
return filepath.SkipDir
}
}
if filepath.Ext(path) == ".go" {
goFileData.data = append(goFileData.data, "."+string(filepath.Separator)+path)
}
return nil
})
return goFileData.data, goFileData.err
}
func GoFmt() error {
files, err := goFiles()
if err != nil {
return err
}
var fmtFiles []string
for _, file := range files {
if strings.HasSuffix(file, ".pb.go") {
continue
}
fmtFiles = append(fmtFiles, file)
}
s, err := sh.Output("gofmt", append([]string{"-l", "-s"}, fmtFiles...)...)
if err != nil {
return err
}
if s != "" {
fmt.Fprintln(os.Stderr, "The following files need `gofmt -s`:")
fmt.Fprintln(os.Stderr, s)
return fmt.Errorf("improperly formatted go files")
}
return nil
}