Skip to content

Commit

Permalink
Make 'ConfirmSend' return a bool instead of an error and rename it
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVulaj committed Jun 23, 2023
1 parent 02cf782 commit 343d233
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 41 deletions.
10 changes: 4 additions & 6 deletions cmd/cluster/resizecontrolplanenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,15 @@ func (o *resizeControlPlaneNodeOptions) run() error {
fmt.Println() // Add an empty line for better output formatting

fmt.Println("To continue, please confirm that the node is up and running and that the cluster is in the desired state to proceed.")
err = utils.ConfirmSend()
if err != nil {
return err
if !utils.ConfirmPrompt() {
return nil
}
fmt.Println() // Add an empty line for better output formatting

fmt.Println("To finish the node resize, it is suggested to update the machine spec. This requires ***elevated privileges***. Do you want to proceed?")
err = utils.ConfirmSend()
if err != nil {
if !utils.ConfirmPrompt() {
fmt.Println("Node resized, machine type not patched. Exiting...")
return err
return nil
}
fmt.Println() // Add an empty line for better output formatting

Expand Down
5 changes: 2 additions & 3 deletions cmd/cluster/support/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ func (o *deleteOptions) run() error {
}

// confirmSend prompt to confirm
err = utils.ConfirmSend()
if err != nil {
return err
if !utils.ConfirmPrompt() {
return nil
}

//getting the cluster
Expand Down
7 changes: 3 additions & 4 deletions cmd/cluster/support/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ func (o *postOptions) run() error {
return nil
}

// ConfirmSend prompt to confirm
err = ctlutil.ConfirmSend()
if err != nil {
return err
// ConfirmPrompt prompt to confirm
if !ctlutil.ConfirmPrompt() {
return nil
}

//getting the cluster
Expand Down
10 changes: 4 additions & 6 deletions cmd/cluster/transferowner.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,15 @@ func (o *transferOwnerOptions) run() error {

fmt.Printf("Transfer cluster: \t\t'%v' (%v)\n", externalClusterID, cluster.Name())
fmt.Printf("from user \t\t\t'%v' to '%v'\n", oldOwnerAccount.ID(), accountID)
err = utils.ConfirmSend()
if err != nil {
return err
if !utils.ConfirmPrompt() {
return nil
}

ok = validateOldOwner(oldOrganizationId, subscription, oldOwnerAccount)
if !ok {
fmt.Print("can't validate this is old owners cluster, this could be because of a previously failed run\n")
err = utils.ConfirmSend()
if err != nil {
return err
if !utils.ConfirmPrompt() {
return nil
}
}

Expand Down
13 changes: 2 additions & 11 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,9 @@ func versionCheck() {
if utils.Version != strings.TrimPrefix(latestVersion, "v") {
fmt.Printf("The current version (%s) is different than the latest released version (%s).", utils.Version, latestVersion)
fmt.Println("It is recommended that you update to the latest released version to ensure that no known bugs or issues are hit.")
fmt.Println("Please confirm that you would like to continue with [y|n]")

var input string
for {
fmt.Scanln(&input)
if strings.ToLower(input) == "y" {
break
}
if strings.ToLower(input) == "n" {
fmt.Println("Exiting")
os.Exit(0)
}
if !utils.ConfirmPrompt() {
os.Exit(0)
}
}
}
5 changes: 2 additions & 3 deletions cmd/servicelog/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ func (o *PostCmdOptions) Run() error {
}

if !o.skipPrompts {
err = ctlutil.ConfirmSend()
if err != nil {
log.Fatal(err)
if !ctlutil.ConfirmPrompt() {
return nil
}
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,37 +224,38 @@ func GetAccount(connection *sdk.Connection, key string) (account *amv1.Account,
return
}

func ConfirmSend() error {
func ConfirmPrompt() bool {
fmt.Print("Continue? (y/N): ")

var response string
_, err := fmt.Scanln(&response)
if err != nil {
return err
fmt.Println("Invalid input. Expecting (yes) or (N)o")
return ConfirmPrompt()
}

switch strings.ToLower(response) {
case "y", "yes":
return nil
return true
case "n", "no":
return fmt.Errorf("Exiting...")
return false
default:
fmt.Println("Invalid input. Expecting (y)es or (N)o")
return ConfirmSend()
return ConfirmPrompt()
}
}

// streamPrintln appends a newline then prints the given msg using the provided IOStreams
// StreamPrintln appends a newline then prints the given msg using the provided IOStreams
func StreamPrintln(stream genericclioptions.IOStreams, msg string) {
stream.Out.Write([]byte(fmt.Sprintln(msg)))
}

// streamPrint prints the given msg using the provided IOStreams
// StreamPrint prints the given msg using the provided IOStreams
func StreamPrint(stream genericclioptions.IOStreams, msg string) {
stream.Out.Write([]byte(msg))
}

// streamPrint prints the given error msg using the provided IOStreams
// StreamErrorln prints the given error msg using the provided IOStreams
func StreamErrorln(stream genericclioptions.IOStreams, msg string) {
stream.ErrOut.Write([]byte(fmt.Sprintln(msg)))
}
Expand Down

0 comments on commit 343d233

Please sign in to comment.