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

@ -37,6 +37,8 @@ type Module struct {
Replace *Module
Path string
Version string
Main bool
Indirect bool
}
func (a *action) Run(c *cli.Context) error {
@ -90,20 +92,8 @@ func (a *action) Run(c *cli.Context) error {
// Get all imported modules
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"}
goOutput, err = exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput()
goOutput, err := exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput()
if err != nil {
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)
for _, importedModule := range importedModules {
// Ignore main module
if importedModule.Main {
continue
}
// Ignore indirect module
if importedModule.Indirect {
continue
}
// Ignore replace module
if importedModule.Replace != nil {
continue
@ -130,9 +130,6 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error
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)
return mapImportedModules, nil