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 (
1516type PromptHandler struct {
1617 moduleconfig.Parameter
1718 Condition func (map [string ]string ) bool
19+ Validate func (string ) error
1820}
1921
2022func NoCondition (map [string ]string ) bool {
2123 return true
2224}
25+
2326func 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
3048func (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
0 commit comments