From 2121dde35d5708534a836d1282d1ce9053fada12 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez <6853656+cristiand391@users.noreply.github.com> Date: Tue, 9 Mar 2021 03:37:45 +0000 Subject: [PATCH] Remove binaries by name (#48) * Remove binaries by name * Reuse logic for finding binary path * make getBinPath check if arg is a path Co-authored-by: Cristian Dominguez --- cmd/remove.go | 7 ++++++- cmd/root.go | 18 ++++++++++++++++++ cmd/update.go | 25 ++++++++----------------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cmd/remove.go b/cmd/remove.go index 9cc4c91..00b8823 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -21,7 +21,7 @@ func newRemoveCmd() *removeCmd { var root = &removeCmd{} // nolint: dupl var cmd = &cobra.Command{ - Use: "remove ", + Use: "remove [ | ]", Aliases: []string{"rm"}, Short: "Removes binaries managed by bin", SilenceUsage: true, @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 8dbe6ab..115f117 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,9 @@ package cmd import ( + "fmt" "os" + "strings" "github.com/apex/log" "github.com/apex/log/handlers/cli" @@ -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) +} diff --git a/cmd/update.go b/cmd/update.go index 765f669..d27c490 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -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 {