-
-
Notifications
You must be signed in to change notification settings - Fork 63
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 1 commit
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 |
---|---|---|
|
@@ -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"}, | ||
}, | ||
}, | ||
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`") | ||
} | ||
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. 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 | ||
} | ||
|
@@ -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) | ||
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 | ||
return "", err | ||
} | ||
} | ||
|
||
templateContent, err := templates.FS.ReadFile("controller/controller.go") | ||
if err != nil { | ||
return err | ||
return "", err | ||
} | ||
|
||
t := language.English | ||
|
@@ -56,11 +63,15 @@ func createController(controllerName string) error { | |
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 := 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ import ( | |
) | ||
|
||
func TestCreateController(t *testing.T) { | ||
err := createController("books") | ||
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)`) | ||
} |
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