feat: ignore main, indirect module using Module struct

main
sudo pacman -Syu 2022-09-22 00:39:57 +07:00
parent 7a67c96d08
commit a4e195f7c1
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
1 changed files with 17 additions and 20 deletions

View File

@ -33,10 +33,12 @@ var (
// See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules // See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules
type Module struct { type Module struct {
Update *Module Update *Module
Replace *Module Replace *Module
Path string Path string
Version string Version string
Main bool
Indirect bool
} }
func (a *action) Run(c *cli.Context) error { func (a *action) Run(c *cli.Context) error {
@ -90,20 +92,8 @@ func (a *action) Run(c *cli.Context) error {
// Get all imported modules // Get all imported modules
func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error) { func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error) {
// https://go.dev/ref/mod#go-list-m
goListMainModuleArgs := []string{"list", "-m", "-json"}
goOutput, err := exec.CommandContext(c.Context, "go", goListMainModuleArgs...).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to run go %s: %w", strings.Join(goListMainModuleArgs, " "), err)
}
mainModule := Module{}
if err := json.Unmarshal(goOutput, &mainModule); err != nil {
return nil, fmt.Errorf("json: failed to unmarshal go output: %w", err)
}
goListAllArgs := []string{"list", "-m", "-json", "all"} goListAllArgs := []string{"list", "-m", "-json", "all"}
goOutput, err = exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput() goOutput, err := exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to run go %s: %w", strings.Join(goListAllArgs, " "), err) return nil, fmt.Errorf("failed to run go %s: %w", strings.Join(goListAllArgs, " "), err)
} }
@ -122,6 +112,16 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error
mapImportedModules := make(map[string]Module) mapImportedModules := make(map[string]Module)
for _, importedModule := range importedModules { for _, importedModule := range importedModules {
// Ignore main module
if importedModule.Main {
continue
}
// Ignore indirect module
if importedModule.Indirect {
continue
}
// Ignore replace module // Ignore replace module
if importedModule.Replace != nil { if importedModule.Replace != nil {
continue continue
@ -130,9 +130,6 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error
mapImportedModules[importedModule.Path] = importedModule mapImportedModules[importedModule.Path] = importedModule
} }
// imported modules also includes main module so we need to remove
delete(mapImportedModules, mainModule.Path)
a.log("Imported modules: %+v\n", importedModules) a.log("Imported modules: %+v\n", importedModules)
return mapImportedModules, nil return mapImportedModules, nil