Skip to content

Commit

Permalink
Added prompt validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bmonkman committed Jun 9, 2020
1 parent c625485 commit 514557d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/context/init.go
Expand Up @@ -128,6 +128,7 @@ func getProjectNamePrompt() PromptHandler {
Default: "",
},
NoCondition,
NoValidation,
}
}

Expand All @@ -140,6 +141,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
Default: "y",
},
NoCondition,
SpecificValueValidation("y", "n"),
},
"GithubRootOrg": {
moduleconfig.Parameter{
Expand All @@ -148,6 +150,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
Default: "github.com/",
},
KeyMatchCondition("ShouldPushRepositories", "y"),
NoValidation,
},
"GithubPersonalToken": {
moduleconfig.Parameter{
Expand All @@ -156,6 +159,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
Default: globalconfig.GetUserCredentials(projectName).AccessToken,
},
KeyMatchCondition("ShouldPushRepositories", "y"),
NoValidation,
},
}

Expand All @@ -169,6 +173,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
Default: module.OutputDir,
},
NoCondition,
NoValidation,
}
}

Expand Down
25 changes: 23 additions & 2 deletions internal/context/prompts.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"regexp"
"strings"

"github.com/commitdev/zero/internal/config/moduleconfig"
"github.com/commitdev/zero/pkg/util/exit"
Expand All @@ -15,17 +16,34 @@ import (
type PromptHandler struct {
moduleconfig.Parameter
Condition func(map[string]string) bool
Validate func(string) error
}

func NoCondition(map[string]string) bool {
return true
}

func KeyMatchCondition(key string, value string) func(map[string]string) bool {
return func(param map[string]string) bool {
return param[key] == value
}
}

func NoValidation(string) error {
return nil
}

func SpecificValueValidation(values ...string) func(string) error {
return func(checkValue string) error {
for _, allowedValue := range values {
if checkValue == allowedValue {
return nil
}
}
return fmt.Errorf("Please choose one of %s", strings.Join(values, "/"))
}
}

// TODO: validation / allow prompt retry ...etc
func (p PromptHandler) GetParam(projectParams map[string]string) string {
var err error
Expand All @@ -40,7 +58,7 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string {
} else if p.Parameter.Value != "" {
result = p.Parameter.Value
} else {
err, result = promptParameter(p.Parameter)
err, result = promptParameter(p)
}
if err != nil {
exit.Fatal("Exiting prompt: %v\n", err)
Expand All @@ -51,7 +69,8 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string {
return ""
}

func promptParameter(param moduleconfig.Parameter) (error, string) {
func promptParameter(prompt PromptHandler) (error, string) {
param := prompt.Parameter
label := param.Label
if param.Label == "" {
label = param.Field
Expand All @@ -72,6 +91,7 @@ func promptParameter(param moduleconfig.Parameter) (error, string) {
Label: label,
Default: defaultValue,
AllowEdit: true,
Validate: prompt.Validate,
}
result, err = prompt.Run()
}
Expand Down Expand Up @@ -119,6 +139,7 @@ func PromptModuleParams(moduleConfig moduleconfig.ModuleConfig, parameters map[s
promptHandler := PromptHandler{
promptConfig,
NoCondition,
NoValidation,
}
result := promptHandler.GetParam(parameters)

Expand Down
3 changes: 3 additions & 0 deletions internal/context/prompts_test.go
Expand Up @@ -20,6 +20,7 @@ func TestGetParam(t *testing.T) {
prompt := context.PromptHandler{
param,
context.NoCondition,
context.NoValidation,
}

result := prompt.GetParam(projectParams)
Expand All @@ -35,6 +36,7 @@ func TestGetParam(t *testing.T) {
prompt := context.PromptHandler{
param,
context.NoCondition,
context.NoValidation,
}

result := prompt.GetParam(map[string]string{
Expand All @@ -52,6 +54,7 @@ func TestGetParam(t *testing.T) {
prompt := context.PromptHandler{
param,
context.NoCondition,
context.NoValidation,
}

result := prompt.GetParam(projectParams)
Expand Down

0 comments on commit 514557d

Please sign in to comment.