Skip to content

Commit

Permalink
default values for some fields in install_config
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Chopra <rchopra@redhat.com>
  • Loading branch information
Rajat Chopra committed Sep 5, 2018
1 parent f2e6b3e commit ef833b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
11 changes: 6 additions & 5 deletions pkg/asset/installconfig/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const (
)

var (
validPlatforms = []string{AWSPlatformType, LibvirtPlatformType}
platformPrompt = fmt.Sprintf("Platform (%s):", strings.Join(validPlatforms, ", "))
validPlatforms = []string{AWSPlatformType, LibvirtPlatformType}
defaultPlatformValue = AWSPlatformType
platformPrompt = fmt.Sprintf("Platform (%s):", strings.Join(validPlatforms, ", "))
)

// Platform is an asset that queries the user for the platform on which to install
Expand Down Expand Up @@ -56,7 +57,7 @@ func (a *Platform) Generate(map[asset.Asset]*asset.State) (*asset.State, error)

func (a *Platform) queryUserForPlatform() string {
for {
input := asset.QueryUser(a.InputReader, platformPrompt)
input := asset.QueryUser(a.InputReader, platformPrompt, defaultPlatformValue)
input = strings.ToLower(input)
for _, p := range validPlatforms {
if input == p {
Expand All @@ -70,15 +71,15 @@ func (a *Platform) queryUserForPlatform() string {
func (a *Platform) awsPlatform() (*asset.State, error) {
return assetStateForStringContents(
AWSPlatformType,
asset.QueryUser(a.InputReader, "Region:"),
asset.QueryUser(a.InputReader, "Region:", "us-east-1"),
), nil
}

func (a *Platform) libvirtPlatform() (*asset.State, error) {
return assetStateForStringContents(
LibvirtPlatformType,
// TODO(yifan): Set the default URI.
asset.QueryUser(a.InputReader, "URI:"),
asset.QueryUser(a.InputReader, "URI:", "qemu:///system"),
), nil
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/asset/installconfig/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ func (s *StockImpl) EstablishStock(directory string, inputReader *bufio.Reader)
}
s.clusterID = &clusterID{}
s.emailAddress = &asset.UserProvided{
Prompt: "Email Address:",
InputReader: inputReader,
Prompt: "Email Address:",
DefaultValue: "a@b.c",
InputReader: inputReader,
}
s.password = &asset.UserProvided{
Prompt: "Password:",
InputReader: inputReader,
Prompt: "Password:",
DefaultValue: "verysecure",
InputReader: inputReader,
}
s.sshKey = &sshPublicKey{
inputReader: inputReader,
Expand Down
16 changes: 12 additions & 4 deletions pkg/asset/userprovided.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bufio"
"fmt"
"io"
"strings"
)

// UserProvided generates an asset that is supplied by a user.
type UserProvided struct {
InputReader *bufio.Reader
Prompt string
InputReader *bufio.Reader
DefaultValue string
Prompt string
}

var _ Asset = (*UserProvided)(nil)
Expand All @@ -21,7 +23,7 @@ func (a *UserProvided) Dependencies() []Asset {

// Generate queries for input from the user.
func (a *UserProvided) Generate(map[Asset]*State) (*State, error) {
input := QueryUser(a.InputReader, a.Prompt)
input := QueryUser(a.InputReader, a.Prompt, a.DefaultValue)
return &State{
Contents: []Content{
{Data: []byte(input)},
Expand All @@ -30,7 +32,10 @@ func (a *UserProvided) Generate(map[Asset]*State) (*State, error) {
}

// QueryUser queries the user for input.
func QueryUser(inputReader *bufio.Reader, prompt string) string {
func QueryUser(inputReader *bufio.Reader, prompt, defaultValue string) string {
if defaultValue != "" {
prompt = strings.Join([]string{prompt, "[", defaultValue, "]"}, "")
}
for {
fmt.Println(prompt)
input, err := inputReader.ReadString('\n')
Expand All @@ -41,6 +46,9 @@ func QueryUser(inputReader *bufio.Reader, prompt string) string {
if input != "" && input[len(input)-1] == '\n' {
input = input[:len(input)-1]
}
if input == "" {
input = defaultValue
}
return input
}
}
14 changes: 13 additions & 1 deletion pkg/asset/userprovided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,52 @@ func TestQueryUser(t *testing.T) {
cases := []struct {
name string
input string
defaultValue string
expectedOutput string
}{
{
name: "single word",
input: "test",
defaultValue: "default",
expectedOutput: "test",
},
{
name: "with newline",
input: `test
`,
defaultValue: "default",
expectedOutput: "test",
},
{
name: "multiple words",
input: "this is a test",
defaultValue: "default",
expectedOutput: "this is a test",
},
{
name: "multiple lines",
input: `first
second`,
defaultValue: "default",
expectedOutput: "first",
},
{
name: "empty",
input: "",
defaultValue: "",
expectedOutput: "",
},
{
name: "default value",
input: "",
defaultValue: "default",
expectedOutput: "default",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
inputReader := bufio.NewReader(strings.NewReader(tc.input))
actual := QueryUser(inputReader, "prompt")
actual := QueryUser(inputReader, "prompt", tc.defaultValue)
assert.Equal(t, tc.expectedOutput, actual, "unexpected output")
})
}
Expand Down

0 comments on commit ef833b9

Please sign in to comment.