From 7222640624e6968e94bcd8a25a4ffbae432b740e Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Sun, 10 Mar 2024 00:25:36 -0300 Subject: [PATCH] fixes removing by lookup fixes #190 Signed-off-by: Marcos Lilljedahl --- cmd/ensure.go | 2 +- cmd/install.go | 20 ++++++++------------ cmd/root.go | 11 ++++++++--- cmd/update.go | 2 +- pkg/assets/assets.go | 4 ++++ pkg/config/config.go | 1 - pkg/config/config_unix.go | 3 +-- pkg/providers/github.go | 4 ++-- pkg/providers/gitlab.go | 2 +- pkg/providers/hashicorp.go | 2 +- pkg/providers/providers.go | 2 +- 11 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cmd/ensure.go b/cmd/ensure.go index 018a6b1..3be8b66 100644 --- a/cmd/ensure.go +++ b/cmd/ensure.go @@ -75,7 +75,7 @@ func newEnsureCmd() *ensureCmd { if err != nil { return err } - log.Infof("Done ensuring %s to %s", binCfg.Path, color.GreenString(binCfg.Version)) + log.Infof("Done ensuring %s to %s", os.ExpandEnv(binCfg.Path), color.GreenString(binCfg.Version)) } return nil }, diff --git a/cmd/install.go b/cmd/install.go index 8e75f1f..dacbcf9 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/apex/log" + "github.com/marcosnils/bin/pkg/assets" "github.com/marcosnils/bin/pkg/config" "github.com/marcosnils/bin/pkg/providers" "github.com/spf13/cobra" @@ -36,19 +37,14 @@ func newInstallCmd() *installCmd { RunE: func(cmd *cobra.Command, args []string) error { u := args[0] - var path string + var resolvedPath string if len(args) > 1 { - var err error - // Resolve to absolute path - if path, err = filepath.Abs(os.ExpandEnv(args[1])); err != nil { - return err - } - + resolvedPath = args[1] } else if len(config.Get().DefaultPath) > 0 { - path = config.Get().DefaultPath + resolvedPath = config.Get().DefaultPath } else { var err error - path, err = os.Getwd() + resolvedPath, err = os.Getwd() if err != nil { return err } @@ -67,18 +63,18 @@ func newInstallCmd() *installCmd { return err } - path, err = checkFinalPath(path, pResult.Name) + resolvedPath, err = checkFinalPath(resolvedPath, assets.SanitizeName(pResult.Name, pResult.Version)) if err != nil { return err } - if err = saveToDisk(pResult, path, root.opts.force); err != nil { + if err = saveToDisk(pResult, resolvedPath, root.opts.force); err != nil { return fmt.Errorf("error installing binary: %w", err) } err = config.UpsertBinary(&config.Binary{ RemoteName: pResult.Name, - Path: path, + Path: resolvedPath, Version: pResult.Version, Hash: fmt.Sprintf("%x", pResult.Hash.Sum(nil)), URL: u, diff --git a/cmd/root.go b/cmd/root.go index d01c073..cfdf921 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "os/exec" "path/filepath" "github.com/apex/log" @@ -126,15 +127,19 @@ func defaultCommand(cmd *cobra.Command, args []string) bool { } func getBinPath(name string) (string, error) { - f, err := filepath.Abs(os.ExpandEnv(name)) + var f string + f, err := exec.LookPath(name) if err != nil { - return "", err + f, err = filepath.Abs(os.ExpandEnv(name)) + if err != nil { + return "", err + } } cfg := config.Get() for _, bin := range cfg.Bins { - if bin.Path == f { + if os.ExpandEnv(bin.Path) == f { return bin.Path, nil } } diff --git a/cmd/update.go b/cmd/update.go index c233041..58e2e5c 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -114,7 +114,7 @@ func newUpdateCmd() *updateCmd { return err } - pResult, err := p.Fetch(&providers.FetchOpts{All: root.opts.all, PackagePath: b.PackagePath, SkipPatchCheck: root.opts.skipPathCheck}) + pResult, err := p.Fetch(&providers.FetchOpts{All: root.opts.all, PackagePath: b.PackagePath, SkipPatchCheck: root.opts.skipPathCheck, PackageName: b.RemoteName}) if err != nil { if root.opts.continueOnError { updateFailures[b] = fmt.Errorf("Error while fetching %v: %w", ui.url, err) diff --git a/pkg/assets/assets.go b/pkg/assets/assets.go index 82c08a4..2066092 100644 --- a/pkg/assets/assets.go +++ b/pkg/assets/assets.go @@ -77,6 +77,10 @@ type FilterOpts struct { SkipScoring bool SkipPathCheck bool + // In case of updates, we're sending the previous version package path + // so in case it's the same one, we can re-use it. + PackageName string + // If target file is in a package format (tar, zip,etc) use this // variable to filter the resulting outputs. This is very useful // so we don't prompt the user to pick the file again on updates diff --git a/pkg/config/config.go b/pkg/config/config.go index 7c54b31..69f99aa 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -142,7 +142,6 @@ func write() error { decoder := json.NewEncoder(f) decoder.SetIndent("", " ") err = decoder.Encode(cfg) - if err != nil { return err } diff --git a/pkg/config/config_unix.go b/pkg/config/config_unix.go index 2aac979..65159f8 100644 --- a/pkg/config/config_unix.go +++ b/pkg/config/config_unix.go @@ -27,7 +27,6 @@ func getDefaultPath() (string, error) { log.Debugf("Checking path %s", p) err := checkDirExistsAndWritable(p) - if err != nil { log.Debugf("Error [%s] checking path", err) continue @@ -62,7 +61,7 @@ func checkDirExistsAndWritable(dir string) error { } else if !fi.IsDir() { return errors.New("Download path is not a directory") } - //TODO make this work in non unix platforms + // TODO make this work in non unix platforms err := unix.Access(dir, unix.W_OK) return err } diff --git a/pkg/providers/github.go b/pkg/providers/github.go index 87cda5f..b039916 100644 --- a/pkg/providers/github.go +++ b/pkg/providers/github.go @@ -49,7 +49,7 @@ func (g *gitHub) Fetch(opts *FetchOpts) (*File, error) { for _, a := range release.Assets { candidates = append(candidates, &assets.Asset{Name: a.GetName(), URL: a.GetURL()}) } - f := assets.NewFilter(&assets.FilterOpts{SkipScoring: opts.All, PackagePath: opts.PackagePath, SkipPathCheck: opts.SkipPatchCheck}) + f := assets.NewFilter(&assets.FilterOpts{SkipScoring: opts.All, PackagePath: opts.PackagePath, SkipPathCheck: opts.SkipPatchCheck, PackageName: opts.PackageName}) gf, err := f.FilterAssets(g.repo, candidates) if err != nil { @@ -71,7 +71,7 @@ func (g *gitHub) Fetch(opts *FetchOpts) (*File, error) { // TODO calculate file hash. Not sure if we can / should do it here // since we don't want to read the file unnecesarily. Additionally, sometimes // releases have .sha256 files, so it'd be nice to check for those also - file := &File{Data: outFile.Source, Name: assets.SanitizeName(outFile.Name, version), Hash: sha256.New(), Version: version, PackagePath: outFile.PackagePath} + file := &File{Data: outFile.Source, Name: outFile.Name, Hash: sha256.New(), Version: version, PackagePath: outFile.PackagePath} return file, nil } diff --git a/pkg/providers/gitlab.go b/pkg/providers/gitlab.go index 1903ef2..2618408 100644 --- a/pkg/providers/gitlab.go +++ b/pkg/providers/gitlab.go @@ -175,7 +175,7 @@ func (g *gitLab) Fetch(opts *FetchOpts) (*File, error) { // TODO calculate file hash. Not sure if we can / should do it here // since we don't want to read the file unnecesarily. Additionally, sometimes // releases have .sha256 files, so it'd be nice to check for those also - file := &File{Data: outFile.Source, Name: assets.SanitizeName(outFile.Name, version), Hash: sha256.New(), Version: version} + file := &File{Data: outFile.Source, Name: outFile.Name, Hash: sha256.New(), Version: version} return file, nil } diff --git a/pkg/providers/hashicorp.go b/pkg/providers/hashicorp.go index 3301fe9..acea1be 100644 --- a/pkg/providers/hashicorp.go +++ b/pkg/providers/hashicorp.go @@ -113,7 +113,7 @@ func (g *hashiCorp) Fetch(opts *FetchOpts) (*File, error) { // TODO calculate file hash. Not sure if we can / should do it here // since we don't want to read the file unnecesarily. Additionally, sometimes // releases have .sha256 files, so it'd be nice to check for those also - file := &File{Data: outFile.Source, Name: assets.SanitizeName(outFile.Name, version), Hash: sha256.New(), Version: version} + file := &File{Data: outFile.Source, Name: outFile.Name, Hash: sha256.New(), Version: version} return file, nil } diff --git a/pkg/providers/providers.go b/pkg/providers/providers.go index 32d3996..5cf4e56 100644 --- a/pkg/providers/providers.go +++ b/pkg/providers/providers.go @@ -23,6 +23,7 @@ type File struct { type FetchOpts struct { All bool + PackageName string PackagePath string SkipPatchCheck bool } @@ -53,7 +54,6 @@ func New(u, provider string) (Provider, error) { } purl, err := url.Parse(u) - if err != nil { return nil, err }