From 365ff66a184121c3dd11cd0359c4a63f0e68a976 Mon Sep 17 00:00:00 2001 From: rizerkrof <34008166+rizerkrof@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:40:55 +0100 Subject: [PATCH 1/3] cli: generate controller --- cmd/fuego/commands/controller.go | 57 +++++++++++++++++++++++++ cmd/fuego/go.mod | 11 +++++ cmd/fuego/go.sum | 8 ++++ cmd/fuego/main.go | 28 ++++++++++++ cmd/fuego/templates/controller.template | 45 +++++++++++++++++++ cmd/fuego/templates/embed.go | 8 ++++ examples/full-app-gourmet/go.mod | 2 +- examples/full-app-gourmet/go.sum | 6 +-- examples/hello-world/go.mod | 2 +- examples/hello-world/go.sum | 3 +- go.mod | 2 +- go.sum | 3 +- go.work | 1 + go.work.sum | 1 + 14 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 cmd/fuego/commands/controller.go create mode 100644 cmd/fuego/go.mod create mode 100644 cmd/fuego/go.sum create mode 100644 cmd/fuego/main.go create mode 100644 cmd/fuego/templates/controller.template create mode 100644 cmd/fuego/templates/embed.go diff --git a/cmd/fuego/commands/controller.go b/cmd/fuego/commands/controller.go new file mode 100644 index 00000000..91793ca0 --- /dev/null +++ b/cmd/fuego/commands/controller.go @@ -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 +} diff --git a/cmd/fuego/go.mod b/cmd/fuego/go.mod new file mode 100644 index 00000000..a2ddeeca --- /dev/null +++ b/cmd/fuego/go.mod @@ -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 +) diff --git a/cmd/fuego/go.sum b/cmd/fuego/go.sum new file mode 100644 index 00000000..31517c04 --- /dev/null +++ b/cmd/fuego/go.sum @@ -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= diff --git a/cmd/fuego/main.go b/cmd/fuego/main.go new file mode 100644 index 00000000..3ee8dd10 --- /dev/null +++ b/cmd/fuego/main.go @@ -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) + } +} diff --git a/cmd/fuego/templates/controller.template b/cmd/fuego/templates/controller.template new file mode 100644 index 00000000..694c3409 --- /dev/null +++ b/cmd/fuego/templates/controller.template @@ -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 + NewControllerEntityRepository NewControllerEntityRepository + 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 +} diff --git a/cmd/fuego/templates/embed.go b/cmd/fuego/templates/embed.go new file mode 100644 index 00000000..c7cbf56e --- /dev/null +++ b/cmd/fuego/templates/embed.go @@ -0,0 +1,8 @@ +package templates + +import ( + "embed" +) + +//go:embed *.template +var FS embed.FS diff --git a/examples/full-app-gourmet/go.mod b/examples/full-app-gourmet/go.mod index b8aae71c..fc06d59d 100644 --- a/examples/full-app-gourmet/go.mod +++ b/examples/full-app-gourmet/go.mod @@ -53,7 +53,7 @@ require ( golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect - golang.org/x/tools v0.16.1 // indirect + golang.org/x/tools v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/uint128 v1.3.0 // indirect modernc.org/cc/v3 v3.41.0 // indirect diff --git a/examples/full-app-gourmet/go.sum b/examples/full-app-gourmet/go.sum index 247cbc1a..17bd376f 100644 --- a/examples/full-app-gourmet/go.sum +++ b/examples/full-app-gourmet/go.sum @@ -122,8 +122,7 @@ golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -145,8 +144,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= -golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/examples/hello-world/go.mod b/examples/hello-world/go.mod index 8bfe128a..2f28ef5d 100644 --- a/examples/hello-world/go.mod +++ b/examples/hello-world/go.mod @@ -31,6 +31,6 @@ require ( golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/tools v0.16.1 // indirect + golang.org/x/tools v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/examples/hello-world/go.sum b/examples/hello-world/go.sum index 045aee28..9c1110b8 100644 --- a/examples/hello-world/go.sum +++ b/examples/hello-world/go.sum @@ -105,8 +105,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= -golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/go.mod b/go.mod index 3e2d1426..3751a64f 100644 --- a/go.mod +++ b/go.mod @@ -36,6 +36,6 @@ require ( golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/tools v0.16.1 // indirect + golang.org/x/tools v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fd897746..35110954 100644 --- a/go.sum +++ b/go.sum @@ -105,8 +105,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= -golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/go.work b/go.work index 25f78544..8e2aaeb1 100644 --- a/go.work +++ b/go.work @@ -2,6 +2,7 @@ go 1.22.0 use ( . + ./cmd/fuego ./examples/basic ./examples/full-app-gourmet ./examples/hello-world diff --git a/go.work.sum b/go.work.sum index 5048b726..67b2bf12 100644 --- a/go.work.sum +++ b/go.work.sum @@ -23,6 +23,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= From a4a610bbb8e51d00f34ca926060f4475a3494662 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Fri, 9 Feb 2024 10:19:05 +0100 Subject: [PATCH 2/3] cli: Improved controller generation --- cmd/fuego/commands/controller.go | 23 ++++-- cmd/fuego/commands/controller_test.go | 12 +++ cmd/fuego/main.go | 6 +- cmd/fuego/templates/controller.template | 45 ----------- cmd/fuego/templates/controller/controller.go | 82 ++++++++++++++++++++ cmd/fuego/templates/embed.go | 2 +- 6 files changed, 114 insertions(+), 56 deletions(-) create mode 100644 cmd/fuego/commands/controller_test.go delete mode 100644 cmd/fuego/templates/controller.template create mode 100644 cmd/fuego/templates/controller/controller.go diff --git a/cmd/fuego/commands/controller.go b/cmd/fuego/commands/controller.go index 91793ca0..f32fbe10 100644 --- a/cmd/fuego/commands/controller.go +++ b/cmd/fuego/commands/controller.go @@ -7,19 +7,21 @@ import ( "github.com/go-fuego/fuego/cmd/fuego/templates" "github.com/urfave/cli/v2" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) -func ControllerCommand() *cli.Command { +func Controller() *cli.Command { return &cli.Command{ Name: "controller", - Usage: "add a new template", + Usage: "creates a new controller file", 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") + fmt.Println("Note: You can add a controller name as an argument. Example: `fuego controller books`") } err := createController(controllerName) @@ -33,22 +35,29 @@ func ControllerCommand() *cli.Command { } } +// createController creates a new controller file func createController(controllerName string) error { controllerDir := "./controllers/" if _, err := os.Stat(controllerDir); os.IsNotExist(err) { - err = os.Mkdir(controllerDir, 0755) + err = os.Mkdir(controllerDir, 0o755) if err != nil { return err } } - templateContent, err := templates.FS.ReadFile("controller.template") + 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", strings.Title(controllerName)) + newContent = strings.ReplaceAll(newContent, "NewController", titler.String(controllerName)) controllerPath := fmt.Sprintf("%s%s.go", controllerDir, controllerName) - err = os.WriteFile(controllerPath, []byte(newContent), 0644) + err = os.WriteFile(controllerPath, []byte(newContent), 0o644) if err != nil { return err } diff --git a/cmd/fuego/commands/controller_test.go b/cmd/fuego/commands/controller_test.go new file mode 100644 index 00000000..475d0e74 --- /dev/null +++ b/cmd/fuego/commands/controller_test.go @@ -0,0 +1,12 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCreateController(t *testing.T) { + err := createController("books") + require.NoError(t, err) +} diff --git a/cmd/fuego/main.go b/cmd/fuego/main.go index 3ee8dd10..0644b84d 100644 --- a/cmd/fuego/main.go +++ b/cmd/fuego/main.go @@ -12,13 +12,13 @@ import ( func main() { app := &cli.App{ Name: "fuego", - Usage: "Fire like fuego!", - Action: func(*cli.Context) error { + Usage: "The framework for busy Go developers", + Action: func(c *cli.Context) error { fmt.Println("The 🔥 CLI!") return nil }, Commands: []*cli.Command{ - commands.ControllerCommand(), + commands.Controller(), }, } diff --git a/cmd/fuego/templates/controller.template b/cmd/fuego/templates/controller.template deleted file mode 100644 index 694c3409..00000000 --- a/cmd/fuego/templates/controller.template +++ /dev/null @@ -1,45 +0,0 @@ -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 - NewControllerEntityRepository NewControllerEntityRepository - 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 -} diff --git a/cmd/fuego/templates/controller/controller.go b/cmd/fuego/templates/controller/controller.go new file mode 100644 index 00000000..20d0843c --- /dev/null +++ b/cmd/fuego/templates/controller/controller.go @@ -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) +} diff --git a/cmd/fuego/templates/embed.go b/cmd/fuego/templates/embed.go index c7cbf56e..588fa041 100644 --- a/cmd/fuego/templates/embed.go +++ b/cmd/fuego/templates/embed.go @@ -4,5 +4,5 @@ import ( "embed" ) -//go:embed *.template +//go:embed */*.go var FS embed.FS From ae6dd587b1680746f752e0f635aff5ac5e376563 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Fri, 9 Feb 2024 10:33:18 +0100 Subject: [PATCH 3/3] cli: choose controller output + tests --- cmd/fuego/commands/controller.go | 33 ++++++++++++++++++--------- cmd/fuego/commands/controller_test.go | 5 +++- 2 files changed, 26 insertions(+), 12 deletions(-) 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)`) }