@@ -53,6 +53,7 @@ type Output struct {
5353 PackageInfo * uast.PackagePathInfo `json:"packageInfo"`
5454 ModuleName string `json:"moduleName"`
5555 GoModPath string `json:"goModPath"`
56+ NumOfGoMod int `json:"numOfGoMod"`
5657}
5758
5859func main () {
@@ -88,7 +89,7 @@ func parseSingleFile(file string, output string) {
8889 packages := make (map [string ]* ast.Package )
8990 packages ["__single__" ] = pkg
9091
91- buildAndPrint ("__single_module__" , packages , fset , output , "" )
92+ buildAndPrint ("__single_module__" , packages , fset , output , "" , 0 )
9293}
9394
9495func parseGoModule (rootDir string , output string ) {
@@ -97,59 +98,70 @@ func parseGoModule(rootDir string, output string) {
9798 var moduleName string
9899 // Read the module name from go.mod
99100 // Find the go.mod file
100- goModPath , err := findGoMod (rootDir )
101+ goModPaths , err := findAllGoMod (rootDir )
101102 if err != nil {
102103 moduleName = "__unknown_module__"
103104 } else {
104- name , _ := readModuleName (goModPath )
105+ name , _ := readModuleName (goModPaths [ 0 ] )
105106 moduleName = name
106107 }
107108
108109 packages , _ := preparePackage (moduleName , rootDir , fset )
109110 //if err != nil {
110111 // panic(err)
111112 //}
112- buildAndPrint (moduleName , packages , fset , output , goModPath )
113+ //默认取找到的第一个go.mod
114+ firstGoModPath := ""
115+ if goModPaths != nil {
116+ firstGoModPath = goModPaths [0 ]
117+ }
118+ buildAndPrint (moduleName , packages , fset , output , firstGoModPath , len (goModPaths ))
113119}
114120
115121// findGoMod searches for a go.mod file starting from dir and recursing into subdirectories if not found
116- func findGoMod (dir string ) (string , error ) {
122+ func findAllGoMod (dir string ) ([] string , error ) {
117123 if strings .Contains (dir , "/vendor" ) {
118- return "" , fmt .Errorf ("find vendor" )
124+ return nil , fmt .Errorf ("find vendor" )
119125 }
120126 const goModFileName = "go.mod"
121127
128+ var paths []string
129+
122130 // Check if go.mod exists in the current directory
123131 goModPath := filepath .Join (dir , goModFileName )
124- if _ , err := os .Stat (goModPath ); ! os . IsNotExist ( err ) {
125- return goModPath , nil
132+ if _ , err := os .Stat (goModPath ); err == nil {
133+ paths = append ( paths , goModPath )
126134 }
127135
128136 // If not found, recurse into subdirectories
129137 entries , err := os .ReadDir (dir )
130138 if err != nil {
131- return "" , err
139+ return nil , err
132140 }
133141
134142 for _ , entry := range entries {
135143 if entry .IsDir () {
136144 subDir := filepath .Join (dir , entry .Name ())
137- goModPath , err := findGoMod (subDir )
138- if err == nil {
139- return goModPath , nil
145+ subPaths , err := findAllGoMod (subDir )
146+ if err == nil && len ( subPaths ) > 0 {
147+ paths = append ( paths , subPaths ... )
140148 }
141149 }
142150 }
143-
144- return "" , fmt .Errorf ("go.mod not found in directory or subdirectories: %s" , dir )
151+ if paths != nil {
152+ return paths , nil
153+ } else {
154+ return nil , fmt .Errorf ("not found go.mod" )
155+ }
145156}
146157
147- func buildAndPrint (moduleName string , packages map [string ]* ast.Package , fset * token.FileSet , outputPath string , goModPath string ) {
158+ func buildAndPrint (moduleName string , packages map [string ]* ast.Package , fset * token.FileSet , outputPath string , goModPath string , numOfGoMod int ) {
148159 packageInfo := buildPackage (moduleName , packages , fset )
149160 output := & Output {
150161 PackageInfo : packageInfo ,
151162 ModuleName : moduleName ,
152163 GoModPath : goModPath ,
164+ NumOfGoMod : numOfGoMod ,
153165 }
154166 //jsonBytes, err := json.MarshalIndent(output, "", " ")
155167 //if err != nil {
0 commit comments