-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: add generate controller cli #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-fuego/fuego/cmd/fuego/templates" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func Controller() *cli.Command { | ||
return &cli.Command{ | ||
Name: "controller", | ||
Usage: "creates a new controller file", | ||
Aliases: []string{"c"}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "output", | ||
Usage: "output file", | ||
Aliases: []string{"o"}, | ||
}, | ||
}, | ||
Action: func(cCtx *cli.Context) error { | ||
controllerName := cCtx.Args().First() | ||
|
||
if controllerName == "" { | ||
controllerName = "newController" | ||
fmt.Println("Note: You can add a controller name as an argument. Example: `fuego controller books`") | ||
} | ||
|
||
_, err := createController(controllerName, cCtx.String("output")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("🔥 Controller %s created successfully\n", controllerName) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
// createController creates a new controller file | ||
func createController(controllerName, outputFile string) (string, error) { | ||
controllerDir := "./controllers/" | ||
if _, err := os.Stat(controllerDir); os.IsNotExist(err) { | ||
err = os.Mkdir(controllerDir, 0o755) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je mets explicitement que c'est de l'octal |
||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
templateContent, err := templates.FS.ReadFile("controller/controller.go") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
t := language.English | ||
titler := cases.Title(t) | ||
|
||
newContent := strings.ReplaceAll(string(templateContent), "newController", controllerName) | ||
newContent = strings.ReplaceAll(newContent, "NewController", titler.String(controllerName)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. askip mon IDE me dit qu'il faut préférer utiliser |
||
|
||
controllerPath := outputFile | ||
if controllerPath == "" { | ||
controllerPath = fmt.Sprintf("%s%s.go", outputFile, controllerName) | ||
} | ||
|
||
err = os.WriteFile(controllerPath, []byte(newContent), 0o644) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return newContent, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package commands | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateController(t *testing.T) { | ||
res, err := createController("books", "/dev/null") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Et on peut direct utiliser notre feature pour que les tests polluent pas notre workspace :) |
||
require.NoError(t, err) | ||
require.Contains(t, res, "package controller") | ||
require.Contains(t, res, `fuego.Get(booksGroup, "/{id}", rs.getBooks)`) | ||
require.Contains(t, res, `func (rs BooksRessources) postBooks(c *fuego.ContextWithBody[BooksCreate]) (Books, error)`) | ||
} |
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 | ||
) |
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= |
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: "The framework for busy Go developers", | ||
Action: func(c *cli.Context) error { | ||
fmt.Println("The 🔥 CLI!") | ||
return nil | ||
}, | ||
Commands: []*cli.Command{ | ||
commands.Controller(), | ||
}, | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renommé parce que comme ça on est sûrs de pas faire de bêtises, on a le formattting, les imports etc... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/go-fuego/fuego" | ||
) | ||
|
||
type NewControllerRessources struct { | ||
// TODO add ressources | ||
NewControllerService NewControllerService | ||
} | ||
|
||
type NewController struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type NewControllerCreate struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
type NewControllerUpdate struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func (rs NewControllerRessources) Routes(s *fuego.Server) { | ||
newControllerGroup := fuego.Group(s, "/newController") | ||
|
||
fuego.Get(newControllerGroup, "/", rs.getAllNewController) | ||
fuego.Post(newControllerGroup, "/", rs.postNewController) | ||
|
||
fuego.Get(newControllerGroup, "/{id}", rs.getNewController) | ||
fuego.Put(newControllerGroup, "/{id}", rs.putNewController) | ||
fuego.Delete(newControllerGroup, "/{id}", rs.deleteNewController) | ||
} | ||
|
||
func (rs NewControllerRessources) getAllNewController(c fuego.ContextNoBody) ([]NewController, error) { | ||
return rs.NewControllerService.GetAllNewController() | ||
} | ||
|
||
func (rs NewControllerRessources) postNewController(c *fuego.ContextWithBody[NewControllerCreate]) (NewController, error) { | ||
body, err := c.Body() | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
new, err := rs.NewControllerService.CreateNewController(body) | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs NewControllerRessources) getNewController(c fuego.ContextNoBody) (NewController, error) { | ||
return rs.NewControllerService.GetNewController(c.PathParam("id")) | ||
} | ||
|
||
func (rs NewControllerRessources) putNewController(c *fuego.ContextWithBody[NewControllerUpdate]) (NewController, error) { | ||
body, err := c.Body() | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
new, err := rs.NewControllerService.UpdateNewController(c.PathParam("id"), body) | ||
if err != nil { | ||
return NewController{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs NewControllerRessources) deleteNewController(c *fuego.ContextNoBody) (any, error) { | ||
return rs.NewControllerService.DeleteNewController(c.PathParam("id")) | ||
} | ||
|
||
type NewControllerService interface { | ||
GetNewController(id string) (NewController, error) | ||
CreateNewController(NewControllerCreate) (NewController, error) | ||
GetAllNewController() ([]NewController, error) | ||
UpdateNewController(id string, input NewControllerUpdate) (NewController, error) | ||
DeleteNewController(id string) (any, error) | ||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'ai rajouté un corps aux fonctions et les signatures d'interface : Comme ça, plus qu'à implémenter les interfaces (faire la partie BDD) et on a notre CRUD fini ! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package templates | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
//go:embed */*.go | ||
var FS embed.FS |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ go 1.22.0 | |
|
||
use ( | ||
. | ||
./cmd/fuego | ||
./examples/basic | ||
./examples/full-app-gourmet | ||
./examples/hello-world | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ce sera une feature demandée je pense