Skip to content

Commit

Permalink
Merge pull request #295 from Liujingfang1/master
Browse files Browse the repository at this point in the history
Enhancement on Yesno()
  • Loading branch information
k8s-ci-robot authored Jun 29, 2018
2 parents 7107816 + edf5608 commit a316f5e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/kubebuilder/initproject/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package initproject

import (
"bufio"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -71,8 +72,9 @@ func (o *projectOptions) RunInit() {
}

if !o.depFlag.Changed {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Run `dep ensure` to fetch dependencies (Recommended) [y/n]?")
o.dep = util.Yesno()
o.dep = util.Yesno(reader)
}
if o.dep {
c := exec.Command("dep", "ensure") // #nosec
Expand Down
12 changes: 7 additions & 5 deletions cmd/kubebuilder/util/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ package util

import (
"bufio"
"fmt"
"log"
"os"
"strings"
)

// Yesno reads from stdin looking for one of "y", "yes", "n", "no" and returns
// true for "y" and false for "n"
func Yesno() bool {
reader := bufio.NewReader(os.Stdin)
func Yesno(reader *bufio.Reader) bool {
for {
switch readstdin(reader) {
text := readstdin(reader)
switch text {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Printf("invalid input %q, should be [y/n]", text)
}
}
}
Expand All @@ -42,7 +44,7 @@ func Yesno() bool {
func readstdin(reader *bufio.Reader) string {
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
log.Fatalf("Error when reading input: %v", err)
}
return strings.TrimSpace(text)
}
6 changes: 4 additions & 2 deletions cmd/kubebuilder/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1

import (
"bufio"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -44,13 +45,14 @@ type apiOptions struct {
func (o *apiOptions) RunAddAPI() {
dieIfNoProject()

reader := bufio.NewReader(os.Stdin)
if !o.resourceFlag.Changed {
fmt.Println("Create Resource under pkg/apis [y/n]?")
o.doResource = util.Yesno()
o.doResource = util.Yesno(reader)
}
if !o.controllerFlag.Changed {
fmt.Println("Create Controller under pkg/controller [y/n]?")
o.doController = util.Yesno()
o.doController = util.Yesno(reader)
}

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

0 comments on commit a316f5e

Please sign in to comment.