-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mukezhz/feature/add-infra-service
feat: add support to generate infrastructure and inject the provider …
- Loading branch information
Showing
34 changed files
with
1,568 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package cmd | ||
|
||
import ( | ||
"embed" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gookit/color" | ||
"github.com/mukezhz/geng/pkg/constant" | ||
"github.com/mukezhz/geng/pkg/model" | ||
"github.com/mukezhz/geng/pkg/terminal" | ||
"github.com/mukezhz/geng/pkg/utility" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var addInfrastructureCmd = &cobra.Command{ | ||
Use: "infra add [name]", | ||
Short: "Add a new infrastructure", | ||
Args: cobra.MaximumNArgs(2), | ||
Run: addInfrastructureHandler, | ||
} | ||
|
||
func addInfrastructureHandler(_ *cobra.Command, args []string) { | ||
if len(args) > 0 && !strings.Contains(args[0], "add") { | ||
color.Redln("Error: invalid command") | ||
return | ||
} | ||
projectModule, err := utility.GetModuleNameFromGoModFile() | ||
if err != nil { | ||
color.Redln("Error finding Module name from go.mod:", err) | ||
return | ||
} | ||
data := utility.GetModuleDataFromModuleName("", projectModule.Module, projectModule.GoVersion) | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
color.Redln("Error getting current directory:", err) | ||
panic(err) | ||
} | ||
projectPath, err := utility.FindGitRoot(currentDir) | ||
if err != nil { | ||
color.Redln("Error finding Git root:", err) | ||
return | ||
} | ||
infrastructureModulePath := filepath.Join(projectPath, "pkg", "infrastructure", "module.go") | ||
templateInfraPath := filepath.Join(".", "templates", "wesionary", "infrastructure") | ||
infrasTmpl := utility.ListDirectory(templatesFS, templateInfraPath) | ||
infras := utility.Map[string, string](infrasTmpl, func(q string) string { | ||
return strings.Replace(q, ".tmpl", "", 1) | ||
}) | ||
questions := []terminal.ProjectQuestion{ | ||
terminal.NewCheckboxQuestion(constant.InfrastructureNameKEY, "Select the infrastructure? [<space> to select]", infras), | ||
} | ||
|
||
terminal.StartInteractiveTerminal(questions) | ||
|
||
items := addInfrastructure(questions, infrasTmpl, infrastructureModulePath, data, false, templatesFS) | ||
if len(items) == 0 { | ||
color.Red.Println("No infrastructure selected") | ||
return | ||
} | ||
var selectedInfras []string | ||
for _, i := range items { | ||
selectedInfras = append(selectedInfras, infras[i]) | ||
} | ||
_ = selectedInfras // Fix for SA4010: this result of append is never used, except maybe in other appends | ||
utility.PrintColorizeInfrastructureDetail(data, selectedInfras) | ||
} | ||
|
||
func addInfrastructure( | ||
questions []terminal.ProjectQuestion, | ||
infrasTmpl []string, | ||
infrastructureModulePath string, | ||
data model.ModuleData, | ||
isNewProject bool, | ||
templatesFS embed.FS) []int { | ||
var functions []string | ||
var items []int | ||
for _, q := range questions { | ||
switch q.Key { | ||
case constant.InfrastructureNameKEY: | ||
selected := q.Input.Selected() | ||
for s := range selected { | ||
functions = append(functions, | ||
utility.GetFunctionDeclarations(filepath.Join(".", "templates", "wesionary", "infrastructure", infrasTmpl[s]), templatesFS)..., | ||
) | ||
items = append(items, s) | ||
} | ||
} | ||
} | ||
if len(items) == 0 { | ||
return items | ||
} | ||
updatedCode := utility.AddListOfProvideInFxOptions(infrastructureModulePath, functions) | ||
utility.WriteContentToPath(infrastructureModulePath, updatedCode) | ||
|
||
servicesTmplMap := make(map[string]bool) | ||
for _, i := range items { | ||
templatePath := filepath.Join(".", "templates", "wesionary", "infrastructure", infrasTmpl[i]) | ||
var targetRoot string | ||
if isNewProject { | ||
if data.Directory == "" { | ||
targetRoot = filepath.Join(data.PackageName, "pkg", "infrastructure", strings.Replace(infrasTmpl[i], ".tmpl", ".go", 1)) | ||
} else { | ||
targetRoot = filepath.Join(data.Directory, "pkg", "infrastructure", strings.Replace(infrasTmpl[i], ".tmpl", ".go", 1)) | ||
} | ||
} else { | ||
targetRoot = filepath.Join(".", "pkg", "infrastructure", strings.Replace(infrasTmpl[i], ".tmpl", ".go", 1)) | ||
} | ||
|
||
fileName := strings.Replace(infrasTmpl[i], ".tmpl", "", 1) | ||
serviceTemplatePath := filepath.Join(".", "templates", "wesionary", "service") | ||
for _, file := range utility.ListDirectory(templatesFS, serviceTemplatePath) { | ||
if strings.Contains(file, fileName) { | ||
servicesTmplMap[file] = true | ||
} | ||
} | ||
utility.GenerateFromEmbeddedTemplate(templatesFS, templatePath, targetRoot, data) | ||
|
||
} | ||
// add service too | ||
var serviceModulePath string | ||
if isNewProject { | ||
serviceModulePath = filepath.Join(data.PackageName, "pkg", "services", "module.go") | ||
} else { | ||
serviceModulePath = filepath.Join(".", "pkg", "services", "module.go") | ||
} | ||
var servicesTmpl []string | ||
for k := range servicesTmplMap { | ||
servicesTmpl = append(servicesTmpl, k) | ||
} | ||
|
||
addService(questions, servicesTmpl, serviceModulePath, data, true, templatesFS) | ||
return items | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cmd | ||
|
||
import ( | ||
"embed" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gookit/color" | ||
"github.com/mukezhz/geng/pkg/constant" | ||
"github.com/mukezhz/geng/pkg/model" | ||
"github.com/mukezhz/geng/pkg/terminal" | ||
"github.com/mukezhz/geng/pkg/utility" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var addServiceCmd = &cobra.Command{ | ||
Use: "service add [name]", | ||
Short: "Add a new Service", | ||
Args: cobra.MaximumNArgs(2), | ||
Run: addServiceHandler, | ||
} | ||
|
||
func addServiceHandler(_ *cobra.Command, args []string) { | ||
if len(args) > 0 && !strings.Contains(args[0], "add") { | ||
color.Redln("Error: invalid command") | ||
return | ||
} | ||
projectModule, err := utility.GetModuleNameFromGoModFile() | ||
if err != nil { | ||
color.Redln("Error finding Module name from go.mod:", err) | ||
return | ||
} | ||
data := utility.GetModuleDataFromModuleName("", projectModule.Module, projectModule.GoVersion) | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
color.Redln("Error getting current directory:", err) | ||
panic(err) | ||
} | ||
projectPath, err := utility.FindGitRoot(currentDir) | ||
if err != nil { | ||
color.Redln("Error finding Git root:", err) | ||
return | ||
} | ||
serviceModulePath := filepath.Join(projectPath, "pkg", "services", "module.go") | ||
templateInfraPath := filepath.Join(".", "templates", "wesionary", "service") | ||
servicesTmpl := utility.ListDirectory(templatesFS, templateInfraPath) | ||
services := utility.Map[string, string](servicesTmpl, func(q string) string { | ||
return strings.Replace(q, ".tmpl", "", 1) | ||
}) | ||
|
||
questions := []terminal.ProjectQuestion{ | ||
terminal.NewCheckboxQuestion(constant.ServiceNameKEY, "Select the Service? [<space> to select]", services), | ||
} | ||
|
||
terminal.StartInteractiveTerminal(questions) | ||
|
||
items := addService(questions, servicesTmpl, serviceModulePath, data, false, templatesFS) | ||
if len(items) == 0 { | ||
color.Red.Println("No Service selected") | ||
return | ||
} | ||
var selectedServices []string | ||
for _, i := range items { | ||
selectedServices = append(selectedServices, services[i]) | ||
} | ||
utility.PrintColorizeServiceDetail(data, selectedServices) | ||
} | ||
|
||
func addService( | ||
questions []terminal.ProjectQuestion, | ||
servicesTmpl []string, | ||
serviceModulePath string, | ||
data model.ModuleData, | ||
isNewProject bool, | ||
templatesFS embed.FS) []int { | ||
var functions []string | ||
var items []int | ||
for _, q := range questions { | ||
switch q.Key { | ||
case constant.ServiceNameKEY: | ||
selected := q.Input.Selected() | ||
for s := range selected { | ||
funcs := utility.GetFunctionDeclarations(filepath.Join(".", "templates", "wesionary", "service", servicesTmpl[s]), templatesFS) | ||
filteredServices := utility.Filter[string](funcs, func(q string) bool { | ||
return strings.Contains(q, "New") | ||
}) | ||
functions = append(functions, | ||
filteredServices..., | ||
) | ||
items = append(items, s) | ||
} | ||
case constant.InfrastructureNameKEY: | ||
for i, s := range servicesTmpl { | ||
funcs := utility.GetFunctionDeclarations(filepath.Join(".", "templates", "wesionary", "service", s), templatesFS) | ||
filteredServices := utility.Filter[string](funcs, func(q string) bool { | ||
return strings.Contains(q, "New") | ||
}) | ||
functions = append(functions, | ||
filteredServices..., | ||
) | ||
items = append(items, i) | ||
} | ||
|
||
} | ||
} | ||
if len(items) == 0 { | ||
return items | ||
} | ||
updatedCode := utility.AddListOfProvideInFxOptions(serviceModulePath, functions) | ||
utility.WriteContentToPath(serviceModulePath, updatedCode) | ||
|
||
for _, i := range items { | ||
templatePath := filepath.Join(".", "templates", "wesionary", "service", servicesTmpl[i]) | ||
var targetRoot string | ||
if isNewProject { | ||
targetRoot = filepath.Join(data.PackageName, "pkg", "services", strings.Replace(servicesTmpl[i], ".tmpl", ".go", 1)) | ||
} else { | ||
targetRoot = filepath.Join(".", "pkg", "services", strings.Replace(servicesTmpl[i], ".tmpl", ".go", 1)) | ||
} | ||
utility.GenerateFromEmbeddedTemplate(templatesFS, templatePath, targetRoot, data) | ||
} | ||
return items | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package cmd | |
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gookit/color" | ||
"github.com/mukezhz/geng/pkg/constant" | ||
|
@@ -25,15 +26,23 @@ func createProject(cmd *cobra.Command, args []string) { | |
var projectDescription string | ||
var author string | ||
var directory string | ||
var questions []terminal.ProjectQuestion | ||
|
||
templateInfraPath := filepath.Join(".", "templates", "wesionary", "infrastructure") | ||
infrasTmpl := utility.ListDirectory(templatesFS, templateInfraPath) | ||
infras := utility.Map[string, string](infrasTmpl, func(q string) string { | ||
return strings.Replace(q, ".tmpl", "", 1) | ||
}) | ||
|
||
if len(args) == 0 { | ||
questions := []terminal.ProjectQuestion{ | ||
questions = []terminal.ProjectQuestion{ | ||
terminal.NewShortQuestion(constant.ProjectNameKEY, constant.ProjectName+" *", "Enter Project Name:"), | ||
terminal.NewShortQuestion(constant.ProjectModuleNameKEY, constant.ProjectModuleName+" *", "Enter Module Name:"), | ||
terminal.NewShortQuestion(constant.AuthorKEY, constant.Author+" [Optional]", "Enter Author Detail[Mukesh Chaudhary <[email protected]>] [Optional]"), | ||
terminal.NewLongQuestion(constant.ProjectDescriptionKEY, constant.ProjectDescription+" [Optional]", "Enter Project Description [Optional]"), | ||
terminal.NewShortQuestion(constant.GoVersionKEY, constant.GoVersion+" [Optional]", "Enter Go Version (Default: 1.20) [Optional]"), | ||
terminal.NewShortQuestion(constant.DirectoryKEY, constant.Directory+" [Optional]", "Enter Project Directory (Default: package_name) [Optional]"), | ||
terminal.NewCheckboxQuestion(constant.InfrastructureNameKEY, "Select the infrastructure? [<space> to select] [Optional]", infras), | ||
} | ||
terminal.StartInteractiveTerminal(questions) | ||
|
||
|
@@ -86,6 +95,13 @@ func createProject(cmd *cobra.Command, args []string) { | |
color.Redln("Error generate file", err) | ||
return | ||
} | ||
for _, q := range questions { | ||
switch q.Key { | ||
case constant.InfrastructureNameKEY: | ||
infrastructureModulePath := filepath.Join(data.PackageName, "pkg", "infrastructure", "module.go") | ||
addInfrastructure(questions, infrasTmpl, infrastructureModulePath, data, true, templatesFS) | ||
} | ||
} | ||
|
||
utility.PrintColorizeProjectDetail(data) | ||
fmt.Println("") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.