Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAC-89 | Generate controller and router from the bru file #11

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ geng gen module <module-name>

<img width="457" alt="image" src="https://github.com/mukezhz/geng/assets/43813670/a0b11d39-e077-4038-852f-7b5b0adb27c8">

### Generate from Bru file

<img width="457" alt="image" src="https://github.com/mukezhz/geng/assets/43813670/e0f01642-cfb1-4e40-b34b-d8a76fdb2268">

### Logo

<img width="45" alt="image" src="https://github.com/mukezhz/geng/assets/43813670/da07d8cc-8896-4a13-9b31-099958e65cb4">
Expand Down
86 changes: 86 additions & 0 deletions cmd/bru_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/gookit/color"
"github.com/mukezhz/bru-go/bru"
"github.com/mukezhz/geng/pkg/gen"
"github.com/mukezhz/geng/pkg/model"
"github.com/mukezhz/geng/pkg/utility"
"github.com/spf13/cobra"
)

var bruCmd = &cobra.Command{
Use: "bru [path]",
Short: "Generate from Bru file",
Args: cobra.MaximumNArgs(2),
Run: generateFromBru,
}

func generateFromBru(_ *cobra.Command, args []string) {
WalkBruFiles(args[0])
}

func WalkBruFiles(rootDir string) {

err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".bru") {
content, err := os.ReadFile(path)
if err != nil {
return err
}
bru, err := bru.Unmarshal(content)
if err != nil {
return err
}

m := utility.GetModuleNameFromPath(path)
b := model.BruModel{
Route: utility.SanitizeEndpoint(bru.HTTP.URL),
Method: strings.ToUpper(bru.HTTP.Method),
Body: bru.Body.Content,
Handler: utility.ToPasalCase(bru.Meta.Name),
ModuleName: m,
Name: bru.Meta.Name,
Description: bru.Meta.Name,
}

if !utility.FileExists(filepath.Join("domain", m)) {
projectPath, projectModule := getProjectPath()
if projectModule == nil || projectPath == "" {
return nil
}

if !utility.FileExists(filepath.Join(projectPath, "domain", "module.go")) {
color.Redln("[Project not found...]")
return errors.New("please initialize the project first")
}
mainModulePath := filepath.Join(projectPath, "domain", "module.go")
if !utility.FileExists(mainModulePath) {
color.Redln("Error: module.go not found")
return nil
}
// create a module
generateModule(mainModulePath, []string{"", m}, *projectModule)
}

gen.AddRoute(b)
gen.AddController(b)
utility.PrintGenerationFromBrufile()

}
return nil
})

if err != nil {
fmt.Printf("Error walking the path %q: %v\n", rootDir, err)
}
}
29 changes: 29 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"
"os"

"github.com/gookit/color"
"github.com/mukezhz/geng/pkg/model"
"github.com/mukezhz/geng/pkg/utility"
)

func getProjectPath() (string, *model.GoMod) {
projectModule, err := utility.GetModuleNameFromGoModFile()
if err != nil {
fmt.Println("Error finding Module name from go.mod:", err)
return "", nil
}
currentDir, err := os.Getwd()
if err != nil {
color.Redln("Error getting current directory:", err)
panic(err)
}
projectPath, err := utility.FindGitRoot(currentDir)
if err != nil {
fmt.Println("Error finding Git root:", err)
return "", &projectModule
}
return projectPath, &projectModule
}
54 changes: 32 additions & 22 deletions cmd/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/mukezhz/geng/pkg/constant"
"github.com/mukezhz/geng/pkg/gen"
"github.com/mukezhz/geng/pkg/terminal"
"github.com/mukezhz/geng/pkg/utility"
"github.com/spf13/cobra"
)

Expand All @@ -31,6 +32,35 @@ func createProject(cmd *cobra.Command, args []string) {
var questions []terminal.ProjectQuestion
choice := projectGen.Infra.GetChoices()

if utility.FileExists("geng.json") {
projectGen.FillProjectMetadataFromJson()
} else {
questions = getValueFromTerminal(args, questions, choice, cmd)
}

if err := projectGen.Validate(); err != nil {
color.Redln(err.Error())
return
}

var selectedItems []int
for _, q := range questions {
if q.Key == constant.InfrastructureNameKEY {
selected := q.Input.Selected()
for s := range selected {
selectedItems = append(selectedItems, s)
}
}
}

if err := projectGen.Generate(selectedItems); err != nil {
color.Redln(err.Error())
return
}

}

