Skip to content

Commit

Permalink
Merge pull request #364 from suzuki-shunsuke/feat/add-which
Browse files Browse the repository at this point in the history
feat: add command "which"
  • Loading branch information
suzuki-shunsuke committed Oct 23, 2021
2 parents 2c12b94 + 2878fd1 commit 91ac6d2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/integration-test.yaml
Expand Up @@ -28,6 +28,8 @@ jobs:
- run: aqua i -l -a
- run: cmdx -v
- run: aqua i --test
- run: aqua which golangci-lint
- run: aqua which go
- run: golangci-lint version
- run: kind version
- run: restic version
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/runner.go
Expand Up @@ -78,6 +78,11 @@ func (runner *Runner) Run(ctx context.Context, args ...string) error { //nolint:
Usage: "List packages in Registries",
Action: runner.listAction,
},
{
Name: "which",
Usage: "Output the file path of the given command",
Action: runner.whichAction,
},
{
Name: "generate",
Aliases: []string{"g"},
Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/which.go
@@ -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
}
92 changes: 92 additions & 0 deletions pkg/controller/which.go
@@ -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
}

0 comments on commit 91ac6d2

Please sign in to comment.