Skip to content

Commit 9189881

Browse files
committed
Update/refactor utility functions
- Replace 'containsString' function with slices.Contains. - Move 'confirm' function to util package.
1 parent c4d5d68 commit 9189881

File tree

5 files changed

+124
-52
lines changed

5 files changed

+124
-52
lines changed

internal/cmd/delete.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,3 @@ postgresclusters/hippo deleted`)
110110

111111
return cmd
112112
}
113-
114-
// containsString returns true if slice contains element
115-
func containsString(slice []string, element string) bool {
116-
for _, elem := range slice {
117-
if elem == element {
118-
return true
119-
}
120-
}
121-
return false
122-
}

internal/cmd/delete_test.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

internal/cmd/show.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
corev1 "k8s.io/api/core/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
30+
"k8s.io/utils/strings/slices"
3031
"sigs.k8s.io/yaml"
3132

3233
"github.com/crunchydata/postgres-operator-client/internal"
@@ -353,7 +354,7 @@ func showUser(config *internal.Config, args []string, showSensitive bool) (strin
353354
for i := 0; confirmed == nil && i < 10; i++ {
354355
// retry 10 times or until a confirmation is given or denied,
355356
// whichever comes first
356-
confirmed = confirm(os.Stdin, os.Stdout)
357+
confirmed = util.Confirm(os.Stdin, os.Stdout)
357358
}
358359

359360
if confirmed == nil || !*confirmed {
@@ -420,7 +421,7 @@ func userData(fields []string, list *corev1.SecretList) (string, error) {
420421
if err != nil {
421422
return output, err
422423
}
423-
if containsString(fields, k) {
424+
if slices.Contains(fields, k) {
424425
output += fmt.Sprintf(" %s: %s\n", strings.ToUpper(k), string(d))
425426
}
426427
}

internal/util/util.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021 - 2023 Crunchy Data Solutions, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import (
18+
"bufio"
19+
"fmt"
20+
"io"
21+
22+
"k8s.io/utils/strings/slices"
23+
)
24+
25+
// Confirm uses a Scanner to parse user input. A user must type in "yes" or "no"
26+
// and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES",
27+
// and "Yes" all count as confirmations and return 'true'. Similarly, "n", "N",
28+
// "no", "No", "NO" all deny confirmation and return 'false'. If the input is not
29+
// recognized, nil is returned.
30+
func Confirm(reader io.Reader, writer io.Writer) *bool {
31+
var response string
32+
var boolVar bool
33+
34+
scanner := bufio.NewScanner(reader)
35+
if scanner.Scan() {
36+
response = scanner.Text()
37+
}
38+
39+
if scanner.Err() != nil || response == "" {
40+
fmt.Fprint(writer, "Please type yes or no and then press enter: ")
41+
return nil
42+
}
43+
44+
yesResponses := []string{"y", "Y", "yes", "Yes", "YES"}
45+
noResponses := []string{"n", "N", "no", "No", "NO"}
46+
if slices.Contains(yesResponses, response) {
47+
boolVar = true
48+
return &boolVar
49+
} else if slices.Contains(noResponses, response) {
50+
return &boolVar
51+
} else {
52+
fmt.Fprint(writer, "Please type yes or no and then press enter: ")
53+
return nil
54+
}
55+
}

internal/util/util_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2021 - 2023 Crunchy Data Solutions, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import (
18+
"bytes"
19+
"io"
20+
"strings"
21+
"testing"
22+
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestConfirmDelete(t *testing.T) {
27+
28+
testsCases := []struct {
29+
input string
30+
invalidResponse bool
31+
confirmed bool
32+
}{
33+
{"abc", true, false}, // invalid
34+
{"", true, false}, // invalid
35+
{"y", false, true},
36+
{"Y", false, true},
37+
{"yes", false, true},
38+
{"Yes", false, true},
39+
{"YES", false, true},
40+
{"n", false, false},
41+
{"N", false, false},
42+
{"no", false, false},
43+
{"No", false, false},
44+
{"NO", false, false},
45+
{"yep", true, false}, // invalid
46+
{"nope", true, false}, // invalid
47+
}
48+
49+
for _, tc := range testsCases {
50+
t.Run("input is "+tc.input, func(t *testing.T) {
51+
var reader io.Reader = strings.NewReader(tc.input)
52+
var writer bytes.Buffer
53+
confirmed := Confirm(reader, &writer)
54+
if tc.invalidResponse {
55+
assert.Assert(t, confirmed == nil)
56+
response, err := writer.ReadString(':')
57+
assert.NilError(t, err)
58+
assert.Equal(t, response, "Please type yes or no and then press enter:")
59+
60+
} else {
61+
assert.Assert(t, confirmed != nil)
62+
assert.Assert(t, *confirmed == tc.confirmed)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)