Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added AskForPasswordValue and 2 tests #1708

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/minikube/cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"bytes"
"fmt"
"testing"
)

Expand Down Expand Up @@ -57,6 +58,32 @@ var configTestCases = []configTestCase{
},
}

func TestHiddenPrint(t *testing.T) {
testString := "gabbagabbahey"
b := new(bytes.Buffer)
_, err := b.WriteString(fmt.Sprintf("%s\r\n", testString)) // you need the \r!
if err != nil {
t.Errorf("Could not prepare bytestring")
}
result, err := concealableAskForStaticValue(b, "hello", true)
if result != testString {
t.Errorf("Result %s not match %s", result, testString)
}
}

func TestVerbosePrint(t *testing.T) {
testString := "gabbagabbahey"
b := new(bytes.Buffer)
_, err := b.WriteString(fmt.Sprintf("%s\r\n", testString)) // you need the \r!
if err != nil {
t.Errorf("Could not prepare bytestring")
}
result, err := concealableAskForStaticValue(b, "hello", false)
if result != testString {
t.Errorf("Result %s not match %s", result, testString)
}
}

func TestWriteConfig(t *testing.T) {
var b bytes.Buffer
for _, tt := range configTestCases {
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var addonsConfigureCmd = &cobra.Command{
if enableDR {
dockerServer = AskForStaticValue("-- Enter docker registry server url: ")
dockerUser = AskForStaticValue("-- Enter docker registry username: ")
dockerPass = AskForStaticValue("-- Enter docker registry password: ")
dockerPass = AskForPasswordValue("-- Enter docker registry password: ")
}

// Create ECR Secret
Expand Down
46 changes: 46 additions & 0 deletions cmd/minikube/cmd/config/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config
import (
"bufio"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -75,6 +77,50 @@ func AskForStaticValue(s string) string {
}
}

func concealableAskForStaticValue(readWriter io.ReadWriter, promptString string, hidden bool) (string, error) {
for {
var (
response string
err error
term *terminal.Terminal
)

if hidden {
term = terminal.NewTerminal(readWriter, "")
response, err = term.ReadPassword(promptString)
} else {
term = terminal.NewTerminal(readWriter, promptString)
response, err = term.ReadLine()
}

if err != nil {
return "", err
}
response = strings.TrimSpace(response)
if len(response) == 0 {
fmt.Println("--Error, please enter a value:")
return concealableAskForStaticValue(readWriter, promptString, hidden)
}
return response, nil
}
}

func AskForPasswordValue(s string) string {

stdInFd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(stdInFd)
if err != nil {
log.Fatal(err)
}
defer terminal.Restore(stdInFd, oldState)

result, err := concealableAskForStaticValue(os.Stdin, s, true)
if err != nil {
log.Fatal(err)
}
return result
}

// posString returns the first index of element in slice.
// If slice does not contain element, returns -1.
func posString(slice []string, element string) int {
Expand Down