Skip to content

Commit 827811e

Browse files
committed
add the validation question and function
1 parent e9ce823 commit 827811e

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

internals/extras/list.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,38 @@ var DatabaseListInfo = map[string]map[string][]string{
3535
"fastapi": {"pip", "install", "aiosqlite"},
3636
},
3737
}
38+
39+
40+
var ValidationList = map[string][]string{
41+
"node": {"zod", "yup", "joi", "class-validator", "none"},
42+
"fastapi": {"pydantic", "none"},
43+
"go": {"go-playground/validator", "none"},
44+
}
45+
46+
47+
var ValidationListInfo = map[string]map[string][]string{
48+
"zod": {
49+
"js": {"npm", "install", "zod"},
50+
"ts": {"npm", "install", "zod"},
51+
},
52+
"yup": {
53+
"js": {"npm", "install", "yup"},
54+
"ts": {"npm", "install", "yup", "@types/yup"},
55+
},
56+
"joi": {
57+
"js": {"npm", "install", "joi"},
58+
"ts": {"npm", "install", "joi", "@types/joi"},
59+
},
60+
"class-validator": {
61+
"js": {"npm", "install", "class-validator", "class-transformer"}, // works but not common in JS-only
62+
"ts": {"npm", "install", "class-validator", "class-transformer"},
63+
},
64+
"pydantic": {
65+
"fastapi": {"pip", "install", "pydantic"},
66+
},
67+
"go-playground/validator": {
68+
"go": {"go", "get", "github.com/go-playground/validator/v10"},
69+
},
70+
}
71+
72+

internals/prompt/prompt.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func SelectBackend(options []string)(string, error){
12-
fmt.Println("Choose a backend starter")
12+
fmt.Println("-----Choose a backend starter------")
1313

1414
for i, opt := range options {
1515
fmt.Printf("%d), %s\n",i+1, opt)
@@ -47,7 +47,7 @@ func AskProjectName()(string, error){
4747

4848
//ask for database
4949
func AskDatabase( option []string)(string, error){
50-
fmt.Println("Choice a database")
50+
fmt.Println("-----Choice a database------")
5151
for i, opt := range option {
5252
fmt.Printf(" %d). %s\n",i+1, opt)
5353
}
@@ -71,6 +71,27 @@ func AskDatabase( option []string)(string, error){
7171
//ask for auth
7272
//ask for linting
7373
//ask for validation
74+
func AskForValidator(options []string)(string,error){
75+
fmt.Println("-----Choose a Validator libray-----")
76+
for i ,opt := range options{
77+
fmt.Printf(" %d). %v\n",i+1, opt)
78+
}
79+
80+
reader := bufio.NewReader(os.Stdin)
81+
fmt.Printf("Enter your choice: ")
82+
input,err := reader.ReadString('\n')
83+
if err != nil {
84+
return "",err
85+
}
86+
input = strings.TrimSpace(input)
87+
88+
choice,err := strconv.Atoi(input)
89+
if err != nil && choice > len(options) && choice < 1 {
90+
return "", nil
91+
}
92+
93+
return options[choice - 1], nil
94+
}
7495

7596

7697
func ConfirmGit()(bool, error){

internals/runner/runValidator.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package runner
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
8+
"github.com/walonCode/backend-starter-cli/internals/extras"
9+
)
10+
11+
// RunDatabase installs DB dependencies based on stack + dbName
12+
func RunValidator(projectDir, stack, validatorName string) error {
13+
if validatorName == "none" {
14+
fmt.Println("No validator selected")
15+
return nil
16+
}
17+
18+
dbInfo, ok := extras.ValidationListInfo[validatorName]
19+
if !ok {
20+
return fmt.Errorf("unknown validator: %s", validatorName)
21+
}
22+
23+
cmdArgs, ok := dbInfo[stack]
24+
if !ok {
25+
return fmt.Errorf("no install instructions for %s in %s", validatorName, stack)
26+
}
27+
28+
if len(cmdArgs) == 0 {
29+
return fmt.Errorf("empty install command for %s in %s", validatorName, stack)
30+
}
31+
32+
// First item is the command, rest are args
33+
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
34+
cmd.Stdout = os.Stdout
35+
cmd.Stderr = os.Stderr
36+
cmd.Dir = projectDir
37+
38+
fmt.Printf("📦 Installing %s for %s...\n", validatorName, stack)
39+
if err := cmd.Run(); err != nil {
40+
return fmt.Errorf("failed to install %s: %w", validatorName, err)
41+
}
42+
43+
fmt.Printf("✅ %s setup complete!\n", validatorName)
44+
return nil
45+
}

0 commit comments

Comments
 (0)