@@ -4,33 +4,27 @@ import (
4
4
"bufio"
5
5
"os"
6
6
"regexp"
7
- "strings"
8
7
9
- "github.com/zeromicro/go-zero/core/logx "
8
+ "github.com/fsgo/go_fmt/gofmtapi "
10
9
"github.com/zeromicro/go-zero/core/mr"
11
- "github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
12
- "github.com/zeromicro/go-zero/tools/goctl/util/console"
13
- "github.com/zeromicro/go-zero/tools/goctl/util/env"
14
10
15
11
"github.com/jzero-io/jzero/config"
16
- "github.com/jzero-io/jzero/pkg"
17
12
"github.com/jzero-io/jzero/pkg/gitstatus"
18
13
)
19
14
20
- /*
21
- Use gofumpt to format go code
22
- */
23
-
24
15
var rxCodeGenerated = regexp .MustCompile (`^// Code generated .* DO NOT EDIT\.$` )
25
16
26
17
func Run () error {
27
- check ()
28
-
29
18
files := getFormatFiles ()
30
19
31
20
files = filterFiles (files )
32
21
33
- return execFormat (files )
22
+ gf := gofmtapi .NewFormatter ()
23
+ opt := gofmtapi .NewOptions ()
24
+ opt .BindFlags ()
25
+
26
+ opt .Files = files
27
+ return gf .Execute (opt )
34
28
}
35
29
36
30
func getFormatFiles () []string {
@@ -40,9 +34,8 @@ func getFormatFiles() []string {
40
34
return files
41
35
}
42
36
return []string {"." }
43
- } else {
44
- return []string {"." }
45
37
}
38
+ return []string {"." }
46
39
}
47
40
48
41
func filterFiles (files []string ) []string {
@@ -62,19 +55,6 @@ func filterFiles(files []string) []string {
62
55
return result
63
56
}
64
57
65
- func execFormat (files []string ) error {
66
- if len (files ) > 0 {
67
- args := []string {"gofumpt" , "-l" , "-w" }
68
- args = append (args , files ... )
69
- logx .Debugf ("execute command: %s" , strings .Join (args , " " ))
70
- err := pkg .Run (strings .Join (args , " " ), "" )
71
- if err != nil {
72
- return err
73
- }
74
- }
75
- return nil
76
- }
77
-
78
58
func readFirstLine (filePath string ) (string , error ) {
79
59
file , err := os .Open (filePath )
80
60
if err != nil {
@@ -89,19 +69,3 @@ func readFirstLine(filePath string) (string, error) {
89
69
90
70
return "" , scanner .Err ()
91
71
}
92
-
93
- func check () {
94
- log := console .NewColorConsole (true )
95
-
96
- // install goctl
97
- _ , err := env .LookPath ("gofumpt" )
98
- if err != nil {
99
- log .Warning (`[jzero-env]: gofumpt is not found in PATH` )
100
- if err = golang .Install ("go install mvdan.cc/gofumpt@latest" ); err != nil {
101
- log .Fatalln ("[jzero-env]: gofumpt is not installed, please install it by running: go install mvdan.cc/gofumpt@latest" )
102
- }
103
- }
104
- if _ , err = env .LookPath ("gofumpt" ); err != nil {
105
- log .Fatalln ("[jzero-env]: env check failed, gofumpt is not installed" )
106
- }
107
- }
0 commit comments