mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-07 05:46:37 +00:00
Merge pull request #7566 from kaovilai/biaOperationErrorsPluginName
Add confirm flag to velero plugin add
This commit is contained in:
57
pkg/cmd/util/confirm/confirm.go
Normal file
57
pkg/cmd/util/confirm/confirm.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user