|
15 | 15 | package cmd |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "bufio" |
19 | 18 | "context" |
20 | 19 | "fmt" |
21 | | - "io" |
22 | 20 | "os" |
23 | 21 |
|
24 | 22 | "github.com/spf13/cobra" |
25 | 23 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
26 | 24 |
|
27 | 25 | "github.com/crunchydata/postgres-operator-client/internal" |
28 | 26 | "github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1" |
| 27 | + "github.com/crunchydata/postgres-operator-client/internal/util" |
29 | 28 | ) |
30 | 29 |
|
31 | 30 | // newDeleteCommand returns the delete subcommand of the PGO plugin. |
@@ -80,7 +79,7 @@ postgresclusters/hippo deleted`) |
80 | 79 | for i := 0; confirmed == nil && i < 10; i++ { |
81 | 80 | // retry 10 times or until a confirmation is given or denied, |
82 | 81 | // whichever comes first |
83 | | - confirmed = confirm(os.Stdin, os.Stdout) |
| 82 | + confirmed = util.Confirm(os.Stdin, os.Stdout) |
84 | 83 | } |
85 | 84 |
|
86 | 85 | if confirmed == nil || !*confirmed { |
@@ -112,38 +111,6 @@ postgresclusters/hippo deleted`) |
112 | 111 | return cmd |
113 | 112 | } |
114 | 113 |
|
115 | | -// confirm uses a Scanner to parse user input. A user must type in "yes" or "no" |
116 | | -// and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", |
117 | | -// and "Yes" all count as confirmations and return 'true'. Similarly, "n", "N", |
118 | | -// "no", "No", "NO" all deny confirmation and return 'false'. If the input is not |
119 | | -// recognized, nil is returned. |
120 | | -func confirm(reader io.Reader, writer io.Writer) *bool { |
121 | | - var response string |
122 | | - var boolVar bool |
123 | | - |
124 | | - scanner := bufio.NewScanner(reader) |
125 | | - if scanner.Scan() { |
126 | | - response = scanner.Text() |
127 | | - } |
128 | | - |
129 | | - if scanner.Err() != nil || response == "" { |
130 | | - fmt.Fprint(writer, "Please type yes or no and then press enter: ") |
131 | | - return nil |
132 | | - } |
133 | | - |
134 | | - yesResponses := []string{"y", "Y", "yes", "Yes", "YES"} |
135 | | - noResponses := []string{"n", "N", "no", "No", "NO"} |
136 | | - if containsString(yesResponses, response) { |
137 | | - boolVar = true |
138 | | - return &boolVar |
139 | | - } else if containsString(noResponses, response) { |
140 | | - return &boolVar |
141 | | - } else { |
142 | | - fmt.Fprint(writer, "Please type yes or no and then press enter: ") |
143 | | - return nil |
144 | | - } |
145 | | -} |
146 | | - |
147 | 114 | // containsString returns true if slice contains element |
148 | 115 | func containsString(slice []string, element string) bool { |
149 | 116 | for _, elem := range slice { |
|
0 commit comments