-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add confirm flag to velero plugin add
Signed-off-by: Tiger Kaovilai <[email protected]>
- Loading branch information
Showing
9 changed files
with
82 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add confirm flag to velero plugin add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package confirm | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type ConfirmOptions struct { | ||
Confirm bool | ||
flagDescription string | ||
} | ||
|
||
func NewConfirmOptions() *ConfirmOptions { | ||
return &ConfirmOptions{flagDescription: "Confirm action"} | ||
} | ||
|
||
func NewConfirmOptionsWithDescription(desc string) *ConfirmOptions { | ||
return &ConfirmOptions{flagDescription: desc} | ||
} | ||
|
||
// Bind confirm flags. | ||
func (o *ConfirmOptions) BindFlags(flags *pflag.FlagSet) { | ||
flags.BoolVar(&o.Confirm, "confirm", o.Confirm, o.flagDescription) | ||
} | ||
|
||
// GetConfirmation ensures that the user confirms the action before proceeding. | ||
func GetConfirmation(prompts ...string) bool { | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
for { | ||
for i := range prompts { | ||
fmt.Println(prompts[i]) | ||
} | ||
fmt.Printf("Are you sure you want to continue (Y/N)? ") | ||
|
||
confirmation, err := reader.ReadString('\n') | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "error reading user input: %v\n", err) | ||
return false | ||
} | ||
confirmation = strings.TrimSpace(confirmation) | ||
if len(confirmation) != 1 { | ||
continue | ||
} | ||
|
||
switch strings.ToLower(confirmation) { | ||
case "y": | ||
return true | ||
case "n": | ||
return false | ||
} | ||
} | ||
} |