Skip to content

Commit 2be569c

Browse files
author
yangyile
committed
把获取模块路径的逻辑从lint包里搬过来
1 parent 3fab532 commit 2be569c

File tree

14 files changed

+286
-104
lines changed

14 files changed

+286
-104
lines changed

go.mod

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ require (
66
github.com/emirpasic/gods/v2 v2.0.0-alpha
77
github.com/spf13/cobra v1.9.1
88
github.com/stretchr/testify v1.10.0
9-
github.com/yyle88/erero v1.0.21
9+
github.com/yyle88/erero v1.0.23
1010
github.com/yyle88/must v0.0.25
1111
github.com/yyle88/neatjson v0.0.12
1212
github.com/yyle88/osexec v0.0.31
1313
github.com/yyle88/osexistpath v0.0.17
1414
github.com/yyle88/runpath v1.0.23
15-
github.com/yyle88/zaplog v0.0.24
15+
github.com/yyle88/zaplog v0.0.26
1616
)
1717

1818
require (
@@ -23,14 +23,13 @@ require (
2323
github.com/spf13/pflag v1.0.6 // indirect
2424
github.com/yyle88/done v1.0.26 // indirect
2525
github.com/yyle88/eroticgo v0.0.4 // indirect
26-
github.com/yyle88/mutexmap v1.0.13 // indirect
26+
github.com/yyle88/mutexmap v1.0.14 // indirect
2727
github.com/yyle88/printgo v1.0.5 // indirect
2828
github.com/yyle88/rese v0.0.10 // indirect
29-
github.com/yyle88/sure v0.0.38 // indirect
30-
github.com/yyle88/syntaxgo v0.0.52 // indirect
29+
github.com/yyle88/sure v0.0.39 // indirect
30+
github.com/yyle88/syntaxgo v0.0.53 // indirect
3131
github.com/yyle88/tern v0.0.8 // indirect
3232
go.uber.org/multierr v1.11.0 // indirect
3333
go.uber.org/zap v1.27.0 // indirect
34-
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
3534
gopkg.in/yaml.v3 v3.0.1 // indirect
3635
)

go.sum

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
2424
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2525
github.com/yyle88/done v1.0.26 h1:lncTfGVFZWDV+ni9kgJWXjwl9qryVrX5xBYKW1Os1yg=
2626
github.com/yyle88/done v1.0.26/go.mod h1:Y/QHJS7guYtGMnbar4SHXGUtwUqckomTmRBaCcwW/y4=
27-
github.com/yyle88/erero v1.0.21 h1:SlVxmw1PsIXKkx23MojbrkYT4LtLEvSx+ZbDCDJ6ijE=
28-
github.com/yyle88/erero v1.0.21/go.mod h1:JH9yodnM7L9/0mCIkwwSXinLG1KY9xaaPGZRShJNTfw=
27+
github.com/yyle88/erero v1.0.23 h1:AY5grHGm+CgwCzPjn60LT7APCLSCuaHhWu2uOle6Nk0=
28+
github.com/yyle88/erero v1.0.23/go.mod h1:ZbUp//iNppPtn4yxilmlJHbpZ5U2ycipje6/SkMYHo4=
2929
github.com/yyle88/eroticgo v0.0.4 h1:ovmaRGWM8iN6P2uGlGNXaP8dVoWloLvYmC5Ta9rBNnM=
3030
github.com/yyle88/eroticgo v0.0.4/go.mod h1:e748J+JnGHnPrHVExVb14VzmYjaJelyhnAzzi5JF/GQ=
31-
github.com/yyle88/formatgo v1.0.24 h1:jdH+LkQ8vDozh/1YM5TnIUKN3okts90E/UgNBaqpY/w=
32-
github.com/yyle88/formatgo v1.0.24/go.mod h1:0Iaj84Kw5slGwGMXK46EyHpsiutpA6e2Lct+J5MOooY=
31+
github.com/yyle88/formatgo v1.0.25 h1:KbrrFte9ngOO5nxnEAl/APyGBKwmMdMbc9b+isRGMfQ=
32+
github.com/yyle88/formatgo v1.0.25/go.mod h1:LICth0xTd1MjDHrKYHxrghqyHDp7QMoiKJlr2Mqihdw=
3333
github.com/yyle88/must v0.0.25 h1:SF1wAYNTfupFNfnXJcnizLZAJtCUCJf2dUVY/9nvtUQ=
3434
github.com/yyle88/must v0.0.25/go.mod h1:SE9pZwrZLt8+wnWJJEAve5QKOm8rz3lSxokUxGGgemo=
35-
github.com/yyle88/mutexmap v1.0.13 h1:wp/6mv/gsC2DSqNbhMYiil4gtnb0sDCIhHHIHK2h0cQ=
36-
github.com/yyle88/mutexmap v1.0.13/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
35+
github.com/yyle88/mutexmap v1.0.14 h1:aBdhtKR0XmFAJFoyswfjAEg9dzBvdaUBXU3Iw50AlB0=
36+
github.com/yyle88/mutexmap v1.0.14/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
3737
github.com/yyle88/neatjson v0.0.12 h1:M6y4IsHbe2/3drF/kDl3zBpaN29lmfW4MetEQcoG04A=
3838
github.com/yyle88/neatjson v0.0.12/go.mod h1:LT3nIhKyB3lkD3INiIXCN3FejNu+g+qvzCJ+fZSZaRc=
3939
github.com/yyle88/osexec v0.0.31 h1:nMJLAv7r0mB1wKrD7njA+Kf2qlujtDDPu+SXy80Z4a0=
@@ -46,14 +46,14 @@ github.com/yyle88/rese v0.0.10 h1:rMEcybaIe/CNVMmilgor3nMQWB4TcJgXRttvDjKtvLg=
4646
github.com/yyle88/rese v0.0.10/go.mod h1:2jCdwu7qpJDdZqVulM4+zz/qiSxJWbOK/ndzQhg+5G0=
4747
github.com/yyle88/runpath v1.0.23 h1:VEdeaMXfvFY7CuF9Nceuxb6HSNDcmiSSGEf+zT7O900=
4848
github.com/yyle88/runpath v1.0.23/go.mod h1:JRGxn/0Ytg6CvGoE2VrO74oX8Lu4jbOPZDKxr8tzPEg=
49-
github.com/yyle88/sure v0.0.38 h1:+yw3++UKizkh7JsBtoZKH6jC2K1Jr4Z2U088zYBLJ/w=
50-
github.com/yyle88/sure v0.0.38/go.mod h1:zkSgOlFeW4xETRZNoR0GZ2v/q7wxnlHl3dxI4f4tftI=
51-
github.com/yyle88/syntaxgo v0.0.52 h1:G3A9MyTHp82j1Y2LPYqtD3lv/slutJ9/nVW/yi4XG4E=
52-
github.com/yyle88/syntaxgo v0.0.52/go.mod h1:wODs8eDwW75ARUJ9PBDBxOvot/Gj01gXPmsFQouTWsU=
49+
github.com/yyle88/sure v0.0.39 h1:uYPNFZekzJtsF0ZlULK8I/MYfFktCqG7/+9BeHzyN7E=
50+
github.com/yyle88/sure v0.0.39/go.mod h1:GVXwOj6Iw6+wdPg/H2dl0/jeAuhGYO2UTzWANztafdQ=
51+
github.com/yyle88/syntaxgo v0.0.53 h1:3W4S5ncRdq3hUp3Qjw4GqB+mAxypJCycMo/mMJP+1vc=
52+
github.com/yyle88/syntaxgo v0.0.53/go.mod h1:68EidTlDxVi/iaCJeg0menpA4v/xbq+ITxa1aKdmjLo=
5353
github.com/yyle88/tern v0.0.8 h1:Qn6dpYNbYA8bhPX/SjQHnML0MIoKScGNRgGH+VFox1U=
5454
github.com/yyle88/tern v0.0.8/go.mod h1:g9weyOMLtXYyt37EAshZPUTTHdH7WFauuxDi4oPr7/8=
55-
github.com/yyle88/zaplog v0.0.24 h1:8/G2Id1H2wSiQKav4e4vipB8LFeFlIYicTsv4sQO6wQ=
56-
github.com/yyle88/zaplog v0.0.24/go.mod h1:3eaCIi66BgCHh5IIbHlvuRWJPXz89m/EDPfqLe7p6Us=
55+
github.com/yyle88/zaplog v0.0.26 h1:NXLMlLLXF88CGfJiCFSirjiXFTNcHr5FgsPBwrr96bw=
56+
github.com/yyle88/zaplog v0.0.26/go.mod h1:0QHBqSYOkJ1giM4CDELhMGuMum3pbs29wG1LI+lQ8UE=
5757
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5858
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
5959
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
@@ -71,7 +71,5 @@ golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY
7171
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7272
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
7373
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
74-
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
75-
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
7674
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7775
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

goworkcmd/goworkcmd_test.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

internal/demos/demo1x/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"github.com/go-mate/go-work/goworkcmd"
54
"github.com/go-mate/go-work/worksexec"
65
"github.com/go-mate/go-work/workspace"
6+
"github.com/go-mate/go-work/worksubcmd"
77
"github.com/spf13/cobra"
88
"github.com/yyle88/must"
99
"github.com/yyle88/osexec"
@@ -19,13 +19,13 @@ func main() {
1919
projectPath := runpath.PARENT.Up(3)
2020
zaplog.SUG.Debugln(projectPath)
2121

22-
wsp := workspace.NewWorkspace("", []string{projectPath})
22+
workSpace := workspace.NewWorkSpace([]string{projectPath})
2323

2424
commandConfig := osexec.NewCommandConfig()
2525
commandConfig.WithBash()
2626
commandConfig.WithDebug()
2727

28-
wse := worksexec.NewWorksExec(commandConfig, []*workspace.Workspace{wsp})
28+
worksExec := worksexec.NewWorksExec(commandConfig, []*workspace.Workspace{workSpace})
2929

3030
// 定义根命令
3131
var rootCmd = &cobra.Command{
@@ -36,8 +36,8 @@ func main() {
3636
zaplog.LOG.Info("run")
3737
},
3838
}
39-
rootCmd.AddCommand(goworkcmd.NewWorkCmd(wse))
40-
rootCmd.AddCommand(goworkcmd.NewModCmd(wse))
39+
rootCmd.AddCommand(worksubcmd.NewWorkCmd(worksExec))
40+
rootCmd.AddCommand(worksubcmd.NewModCmd(worksExec))
4141

4242
must.Done(rootCmd.Execute())
4343
}

internal/utils/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package utils
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/yyle88/osexistpath/osomitexist"
7+
)
8+
9+
func GetProjectPath(currentPath string) (string, string, bool) {
10+
projectPath := currentPath
11+
shortMiddle := ""
12+
for !osomitexist.IsFile(filepath.Join(projectPath, "go.mod")) {
13+
subName := filepath.Base(projectPath)
14+
15+
prePath := filepath.Dir(projectPath)
16+
if prePath == projectPath {
17+
return "", "", false
18+
}
19+
projectPath = prePath
20+
shortMiddle = filepath.Join(subName, shortMiddle)
21+
}
22+
return projectPath, shortMiddle, true
23+
}

