Skip to content

Commit 79bc8d5

Browse files
committed
feat(format): update format
1 parent 1f2b144 commit 79bc8d5

File tree

3 files changed

+14
-44
lines changed

3 files changed

+14
-44
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Masterminds/sprig/v3 v3.3.0
77
github.com/a8m/envsubst v1.4.2
88
github.com/dave/dst v0.27.3
9+
github.com/fsgo/go_fmt v0.6.3
910
github.com/go-git/go-git/v5 v5.13.2
1011
github.com/golang-migrate/migrate/v4 v4.18.2
1112
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1
@@ -62,6 +63,7 @@ require (
6263
github.com/go-sql-driver/mysql v1.9.0 // indirect
6364
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
6465
github.com/golang/protobuf v1.5.4 // indirect
66+
github.com/google/go-cmp v0.6.0 // indirect
6567
github.com/google/uuid v1.6.0 // indirect
6668
github.com/gookit/color v1.5.4 // indirect
6769
github.com/hashicorp/errwrap v1.1.0 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
7272
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
7373
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
7474
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
75+
github.com/fsgo/fst v0.0.5 h1:c12J39shorNiS3X9QsK6sg/KUzw8FOA3IoKPb/upj7E=
76+
github.com/fsgo/fst v0.0.5/go.mod h1:vNB0la0LICDwsMuwD7KR8NNDnslYyH/1x1+fOamXra8=
77+
github.com/fsgo/go_fmt v0.6.3 h1:JFm6d78v9MxqRk5KmvFK/a1djqyS2lhBYCgYc7pBYBw=
78+
github.com/fsgo/go_fmt v0.6.3/go.mod h1:4fGqOpLBa2PNg8L3x4X/ojg92bejdJ8Y0oRKf/dWNVc=
7579
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
7680
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
7781
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=

internal/format/formatgo/run.go

+8-44
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,27 @@ import (
44
"bufio"
55
"os"
66
"regexp"
7-
"strings"
87

9-
"github.com/zeromicro/go-zero/core/logx"
8+
"github.com/fsgo/go_fmt/gofmtapi"
109
"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"
1410

1511
"github.com/jzero-io/jzero/config"
16-
"github.com/jzero-io/jzero/pkg"
1712
"github.com/jzero-io/jzero/pkg/gitstatus"
1813
)
1914

20-
/*
21-
Use gofumpt to format go code
22-
*/
23-
2415
var rxCodeGenerated = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
2516

2617
func Run() error {
27-
check()
28-
2918
files := getFormatFiles()
3019

3120
files = filterFiles(files)
3221

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)
3428
}
3529

3630
func getFormatFiles() []string {
@@ -40,9 +34,8 @@ func getFormatFiles() []string {
4034
return files
4135
}
4236
return []string{"."}
43-
} else {
44-
return []string{"."}
4537
}
38+
return []string{"."}
4639
}
4740

4841
func filterFiles(files []string) []string {
@@ -62,19 +55,6 @@ func filterFiles(files []string) []string {
6255
return result
6356
}
6457

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-
7858
func readFirstLine(filePath string) (string, error) {
7959
file, err := os.Open(filePath)
8060
if err != nil {
@@ -89,19 +69,3 @@ func readFirstLine(filePath string) (string, error) {
8969

9070
return "", scanner.Err()
9171
}
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

Comments
 (0)