From 860540628c2056c2c7e257058493283388bf6ff8 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 29 Sep 2024 08:28:02 +0900 Subject: [PATCH] refactor(remove): split functions --- pkg/controller/remove/remove.go | 43 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/controller/remove/remove.go b/pkg/controller/remove/remove.go index c8c537d0c..7db5bc53d 100644 --- a/pkg/controller/remove/remove.go +++ b/pkg/controller/remove/remove.go @@ -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{ + "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")