-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenCodeX.go
More file actions
138 lines (119 loc) · 3.21 KB
/
Copy pathGenCodeX.go
File metadata and controls
138 lines (119 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// GenCodeX.go
package GenCodeX
import (
controller "GenCodeX/controller"
db "GenCodeX/db"
"GenCodeX/handler"
"GenCodeX/models"
"fmt"
"log"
"os"
"sort"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/urfave/cli/v2"
)
func main() {
var language string = "javascript"
color.Cyan("CodegenX : Generate boilerplate code for your project in no time")
client := db.Dbconnect()
app := &cli.App{
Name: "CodeGenX",
Usage: "Generate boilerplate code for your project in no time",
Authors: []*cli.Author{{
Name: "Kanak Kholwal",
Email: "kanakkholwal@gmail.com",
}},
Version: "0.0.1",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lang",
Value: "javascript",
Usage: "language for the code to be generated",
Destination: &language,
},
},
Action: func(cCtx *cli.Context) error {
langcontroller := controller.GetLangs(client)
if len(langcontroller) == 0 {
color.Magenta("No languages found")
return nil
}
// Sort the language controller alphabetically
sort.SliceStable(langcontroller, func(i, j int) bool {
return langcontroller[i].ProgLang < langcontroller[j].ProgLang
})
// Create a prompt for user selection
langPrompt := promptui.Select{
Label: "Select a language",
Items: Map(langcontroller, func(lang controller.Options) string {
return lang.ProgLang
}),
}
_, selectedLang, err := langPrompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
panic(err)
}
// Perform a search to find the selected language
var langIndex = 0
for idx, lang := range langcontroller {
if lang.ProgLang == selectedLang {
langIndex = idx
break
}
}
topicPrompt := promptui.Select{
Label: "Select a Topic",
Items: langcontroller[langIndex].Topics,
}
_, selectedTopic, err := topicPrompt.Run()
if err != nil {
color.Red("Prompt failed %v\n", err)
panic(err)
}
// fmt.Println("Selected : ", langIndex, langcontroller[langIndex].ProgLang, selectedTopic)
codeSuggestions := controller.GetTemplateSuggestions(client, langcontroller[langIndex].ProgLang, selectedTopic)
sort.SliceStable(codeSuggestions, func(i, j int) bool {
return codeSuggestions[i].Title < codeSuggestions[j].Title
})
if len(codeSuggestions) == 0 {
color.Magenta("No code templates found")
return nil
}
codePrompt := promptui.Select{
Label: "Select a Code Template",
Items: Map(codeSuggestions, func(template models.BoilerPlate) string {
return template.Title
}),
}
_, selectedCodeTemplate, err := codePrompt.Run()
if err != nil {
color.Red("Prompt failed %v\n", err)
panic(err)
}
// Perform a search to find the selected template
var tempIndex = 0
for idx, lang := range codeSuggestions {
if lang.Title == selectedCodeTemplate {
tempIndex = idx
break
}
}
var template models.BoilerPlate = codeSuggestions[tempIndex]
handler.GenerateCode(template)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
db.CloseClientDB(client)
}
func Map[T, U any](ts []T, f func(T) U) []U {
us := make([]U, len(ts))
for i := range ts {
us[i] = f(ts[i])
}
return us
}