Skip to content

Commit

Permalink
add force flag for update (#101)
Browse files Browse the repository at this point in the history
* feat: add force flag for update

Relates to #95

* feat: rename flag

* feat: remove viper dependency

* feat: add shorthand for yes

* Update cmd/update.go

Co-authored-by: Sune Keller <[email protected]>

Co-authored-by: Sune Keller <[email protected]>
  • Loading branch information
sascha-andres and sirlatrom authored Jun 23, 2021
1 parent 83f4e74 commit a638c4a
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package cmd
import (
"bufio"
"fmt"
"os"
"strings"

"github.com/apex/log"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/marcosnils/bin/pkg/config"
"github.com/marcosnils/bin/pkg/providers"
"github.com/spf13/cobra"
"os"
"strings"
)

type updateCmd struct {
Expand All @@ -20,8 +19,9 @@ type updateCmd struct {
}

type updateOpts struct {
dryRun bool
all bool
yesToUpdate bool
dryRun bool
all bool
}

type updateInfo struct{ version, url string }
Expand Down Expand Up @@ -84,21 +84,23 @@ func newUpdateCmd() *updateCmd {
return wrapErrorWithCode(fmt.Errorf("Updates found, exit (dry-run mode)."), 3, "")
}

// TODO will have to refactor this prompt to a separate function
// so it can be reused in some other places
fmt.Printf("\nDo you want to continue? [Y/n] ")
reader := bufio.NewReader(os.Stdin)
var response string
if !root.opts.yesToUpdate {
// TODO will have to refactor this prompt to a separate function
// so it can be reused in some other places
fmt.Printf("\nDo you want to continue? [Y/n] ")
reader := bufio.NewReader(os.Stdin)
var response string

response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Invalid input")
}
response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Invalid input")
}

switch strings.ToLower(strings.TrimSpace(response)) {
case "y", "yes":
default:
return fmt.Errorf("Command aborted")
switch strings.ToLower(strings.TrimSpace(response)) {
case "y", "yes":
default:
return fmt.Errorf("Command aborted")
}
}

// TODO :S code smell here, this pretty much does
Expand Down Expand Up @@ -139,6 +141,7 @@ func newUpdateCmd() *updateCmd {

root.cmd = cmd
root.cmd.Flags().BoolVarP(&root.opts.dryRun, "dry-run", "", false, "Only show status, don't prompt for update")
root.cmd.Flags().BoolVarP(&root.opts.yesToUpdate, "yes", "y", false, "Assume yes to update prompt")
root.cmd.Flags().BoolVarP(&root.opts.all, "all", "a", false, "Show all possible download options (skip scoring & filtering)")
return root
}
Expand Down

0 comments on commit a638c4a

Please sign in to comment.