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

feat: add generate controller cli #39

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 22 additions & 11 deletions cmd/fuego/commands/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ func Controller() *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"},
},
},
Comment on lines +19 to +25
Copy link
Member

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

Action: func(cCtx *cli.Context) error {
controllerName := "newController"
if cCtx.NArg() > 0 {
controllerName = cCtx.Args().First()
} else {
controllerName := cCtx.Args().First()

if controllerName == "" {
controllerName = "newController"
fmt.Println("Note: You can add a controller name as an argument. Example: `fuego controller books`")
}
Copy link
Member

@EwenQuim EwenQuim Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'abord on prend la valeur, et seulement si elle n'est pas précisée, on lui assigne la valeur par défaut


err := createController(controllerName)
_, err := createController(controllerName, cCtx.String("output"))
if err != nil {
return err
}
Expand All @@ -36,18 +43,18 @@ func Controller() *cli.Command {
}

// createController creates a new controller file
func createController(controllerName string) error {
func createController(controllerName, outputFile string) (string, error) {
controllerDir := "./controllers/"
if _, err := os.Stat(controllerDir); os.IsNotExist(err) {
err = os.Mkdir(controllerDir, 0o755)
Copy link
Member

Choose a reason for hiding this comment

The 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
return "", err
}
}

templateContent, err := templates.FS.ReadFile("controller/controller.go")
if err != nil {
return err
return "", err
}

t := language.English
Expand All @@ -56,11 +63,15 @@ func createController(controllerName string) error {
newContent := strings.ReplaceAll(string(templateContent), "newController", controllerName)
newContent = strings.ReplaceAll(newContent, "NewController", titler.String(controllerName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

askip mon IDE me dit qu'il faut préférer utiliser cases


controllerPath := fmt.Sprintf("%s%s.go", controllerDir, controllerName)
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 "", err
}

return nil
return newContent, nil
}
5 changes: 4 additions & 1 deletion cmd/fuego/commands/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func TestCreateController(t *testing.T) {
err := createController("books")
res, err := createController("books", "/dev/null")
Copy link
Member

Choose a reason for hiding this comment

The 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)`)
}
Loading