func getValueFromTerminal(args []string, questions []terminal.ProjectQuestion, choice *gen.InfraChoice, cmd *cobra.Command) []terminal.ProjectQuestion {
if len(args) == 0 {
questions = []terminal.ProjectQuestion{
terminal.NewShortQuestion(constant.ProjectNameKEY, constant.ProjectName+" *", "Enter Project Name:"),
Expand All @@ -50,7 +80,7 @@ func createProject(cmd *cobra.Command, args []string) {
questionMap[q.Key] = q.Answer
if q.Input.Exited() {
color.Redln("exited without completing...")
return
return nil
}
}

Expand All @@ -62,25 +92,5 @@ func createProject(cmd *cobra.Command, args []string) {
projectGen.GoVersion, _ = cmd.Flags().GetString("version")
projectGen.Directory, _ = cmd.Flags().GetString("dir")
}

if err := projectGen.Validate(); err != nil {
color.Redln(err.Error())
return
}

var selectedItems []int
for _, q := range questions {
if q.Key == constant.InfrastructureNameKEY {
selected := q.Input.Selected()
for s := range selected {
selectedItems = append(selectedItems, s)
}
}
}

if err := projectGen.Generate(selectedItems); err != nil {
color.Redln(err.Error())
return
}

return questions
}
24 changes: 8 additions & 16 deletions cmd/new_module.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/gookit/color"
Expand Down Expand Up @@ -35,28 +33,22 @@ func generate(_ *cobra.Command, args []string) {
if len(args) == 0 {
args = append(args, "module")
}
projectModule, err := utility.GetModuleNameFromGoModFile()
if err != nil {
fmt.Println("Error finding Module name from go.mod:", err)

projectPath, projectModule := getProjectPath()
if projectModule == nil || projectPath == "" {
return
}
currentDir, err := os.Getwd()
if err != nil {
color.Redln("Error getting current directory:", err)
panic(err)
}
projectPath, err := utility.FindGitRoot(currentDir)
if err != nil {
fmt.Println("Error finding Git root:", err)
mainModulePath := filepath.Join(projectPath, "domain", "module.go")
if !utility.FileExists(mainModulePath) {
color.Redln("Error: module.go not found")
return
}
// Define the directory structure
generateModule(projectPath, args, projectModule)
generateModule(mainModulePath, args, *projectModule)

}

func generateModule(projectPath string, args []string, projectModule model.GoMod) {
mainModulePath := filepath.Join(projectPath, "domain", "module.go")
func generateModule(mainModulePath string, args []string, projectModule model.GoMod) {
var moduleName string
if len(args) == 1 {
questions := []terminal.ProjectQuestion{
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ func init() {
seedProjectCmd,
startProjectCmd,
migrationProjectCmd,
bruCmd,
)
}
10 changes: 10 additions & 0 deletions geng.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"projectName": "haha",
"projectModuleName": "github.com/mukezhz/haha",
"author": "Mukesh Kumar Chaudhary <[email protected]>",
"projectDescription": "A simple project",
"goVersion": "1.21"
// "directory": ".",
// "infrastructureName": [],
// "serviceName": []
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
github.com/gookit/color v1.5.4
github.com/spf13/cobra v1.8.0
golang.org/x/text v0.14.0
golang.org/x/tools v0.6.0
muzzammil.xyz/jsonc v1.0.0
)

require (
Expand All @@ -25,7 +27,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/mukezhz/bru-go v0.0.1 // indirect
github.com/mukezhz/bru-go v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/mukezhz/bru-go v0.0.0-20240707022614-f2cc65a6ead3 h1:xPiN2zAUPfPb5njsMgmSqfcFJeoy31Qbia7BXZPQvC0=
github.com/mukezhz/bru-go v0.0.0-20240707022614-f2cc65a6ead3/go.mod h1:34XGFbrPJ9eWFeSxEkBnYDsoRTM0JAV2x22AODJDgAQ=
github.com/mukezhz/bru-go v0.0.1 h1:V2Wuks+zTSDm6X3YGEcprFZshC9ARwdKOGN3SuTEvjg=
github.com/mukezhz/bru-go v0.0.1/go.mod h1:34XGFbrPJ9eWFeSxEkBnYDsoRTM0JAV2x22AODJDgAQ=
github.com/mukezhz/bru-go v0.0.2 h1:Q3Y9/jwbhTI07dH5OPG2xSL23vwjMhqtngDF1VKKoVU=
github.com/mukezhz/bru-go v0.0.2/go.mod h1:34XGFbrPJ9eWFeSxEkBnYDsoRTM0JAV2x22AODJDgAQ=
github.com/mukezhz/bru-go v0.1.0 h1:Men3Mh9U7HZSggi9+m029S4MKEYYBcT2OONcF6yiAS8=
github.com/mukezhz/bru-go v0.1.0/go.mod h1:34XGFbrPJ9eWFeSxEkBnYDsoRTM0JAV2x22AODJDgAQ=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -63,6 +65,10 @@ golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
muzzammil.xyz/jsonc v1.0.0 h1:B6kaT3wHueZ87mPz3q1nFuM1BlL32IG0wcq0/uOsQ18=
muzzammil.xyz/jsonc v1.0.0/go.mod h1:rFv8tUUKe+QLh7v02BhfxXEf4ZHhYD7unR93HL/1Uvo=
Loading