diff --git a/pkg/jx/cmd/init.go b/pkg/jx/cmd/init.go index e73ea5e8aa..9e79f323e6 100644 --- a/pkg/jx/cmd/init.go +++ b/pkg/jx/cmd/init.go @@ -817,7 +817,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr return domain, err } log.Infof("\nOn AWS we recommend using a custom DNS name to access services in your Kubernetes cluster to ensure you can use all of your Availability Zones\n") - log.Infof("If you do not have a custom DNS name you can use yet you can register a new one here: %s\n\n", util.ColorInfo("https://console.aws.amazon.com/route53/home?#DomainRegistration:")) + log.Infof("If you do not have a custom DNS name you can use yet, then you can register a new one here: %s\n\n", util.ColorInfo("https://console.aws.amazon.com/route53/home?#DomainRegistration:")) for { if util.Confirm("Would you like to register a wildcard DNS ALIAS to point at this ELB address? ", true, @@ -902,7 +902,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr } log.Successf("You can now configure a wildcard DNS pointing to the new loadbalancer address %s", address) log.Info("\nIf you do not have a custom domain setup yet, Ingress rules will be set for magic dns nip.io.") - log.Infof("\nOnce you have a customer domain ready, you can update with the command %s", util.ColorInfo("jx upgrade ingress --cluster")) + log.Infof("\nOnce you have a custom domain ready, you can update with the command %s", util.ColorInfo("jx upgrade ingress --cluster")) log.Infof("\nIf you don't have a wildcard DNS setup then setup a new CNAME and point it at: %s then use the DNS domain in the next input...\n", defaultDomain) @@ -912,7 +912,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr Default: defaultDomain, Help: "Enter your custom domain that is used to generate Ingress rules, defaults to the magic dns nip.io", } - survey.AskOne(prompt, &domain, survey.Required, surveyOpts) + survey.AskOne(prompt, &domain, survey.ComposeValidators(survey.Required, util.NoWhiteSpaceValidator()), surveyOpts) } if domain == "" { domain = defaultDomain @@ -925,3 +925,4 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr return domain, nil } + diff --git a/pkg/util/validators.go b/pkg/util/validators.go new file mode 100644 index 0000000000..919a26bffd --- /dev/null +++ b/pkg/util/validators.go @@ -0,0 +1,23 @@ +package util + +import ( + "fmt" + "gopkg.in/AlecAivazis/survey.v1" + "strings" +) + +//NoWhiteSpaceValidator is an input validator for the survey package that disallows any whitespace in the input +func NoWhiteSpaceValidator() survey.Validator { + // return a validator that ensures the given string does not contain any whitespace + return func(val interface{}) error { + if str, ok := val.(string); ok { + if strings.ContainsAny(str, " ") { + // yell loudly + return fmt.Errorf("supplied value \"%v\" must not contain any whitespace", str) + } + } + // the input is fine + return nil + } +} + diff --git a/pkg/util/validators_test.go b/pkg/util/validators_test.go new file mode 100644 index 0000000000..753108fee7 --- /dev/null +++ b/pkg/util/validators_test.go @@ -0,0 +1,40 @@ +package util_test + +import ( + "github.com/jenkins-x/jx/pkg/util" + "github.com/stretchr/testify/assert" + "gopkg.in/AlecAivazis/survey.v1" + "testing" +) + +func TestNoWhitespaceValidator(t *testing.T) { + t.Parallel() + + tests := []struct { + testName string + domainName string + want string + }{ + {"leading whitespace"," fake.com", "supplied value \" fake.com\" must not contain any whitespace"}, + {"trailing whitespace","fake.com ", "supplied value \"fake.com \" must not contain any whitespace"}, + {"embedded whitespace","fake .com", "supplied value \"fake .com\" must not contain any whitespace"}, + } + + for _, tt := range tests { + t.Run(tt.testName, func(t *testing.T) { + assert.Equal(t, tt.want, testInputValidation(t, tt.domainName)) + }) + } +} + +func testInputValidation(t *testing.T, s string) interface{} { + valid := survey.ComposeValidators( + util.NoWhiteSpaceValidator(), + ) + err := valid(s) + if err != nil { + return err.Error() + } + return "" +} +