Skip to content

Commit edf5608

Browse files
committed
Enhancement on Yesno()
1 parent c360931 commit edf5608

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

cmd/kubebuilder/initproject/project.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package initproject
1818

1919
import (
20+
"bufio"
2021
"fmt"
2122
"log"
2223
"os"
@@ -71,8 +72,9 @@ func (o *projectOptions) RunInit() {
7172
}
7273

7374
if !o.depFlag.Changed {
75+
reader := bufio.NewReader(os.Stdin)
7476
fmt.Println("Run `dep ensure` to fetch dependencies (Recommended) [y/n]?")
75-
o.dep = util.Yesno()
77+
o.dep = util.Yesno(reader)
7678
}
7779
if o.dep {
7880
c := exec.Command("dep", "ensure") // #nosec

cmd/kubebuilder/util/stdin.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ package util
1818

1919
import (
2020
"bufio"
21+
"fmt"
2122
"log"
22-
"os"
2323
"strings"
2424
)
2525

2626
// Yesno reads from stdin looking for one of "y", "yes", "n", "no" and returns
2727
// true for "y" and false for "n"
28-
func Yesno() bool {
29-
reader := bufio.NewReader(os.Stdin)
28+
func Yesno(reader *bufio.Reader) bool {
3029
for {
31-
switch readstdin(reader) {
30+
text := readstdin(reader)
31+
switch text {
3232
case "y", "yes":
3333
return true
3434
case "n", "no":
3535
return false
36+
default:
37+
fmt.Printf("invalid input %q, should be [y/n]", text)
3638
}
3739
}
3840
}
@@ -42,7 +44,7 @@ func Yesno() bool {
4244
func readstdin(reader *bufio.Reader) string {
4345
text, err := reader.ReadString('\n')
4446
if err != nil {
45-
log.Fatal(err)
47+
log.Fatalf("Error when reading input: %v", err)
4648
}
4749
return strings.TrimSpace(text)
4850
}

cmd/kubebuilder/v1/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"bufio"
2021
"fmt"
2122
"log"
2223
"os"
@@ -44,13 +45,14 @@ type apiOptions struct {
4445
func (o *apiOptions) RunAddAPI() {
4546
dieIfNoProject()
4647

48+
reader := bufio.NewReader(os.Stdin)
4749
if !o.resourceFlag.Changed {
4850
fmt.Println("Create Resource under pkg/apis [y/n]?")
49-
o.doResource = util.Yesno()
51+
o.doResource = util.Yesno(reader)
5052
}
5153
if !o.controllerFlag.Changed {
5254
fmt.Println("Create Controller under pkg/controller [y/n]?")
53-
o.doController = util.Yesno()
55+
o.doController = util.Yesno(reader)
5456
}
5557

5658
fmt.Println("Writing scaffold for you to edit...")

0 commit comments

Comments
 (0)