internal/utils/utils_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package utils
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"github.com/yyle88/runpath"
9+
)
10+
11+
func TestGetProjectPath(t *testing.T) {
12+
path := runpath.PARENT.Path()
13+
t.Log(path)
14+
projectPath, shortMiddle, isGoModule := GetProjectPath(path)
15+
require.True(t, isGoModule)
16+
t.Log(projectPath)
17+
t.Log(shortMiddle)
18+
19+
require.Equal(t, path, filepath.Join(projectPath, shortMiddle))
20+
}

modulepath/module_path.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package modulepath
2+
3+
import (
4+
"io/fs"
5+
"path/filepath"
6+
"strings"
7+
8+
"github.com/emirpasic/gods/v2/sets/linkedhashset"
9+
"github.com/go-mate/go-work/internal/utils"
10+
"github.com/yyle88/must"
11+
"github.com/yyle88/neatjson/neatjsons"
12+
"github.com/yyle88/osexistpath/osmustexist"
13+
"github.com/yyle88/zaplog"
14+
)
15+
16+
func GetProjectPath(currentPath string) (string, string, bool) {
17+
return utils.GetProjectPath(currentPath)
18+
}
19+
20+
func GetModulePaths(currentPath string, options *Options) []string {
21+
set := linkedhashset.New[string]()
22+
23+
if options.IncludeHere {
24+
projectPath, shortMiddle, isGoModule := GetProjectPath(currentPath)
25+
if !isGoModule {
26+
must.None(projectPath)
27+
must.None(shortMiddle)
28+
} else {
29+
set.Add(currentPath) //把当前目录添加到需要lint的目录里
30+
}
31+
}
32+
33+
//这里很有可能,当前目录下就是 go.mod 文件,就是把当前目录设置两次,因此使用 hash-set 去重复
34+
must.Done(filepath.Walk(currentPath, func(path string, info fs.FileInfo, err error) error {
35+
if exSkip, isHide := isHidePath(info); isHide {
36+
return exSkip
37+
}
38+
if !info.IsDir() && info.Name() == "go.mod" {
39+
if subRoot := filepath.Dir(path); osmustexist.IsRoot(subRoot) {
40+
set.Add(subRoot)
41+
}
42+
return nil
43+
}
44+
return nil
45+
}))
46+
if options.debugMode {
47+
zaplog.SUG.Debugln(neatjsons.S(set))
48+
}
49+
50+
if options.ExcludeNoGo {
51+
//但是有些项目里是没有go文件的,比如空项目,或者大项目里只有子项目,而没有逻辑,因此需要去除
52+
set = set.Select(func(index int, value string) bool {
53+
if options.debugMode {
54+
zaplog.SUG.Debugln(index, value)
55+
}
56+
return hasGoFiles(value)
57+
})
58+
}
59+
60+
if options.debugMode {
61+
zaplog.SUG.Debugln(neatjsons.S(set))
62+
}
63+
roots := set.Values()
64+
return roots
65+
}
66+
67+
func hasGoFiles(root string) bool {
68+
existGo := false
69+
must.Done(filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
70+
if exSkip, isHide := isHidePath(info); isHide {
71+
return exSkip
72+
}
73+
74+
if info.IsDir() {
75+
//当遇到其它项目的 go.mod 时结束(由于传进来的就是项目根目录,因此要排除当前目录)
76+
if path != root && osmustexist.IsFile(filepath.Join(path, "go.mod")) {
77+
return filepath.SkipDir
78+
}
79+
} else {
80+
if filepath.Ext(info.Name()) == ".go" {
81+
existGo = true
82+
return filepath.SkipAll
83+
}
84+
}
85+
return nil
86+
}))
87+
return existGo
88+
}
89+
90+
func isHidePath(info fs.FileInfo) (error, bool) {
91+
if info.IsDir() {
92+
if strings.HasPrefix(info.Name(), ".") {
93+
return filepath.SkipDir, true
94+
}
95+
} else {
96+
if strings.HasPrefix(info.Name(), ".") {
97+
return nil, true
98+
}
99+
}
100+
return nil, false
101+
}
102+
103+
type Options struct {
104+
IncludeHere bool // 假如当前项目是go项目时,是否当前项目
105+
ExcludeNoGo bool
106+
debugMode bool
107+
}
108+
109+
func NewOptions() *Options {
110+
return &Options{
111+
IncludeHere: true, //要包含当前目录
112+
ExcludeNoGo: true, //不包含没有 go 文件的目录
113+
debugMode: false,
114+
}
115+
}
116+
117+
func (c *Options) WithIncludeHere(includeHere bool) *Options {
118+
c.IncludeHere = includeHere
119+
return c
120+
}
121+
122+
func (c *Options) WithExcludeNoGo(excludeNoGo bool) *Options {
123+
c.ExcludeNoGo = excludeNoGo
124+
return c
125+
}
126+
127+
func (c *Options) WithDebugMode(debugMode bool) *Options {
128+
c.debugMode = debugMode
129+
return c
130+
}

modulepath/module_path_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package modulepath
2+
3+
import (
4+
"testing"
5+
6+
"github.com/yyle88/runpath"
7+
)
8+
9+
func TestGetModulePaths(t *testing.T) {
10+
paths := GetModulePaths(runpath.PARENT.Path(), NewOptions().WithDebugMode(true))
11+
t.Log(paths)
12+
}

0 commit comments

Comments
 (0)