Skip to content

Commit 514557d

Browse files
committed
Added prompt validation
1 parent c625485 commit 514557d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

internal/context/init.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func getProjectNamePrompt() PromptHandler {
128128
Default: "",
129129
},
130130
NoCondition,
131+
NoValidation,
131132
}
132133
}
133134

@@ -140,6 +141,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
140141
Default: "y",
141142
},
142143
NoCondition,
144+
SpecificValueValidation("y", "n"),
143145
},
144146
"GithubRootOrg": {
145147
moduleconfig.Parameter{
@@ -148,6 +150,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
148150
Default: "github.com/",
149151
},
150152
KeyMatchCondition("ShouldPushRepositories", "y"),
153+
NoValidation,
151154
},
152155
"GithubPersonalToken": {
153156
moduleconfig.Parameter{
@@ -156,6 +159,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
156159
Default: globalconfig.GetUserCredentials(projectName).AccessToken,
157160
},
158161
KeyMatchCondition("ShouldPushRepositories", "y"),
162+
NoValidation,
159163
},
160164
}
161165

@@ -169,6 +173,7 @@ func getProjectPrompts(projectName string, modules map[string]moduleconfig.Modul
169173
Default: module.OutputDir,
170174
},
171175
NoCondition,
176+
NoValidation,
172177
}
173178
}
174179

internal/context/prompts.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"regexp"
9+
"strings"
910

1011
"github.com/commitdev/zero/internal/config/moduleconfig"
1112
"github.com/commitdev/zero/pkg/util/exit"
@@ -15,17 +16,34 @@ import (
1516
type PromptHandler struct {
1617
moduleconfig.Parameter
1718
Condition func(map[string]string) bool
19+
Validate func(string) error
1820
}
1921

2022
func NoCondition(map[string]string) bool {
2123
return true
2224
}
25+
2326
func KeyMatchCondition(key string, value string) func(map[string]string) bool {
2427
return func(param map[string]string) bool {
2528
return param[key] == value
2629
}
2730
}
2831

32+
func NoValidation(string) error {
33+
return nil
34+
}
35+
36+
func SpecificValueValidation(values ...string) func(string) error {
37+
return func(checkValue string) error {
38+
for _, allowedValue := range values {
39+
if checkValue == allowedValue {
40+
return nil
41+
}
42+
}
43+
return fmt.Errorf("Please choose one of %s", strings.Join(values, "/"))
44+
}
45+
}
46+
2947
// TODO: validation / allow prompt retry ...etc
3048
func (p PromptHandler) GetParam(projectParams map[string]string) string {
3149
var err error
@@ -40,7 +58,7 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string {
4058
} else if p.Parameter.Value != "" {
4159
result = p.Parameter.Value
4260
} else {
43-
err, result = promptParameter(p.Parameter)
61+
err, result = promptParameter(p)
4462
}
4563
if err != nil {
4664
exit.Fatal("Exiting prompt: %v\n", err)
@@ -51,7 +69,8 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string {
5169
return ""
5270
}
5371

54-
func promptParameter(param moduleconfig.Parameter) (error, string) {
72+
func promptParameter(prompt PromptHandler) (error, string) {
73+
param := prompt.Parameter
5574
label := param.Label
5675
if param.Label == "" {
5776
label = param.Field
@@ -72,6 +91,7 @@ func promptParameter(param moduleconfig.Parameter) (error, string) {
7291
Label: label,
7392
Default: defaultValue,
7493
AllowEdit: true,
94+
Validate: prompt.Validate,
7595
}
7696
result, err = prompt.Run()
7797
}
@@ -119,6 +139,7 @@ func PromptModuleParams(moduleConfig moduleconfig.ModuleConfig, parameters map[s
119139
promptHandler := PromptHandler{
120140
promptConfig,
121141
NoCondition,
142+
NoValidation,
122143
}
123144
result := promptHandler.GetParam(parameters)
124145

internal/context/prompts_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestGetParam(t *testing.T) {
2020
prompt := context.PromptHandler{
2121
param,
2222
context.NoCondition,
23+
context.NoValidation,
2324
}
2425

2526
result := prompt.GetParam(projectParams)
@@ -35,6 +36,7 @@ func TestGetParam(t *testing.T) {
3536
prompt := context.PromptHandler{
3637
param,
3738
context.NoCondition,
39+
context.NoValidation,
3840
}
3941

4042
result := prompt.GetParam(map[string]string{
@@ -52,6 +54,7 @@ func TestGetParam(t *testing.T) {
5254
prompt := context.PromptHandler{
5355
param,
5456
context.NoCondition,
57+
context.NoValidation,
5558
}
5659

5760
result := prompt.GetParam(projectParams)

0 commit comments

Comments
 (0)