Skip to content

Commit

Permalink
fix(remove): ignore not found commands (#3138)
Browse files Browse the repository at this point in the history
* ci: test removing not found packages

* fix(remove): ignore not found commands

* refactor(remove): split functions

* refactor: fix a lint error
  • Loading branch information
suzuki-shunsuke authored Sep 28, 2024
1 parent e5bd936 commit 08043c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/wc-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ jobs:
# http - terraform
# go_install - terrafmt
run: aqua rm x-motemen/ghq bats-core/bats-core terraform terrafmt
- name: Remove not found package
run: aqua rm Builditluc/wiki-tui
- name: Remove not found command
run: aqua rm wiki-tui

- name: Test minisign
run: aqua i
Expand Down
38 changes: 26 additions & 12 deletions pkg/controller/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/controller/which"
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
Expand Down Expand Up @@ -159,24 +160,37 @@ func (c *Controller) removeCommands(ctx context.Context, logE *logrus.Entry, par
}
for _, cmd := range cmds {
logE := logE.WithField("exe_name", cmd)
findResult, err := c.which.Which(ctx, logE, param, cmd)
if err != nil {
return fmt.Errorf("find a command: %w", err)
}
if findResult.Package == nil {
logE.Debug("no package is found")
continue
}
logE = logE.WithField("package_name", findResult.Package.Package.Name)
if err := c.removePackage(logE, param.RootDir, findResult.Package.PackageInfo); err != nil {
return fmt.Errorf("remove a package: %w", logerr.WithFields(err, logrus.Fields{
"package_name": findResult.Package.Package.Name,
if err := c.removeCommand(ctx, logE, param, cmd); err != nil {
return fmt.Errorf("remove a command: %w", logerr.WithFields(err, logrus.Fields{
"exe_name": cmd,
}))
}
}
return gErr
}

func (c *Controller) removeCommand(ctx context.Context, logE *logrus.Entry, param *config.Param, cmd string) error {
findResult, err := c.which.Which(ctx, logE, param, cmd)
if err != nil {
if errors.Is(err, which.ErrCommandIsNotFound) {
logE.Debug("the command isn't found")
return nil
}
return fmt.Errorf("find a command: %w", err)
}
if findResult.Package == nil {
logE.Debug("no package is found")
return nil
}
logE = logE.WithField("package_name", findResult.Package.Package.Name)
if err := c.removePackage(logE, param.RootDir, findResult.Package.PackageInfo); err != nil {
return fmt.Errorf("remove a package: %w", logerr.WithFields(err, logrus.Fields{
"package_name": findResult.Package.Package.Name,
}))
}
return nil
}

func (c *Controller) removePackage(logE *logrus.Entry, rootDir string, pkg *registry.PackageInfo) error {
var gErr error
logE.Info("removing a package")
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/which/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package which
import "errors"

var (
errCommandIsNotFound = errors.New("command is not found")
ErrCommandIsNotFound = errors.New("command is not found")
errVersionIsRequired = errors.New("version is required")
)
2 changes: 1 addition & 1 deletion pkg/controller/which/which.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Controller) Which(ctx context.Context, logE *logrus.Entry, param *confi
ExePath: exePath,
}, nil
}
return nil, logerr.WithFields(errCommandIsNotFound, logrus.Fields{ //nolint:wrapcheck
return nil, logerr.WithFields(ErrCommandIsNotFound, logrus.Fields{ //nolint:wrapcheck
"exe_name": exeName,
"doc": "https://aquaproj.github.io/docs/reference/codes/004",
})
Expand Down

0 comments on commit 08043c1

Please sign in to comment.