Skip to content

Commit

Permalink
fixes removing by lookup (#191)
Browse files Browse the repository at this point in the history
fixes #190

Signed-off-by: Marcos Lilljedahl <[email protected]>
  • Loading branch information
marcosnils authored Mar 10, 2024
1 parent 9da887d commit c45e354
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
20 changes: 8 additions & 12 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/apex/log"
Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func write() error {
decoder := json.NewEncoder(f)
decoder.SetIndent("", " ")
err = decoder.Encode(cfg)

if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/hashicorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type File struct {

type FetchOpts struct {
All bool
PackageName string
PackagePath string
SkipPatchCheck bool
}
Expand Down Expand Up @@ -53,7 +54,6 @@ func New(u, provider string) (Provider, error) {
}

purl, err := url.Parse(u)

if err != nil {
return nil, err
}
Expand Down

0 comments on commit c45e354

Please sign in to comment.