diff --git a/cmd/fuego/commands/controller.go b/cmd/fuego/commands/controller.go index f32fbe10..54ae9a2e 100644 --- a/cmd/fuego/commands/controller.go +++ b/cmd/fuego/commands/controller.go @@ -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`") } - 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) 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)) - 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 } diff --git a/cmd/fuego/commands/controller_test.go b/cmd/fuego/commands/controller_test.go index 475d0e74..077f30c2 100644 --- a/cmd/fuego/commands/controller_test.go +++ b/cmd/fuego/commands/controller_test.go @@ -7,6 +7,9 @@ import ( ) func TestCreateController(t *testing.T) { - err := createController("books") + res, err := createController("books", "/dev/null") 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)`) }