Skip to content

Commit

Permalink
feat: add generate controller cli
Browse files Browse the repository at this point in the history
  • Loading branch information
rizerkrof committed Feb 8, 2024
1 parent 9789dab commit d8d513a
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/fuego/commands/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package commands

import (
"fmt"
"os"
"strings"

"github.com/go-fuego/fuego/cmd/fuego/templates"
"github.com/urfave/cli/v2"
)

func ControllerCommand() *cli.Command {
return &cli.Command{
Name: "controller",
Usage: "add a new template",
Aliases: []string{"c"},
Action: func(cCtx *cli.Context) error {
controllerName := "newController"
if cCtx.NArg() > 0 {
controllerName = cCtx.Args().First()
} else {
fmt.Println("Note: You can add a controller name as an argument. Example: fuego controller yourControllerName")
}

err := createController(controllerName)
if err != nil {
return err
}

fmt.Printf("🔥 Controller %s created successfully\n", controllerName)
return nil
},
}
}

func createController(controllerName string) error {
controllerDir := "./controllers/"
if _, err := os.Stat(controllerDir); os.IsNotExist(err) {
err = os.Mkdir(controllerDir, 0755)
if err != nil {
return err
}
}

templateContent, err := templates.FS.ReadFile("controller.template")

newContent := strings.ReplaceAll(string(templateContent), "newController", controllerName)
newContent = strings.ReplaceAll(newContent, "NewController", strings.Title(controllerName))

controllerPath := fmt.Sprintf("%s%s.go", controllerDir, controllerName)
err = os.WriteFile(controllerPath, []byte(newContent), 0644)
if err != nil {
return err
}

return nil
}
11 changes: 11 additions & 0 deletions cmd/fuego/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/go-fuego/fuego/cmd/fuego

go 1.21.5

require github.com/urfave/cli/v2 v2.27.1

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
8 changes: 8 additions & 0 deletions cmd/fuego/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
28 changes: 28 additions & 0 deletions cmd/fuego/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"log"
"os"

"github.com/go-fuego/fuego/cmd/fuego/commands"
"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "fuego",
Usage: "Fire like fuego!",
Action: func(*cli.Context) error {
fmt.Println("The 🔥 CLI!")
return nil
},
Commands: []*cli.Command{
commands.ControllerCommand(),
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
45 changes: 45 additions & 0 deletions cmd/fuego/templates/controller.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package controllers

import (
"github.com/go-fuego/fuego"
)

type NewControllerEntityRepository interface {
// TODO add queries interfaces
}

type NewControllerService interface {
// TODO add service functions interfaces
}

type newControllerRessources struct {
// TODO add ressourses
NewControllerRepository NewControllerRepository
NewControllerService NewControllerService
}

func (rs newControllerRessources) Routes(s *fuego.Server) {
newControllerRoutesGroupe := fuego.Group(s, "/newController")
fuego.Post(newControllerRoutesGroupe, "/", rs.postNewControllerFunction)
fuego.Get(newControllerRoutesGroupe, "/{id}", rs.getNewControllerFunction)
fuego.Patch(newControllerRoutesGroupe, "/{id}", rs.patchNewControllerFunction)
fuego.Put(newControllerRoutesGroupe, "/{id}", rs.putNewControllerFunction)
fuego.Delete(newControllerRoutesGroupe, "/{id}", rs.deleteNewControllerFunction)
}

// TODO replace the following with your own functions
func (rs newControllerRessources) postNewControllerRessources(c fuego.Ctx[any]) (string, error) {
return "🔥", nil
}
func (rs newControllerRessources) getNewControllerRessources(c fuego.Ctx[any]) (string, error) {
return "🔥", nil
}
func (rs newControllerRessources) patchNewControllerRessources(c fuego.Ctx[any]) (string, error) {
return "🔥", nil
}
func (rs newControllerRessources) putNewControllerRessources(c fuego.Ctx[any]) (string, error) {
return "🔥", nil
}
func (rs newControllerRessources) deleteNewControllerRessources(c fuego.Ctx[any]) (string, error) {
return "🔥", nil
}
8 changes: 8 additions & 0 deletions cmd/fuego/templates/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package templates

import (
"embed"
)

//go:embed *.template
var FS embed.FS
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ require (
github.com/swaggo/http-swagger v1.3.4
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -32,6 +38,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/swaggo/swag v1.16.2 // indirect
github.com/urfave/cli/v2 v2.27.1
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -53,6 +55,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -69,6 +73,10 @@ github.com/swaggo/swag v1.16.2 h1:28Pp+8DkQoV+HLzLx8RGJZXNGKbFqnuvSbAAtoxiY04=
github.com/swaggo/swag v1.16.2/go.mod h1:6YzXnDcpr0767iOejs318CwYkCQqyGer6BizOg03f+E=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use (
.
./examples/simple-crud
./examples/basic
./cmd/fuego
)
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSY
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
Expand Down

0 comments on commit d8d513a

Please sign in to comment.