-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from suzuki-shunsuke/feat/add-which
feat: add command "which"
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/suzuki-shunsuke/aqua/pkg/controller" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func (runner *Runner) whichAction(c *cli.Context) error { | ||
param := &controller.Param{} | ||
if err := runner.setCLIArg(c, param); err != nil { | ||
return fmt.Errorf("parse the command line arguments: %w", err) | ||
} | ||
|
||
ctrl, err := controller.New(c.Context, param) | ||
if err != nil { | ||
return fmt.Errorf("initialize a controller: %w", err) | ||
} | ||
|
||
return ctrl.Which(c.Context, param, c.Args().Slice()) //nolint:wrapcheck | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/suzuki-shunsuke/logrus-error/logerr" | ||
) | ||
|
||
func (ctrl *Controller) Which(ctx context.Context, param *Param, args []string) error { //nolint:cyclop,funlen | ||
if len(args) == 0 { | ||
return errCommandIsRequired | ||
} | ||
|
||
exeName := filepath.Base(args[0]) | ||
fields := logrus.Fields{ | ||
"exe_name": exeName, | ||
} | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("get the current directory: %w", logerr.WithFields(err, fields)) | ||
} | ||
|
||
if cfgFilePath := ctrl.getConfigFilePath(wd, param.ConfigFilePath); cfgFilePath != "" { | ||
pkg, pkgInfo, file, err := ctrl.findExecFile(ctx, cfgFilePath, exeName) | ||
if err != nil { | ||
return err | ||
} | ||
if pkg != nil { | ||
return ctrl.which(pkg, pkgInfo, file) | ||
} | ||
} | ||
|
||
for _, cfgFilePath := range getGlobalConfigFilePaths() { | ||
if _, err := os.Stat(cfgFilePath); err != nil { | ||
continue | ||
} | ||
pkg, pkgInfo, file, err := ctrl.findExecFile(ctx, cfgFilePath, exeName) | ||
if err != nil { | ||
return err | ||
} | ||
if pkg != nil { | ||
return ctrl.which(pkg, pkgInfo, file) | ||
} | ||
} | ||
|
||
cfgFilePath := ctrl.ConfigFinder.FindGlobal(ctrl.RootDir) | ||
if _, err := os.Stat(cfgFilePath); err != nil { | ||
exePath := lookPath(exeName) | ||
if exePath == "" { | ||
return logerr.WithFields(errCommandIsNotFound, logrus.Fields{ //nolint:wrapcheck | ||
"exe_name": exeName, | ||
}) | ||
} | ||
fmt.Fprintln(ctrl.Stdout, exePath) | ||
return nil | ||
} | ||
|
||
pkg, pkgInfo, file, err := ctrl.findExecFile(ctx, cfgFilePath, exeName) | ||
if err != nil { | ||
return err | ||
} | ||
if pkg == nil { | ||
exePath := lookPath(exeName) | ||
if exePath == "" { | ||
return logerr.WithFields(errCommandIsNotFound, logrus.Fields{ //nolint:wrapcheck | ||
"exe_name": exeName, | ||
}) | ||
} | ||
fmt.Fprintln(ctrl.Stdout, exePath) | ||
return nil | ||
} | ||
|
||
return ctrl.which(pkg, pkgInfo, file) | ||
} | ||
|
||
func (ctrl *Controller) which(pkg *Package, pkgInfo *MergedPackageInfo, file *File) error { | ||
fileSrc, err := pkgInfo.GetFileSrc(pkg, file) | ||
if err != nil { | ||
return fmt.Errorf("get file_src: %w", err) | ||
} | ||
pkgPath, err := pkgInfo.GetPkgPath(ctrl.RootDir, pkg) | ||
if err != nil { | ||
return fmt.Errorf("get pkg install path: %w", err) | ||
} | ||
fmt.Fprintln(ctrl.Stdout, filepath.Join(pkgPath, fileSrc)) | ||
return nil | ||
} |