Skip to content

Commit

Permalink
Remove binaries by name (#48)
Browse files Browse the repository at this point in the history
* Remove binaries by name

* Reuse logic for finding binary path

* make getBinPath check if arg is a path

Co-authored-by: Cristian Dominguez <cristiand391@users.noreply.github.com>
  • Loading branch information
cristiand391 and cristiand391 authored Mar 9, 2021
1 parent 67ed6c3 commit 2121dde
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
7 changes: 6 additions & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newRemoveCmd() *removeCmd {
var root = &removeCmd{}
// nolint: dupl
var cmd = &cobra.Command{
Use: "remove <paths...>",
Use: "remove [<name> | <paths...>]",
Aliases: []string{"rm"},
Short: "Removes binaries managed by bin",
SilenceUsage: true,
Expand All @@ -34,6 +34,11 @@ func newRemoveCmd() *removeCmd {
existingToRemove := []string{}

for _, p := range args {
p, err := getBinPath(p)
if err != nil {
return err
}

if _, ok := cfg.Bins[p]; ok {
existingToRemove = append(existingToRemove, p)
//TODO some providers (like docker) might download
Expand Down
18 changes: 18 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
Expand Down Expand Up @@ -112,3 +114,19 @@ func defaultCommand(cmd *cobra.Command, args []string) bool {
// otherwise, we should probably prepend ls
return true
}

func getBinPath(name string) (string, error) {
if strings.Contains(name, "/") {
return name, nil
}

cfg := config.Get()

for _, bin := range cfg.Bins {
if bin.RemoteName == name {
return bin.Path, nil
}
}

return "", fmt.Errorf("Binary path %s not found", name)
}
25 changes: 8 additions & 17 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,17 @@ func newUpdateCmd() *updateCmd {

// Update single binary
if bin != "" {
// If user enter a command update like this: $ bin update yq
// Find path on the config file
if !strings.Contains(bin, "/") {
for _, b := range cfg.Bins {
if b.RemoteName == bin {
bin = b.Path
break
}
}
bin, err := getBinPath(bin)
if err != nil {
return err
}

if b, found := cfg.Bins[bin]; !found {
return fmt.Errorf("Binary path %s not found", bin)
b := cfg.Bins[bin]

} else {
if ui, err := getLatestVersion(b); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}
if ui, err := getLatestVersion(b); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}

} else {
Expand Down

0 comments on commit 2121dde

Please sign in to comment.