From c87f23290719dc20f0717bef9921833ed37d9e62 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Wed, 24 Aug 2022 23:04:42 +0700 Subject: [PATCH] feat: ignore containing modules --- .deps | 3 ++- internal/cli/action_run.go | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/.deps b/.deps index 6478bbe..837cc6d 100644 --- a/.deps +++ b/.deps @@ -1,2 +1,3 @@ +github.com/haunt98/update-go-mod github.com/make-go-great/color-go -github.com/urfave/cli/v2 \ No newline at end of file +github.com/urfave/cli/v2 diff --git a/internal/cli/action_run.go b/internal/cli/action_run.go index 7edf1c4..bf02b6d 100644 --- a/internal/cli/action_run.go +++ b/internal/cli/action_run.go @@ -42,6 +42,11 @@ type Module struct { func (a *action) Run(c *cli.Context) error { a.getFlags(c) + containingModules, err := a.runGetContainingModules(c) + if err != nil { + return err + } + mapImportedModules, err := a.runGetImportedModules(c) if err != nil { return err @@ -68,6 +73,7 @@ func (a *action) Run(c *cli.Context) error { for _, modulePath := range modulePaths { successUpgradedModules, err = a.runUpgradeModule( c, + containingModules, mapImportedModules, successUpgradedModules, modulePath, @@ -88,6 +94,38 @@ func (a *action) Run(c *cli.Context) error { return nil } +// Get all package containing modules +// aka internal modules +func (a *action) runGetContainingModules(c *cli.Context) (map[string]struct{}, error) { + goListAllArgs := []string{"list", "-f", "'{{ .Module }}'", "./..."} + goOutput, err := exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput() + if err != nil { + return nil, fmt.Errorf("failed to run go %+v: %w", strings.Join(goListAllArgs, " "), err) + } + + containingModules := make(map[string]struct{}) + for _, line := range strings.Split(string(goOutput), "\n") { + line = strings.TrimSpace(line) + + // Don't know how ' exist in this + // After long time debug, I finally found that + line = strings.Trim(line, "'") + + if line == "" { + continue + } + + if _, ok := containingModules[line]; ok { + continue + } + + containingModules[line] = struct{}{} + } + + a.log("Containing modules: %+v\n", containingModules) + return containingModules, nil +} + func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error) { // Get all imported modules goListAllArgs := []string{"list", "-m", "-json", "all"} @@ -164,6 +202,7 @@ func (a *action) runReadDepsFile(c *cli.Context) (string, error) { func (a *action) runUpgradeModule( c *cli.Context, + containingModules map[string]struct{}, mapImportedModules map[string]Module, successUpgradedModules []Module, modulePath string, @@ -182,6 +221,12 @@ func (a *action) runUpgradeModule( a.log("Module path: %s\n", modulePath) + // Ignore if is containing module + if _, ok := containingModules[modulePath]; ok { + a.log("%s is containing module\n", modulePath) + return successUpgradedModules, nil + } + // Ignore not imported module if _, ok := mapImportedModules[modulePath]; !ok { a.log("%s is not imported module\n", modulePath)