feat: ignore containing modules

main
sudo pacman -Syu 2022-08-24 23:04:42 +07:00
parent cc44fbc78c
commit c87f232907
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
2 changed files with 47 additions and 1 deletions

1
.deps
View File

@ -1,2 +1,3 @@
github.com/haunt98/update-go-mod
github.com/make-go-great/color-go github.com/make-go-great/color-go
github.com/urfave/cli/v2 github.com/urfave/cli/v2

View File

@ -42,6 +42,11 @@ type Module struct {
func (a *action) Run(c *cli.Context) error { func (a *action) Run(c *cli.Context) error {
a.getFlags(c) a.getFlags(c)
containingModules, err := a.runGetContainingModules(c)
if err != nil {
return err
}
mapImportedModules, err := a.runGetImportedModules(c) mapImportedModules, err := a.runGetImportedModules(c)
if err != nil { if err != nil {
return err return err
@ -68,6 +73,7 @@ func (a *action) Run(c *cli.Context) error {
for _, modulePath := range modulePaths { for _, modulePath := range modulePaths {
successUpgradedModules, err = a.runUpgradeModule( successUpgradedModules, err = a.runUpgradeModule(
c, c,
containingModules,
mapImportedModules, mapImportedModules,
successUpgradedModules, successUpgradedModules,
modulePath, modulePath,
@ -88,6 +94,38 @@ func (a *action) Run(c *cli.Context) error {
return nil 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) { func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error) {
// Get all imported modules // Get all imported modules
goListAllArgs := []string{"list", "-m", "-json", "all"} goListAllArgs := []string{"list", "-m", "-json", "all"}
@ -164,6 +202,7 @@ func (a *action) runReadDepsFile(c *cli.Context) (string, error) {
func (a *action) runUpgradeModule( func (a *action) runUpgradeModule(
c *cli.Context, c *cli.Context,
containingModules map[string]struct{},
mapImportedModules map[string]Module, mapImportedModules map[string]Module,
successUpgradedModules []Module, successUpgradedModules []Module,
modulePath string, modulePath string,
@ -182,6 +221,12 @@ func (a *action) runUpgradeModule(
a.log("Module path: %s\n", modulePath) 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 // Ignore not imported module
if _, ok := mapImportedModules[modulePath]; !ok { if _, ok := mapImportedModules[modulePath]; !ok {
a.log("%s is not imported module\n", modulePath) a.log("%s is not imported module\n", modulePath)