Skip to content

Commit

Permalink
refactor(remove): split functions
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Sep 28, 2024
1 parent 40f76a6 commit 8605406
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions pkg/controller/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +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 {
if errors.Is(err, which.ErrCommandIsNotFound) {
logE.Debug("the command isn't found")
continue
}
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 logerr.WithFields(err, logrus.Fields{

Check failure on line 164 in pkg/controller/remove/remove.go

View workflow job for this annotation

GitHub Actions / test / test

error returned from external package is unwrapped: sig: func github.com/suzuki-shunsuke/logrus-error/logerr.WithFields(err error, fields github.com/sirupsen/logrus.Fields) error (wrapcheck)
"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

0 comments on commit 8605406

Please sign in to comment.