feat: add cmd wire all

This commit is contained in:
chris
2023-08-15 23:01:27 +08:00
parent 53fa1f1f15
commit ee7ac0e071
8 changed files with 76 additions and 143 deletions

View File

@@ -3,12 +3,12 @@ package nunu
import (
"fmt"
"github.com/go-nunu/nunu/config"
"github.com/go-nunu/nunu/internal/command/wire"
"github.com/go-nunu/nunu/internal/command/create"
"github.com/go-nunu/nunu/internal/command/project"
"github.com/go-nunu/nunu/internal/command/run"
"github.com/go-nunu/nunu/internal/command/upgrade"
"github.com/go-nunu/nunu/internal/command/wire"
"github.com/spf13/cobra"
)
@@ -22,14 +22,17 @@ var CmdRoot = &cobra.Command{
func init() {
CmdRoot.AddCommand(project.CmdNew)
CmdRoot.AddCommand(create.CmdCreate)
CmdRoot.AddCommand(wire.CmdWire)
CmdRoot.AddCommand(run.CmdRun)
CmdRoot.AddCommand(upgrade.CmdUpgrade)
create.CmdCreate.AddCommand(create.CmdCreateHandler)
create.CmdCreate.AddCommand(create.CmdCreateService)
create.CmdCreate.AddCommand(create.CmdCreateRepository)
create.CmdCreate.AddCommand(create.CmdCreateModel)
create.CmdCreate.AddCommand(create.CmdCreateAll)
CmdRoot.AddCommand(wire.CmdWire)
wire.CmdWire.AddCommand(wire.CmdWireAll)
}
// Execute executes the root command.

View File

@@ -1,7 +1,7 @@
package config
const (
Version = "1.0.5"
Version = "1.0.6"
WireCmd = "github.com/google/wire/cmd/wire@latest"
NunuCmd = "github.com/go-nunu/nunu@latest"
RepoBase = "https://github.com/go-nunu/nunu-layout-base.git"

View File

@@ -67,100 +67,46 @@ After running the above command, Nunu will automatically create a well-structure
│   │   └── wire_gen.go
│   ├── migration
│   │   ├── main.go
│   │   ├── migration.go
│   │   ├── wire.go
│   │   └── wire_gen.go
│   └── server
│   ├── wire
│   ├── main.go
│   ├── wire.go
│   └── wire_gen.go
├── config
│   ├── local.yml
│   └── prod.yml
├── deploy
│   ├── build
│   │   └── Dockerfile
│   ├── docker-compose
│   │   └── docker-compose.yml
│   └── docker-composer
│   └── conf
│   ├── mysql
│   │   └── conf.d
│   └── redis
│   └── cache
│   └── redis.conf
├── internal
│   ├── handler
│   │   ├── handler.go
│   │   └── user.go
│   ├── job
│   │   └── job.go
│   ├── middleware
│   │   ├── cors.go
│   │   ├── jwt.go
│   │   ├── log.go
│   │   └── sign.go
│   ├── migration
│   │   └── migration.go
│   ├── model
│   │   └── user.go
│   ├── pkg
│   │   ├── middleware
│   │   └── request
│   ├── repository
│   │   ├── repository.go
│   │   └── user.go
│   ├── server
│   │   ── http.go
│   │   ── http.go
│   │   └── server.go
│   └── service
│   ├── service.go
│   └── user.go
├── mocks
│   ├── repository
│   │   └── user.go
│   └── service
│   └── user.go
├── pkg
│   ├── config
│   │   └── config.go
│   ├── helper
│   │   ├── convert
│   │   │   └── convert.go
│   │   ├── md5
│   │   │   └── md5.go
│   │   ├── resp
│   │   │   └── resp.go
│   │   ├── sid
│   │   │   └── sid.go
│   │   └── uuid
│   │   └── uuid.go
│   ├── http
│   │   └── http.go
│   └── log
│   └── log.go
├── scripts
│   └── deploy.sh
├── storage
│   └── logs
│   └── server.log
├── test
│   └── server
│   ├── handler
│   │   ├── storage
│   │   │   └── logs
│   │   │   └── server.log
│   │   └── user_test.go
│   ├── repository
│   │   ├── repository_test.go
│   │   └── user_test.go
│   └── service
│   └── user_test.go
├── web
│   └── index.html
├── LICENSE
├── Makefile
├── README.md
├── README_zh.md
├── coverage.html
├── go.mod
└── go.sum
```
## Creating Components
@@ -251,7 +197,7 @@ func newApp(*viper.Viper, *log.Logger) (*gin.Engine, func(), error) {
In Nunu, you can use the following command to compile Wire:
```bash
nunu wire
nunu wire all
```
After running the above command, you will find that the `wire_gen.go` file is generated from the `cmd/server/wire.go` file.

View File

@@ -73,100 +73,46 @@ nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-basic.git
│   │   └── wire_gen.go
│   ├── migration
│   │   ├── main.go
│   │   ├── migration.go
│   │   ├── wire.go
│   │   └── wire_gen.go
│   └── server
│   ├── wire
│   ├── main.go
│   ├── wire.go
│   └── wire_gen.go
├── config
│   ├── local.yml
│   └── prod.yml
├── deploy
│   ├── build
│   │   └── Dockerfile
│   ├── docker-compose
│   │   └── docker-compose.yml
│   └── docker-composer
│   └── conf
│   ├── mysql
│   │   └── conf.d
│   └── redis
│   └── cache
│   └── redis.conf
├── internal
│   ├── handler
│   │   ├── handler.go
│   │   └── user.go
│   ├── job
│   │   └── job.go
│   ├── middleware
│   │   ├── cors.go
│   │   ├── jwt.go
│   │   ├── log.go
│   │   └── sign.go
│   ├── migration
│   │   └── migration.go
│   ├── model
│   │   └── user.go
│   ├── pkg
│   │   ├── middleware
│   │   └── request
│   ├── repository
│   │   ├── repository.go
│   │   └── user.go
│   ├── server
│   │   ── http.go
│   │   ── http.go
│   │   └── server.go
│   └── service
│   ├── service.go
│   └── user.go
├── mocks
│   ├── repository
│   │   └── user.go
│   └── service
│   └── user.go
├── pkg
│   ├── config
│   │   └── config.go
│   ├── helper
│   │   ├── convert
│   │   │   └── convert.go
│   │   ├── md5
│   │   │   └── md5.go
│   │   ├── resp
│   │   │   └── resp.go
│   │   ├── sid
│   │   │   └── sid.go
│   │   └── uuid
│   │   └── uuid.go
│   ├── http
│   │   └── http.go
│   └── log
│   └── log.go
├── scripts
│   └── deploy.sh
├── storage
│   └── logs
│   └── server.log
├── test
│   └── server
│   ├── handler
│   │   ├── storage
│   │   │   └── logs
│   │   │   └── server.log
│   │   └── user_test.go
│   ├── repository
│   │   ├── repository_test.go
│   │   └── user_test.go
│   └── service
│   └── user_test.go
├── web
│   └── index.html
├── LICENSE
├── Makefile
├── README.md
├── README_zh.md
├── coverage.html
├── go.mod
└── go.sum
```
## 创建组件
@@ -257,7 +203,7 @@ func newApp(*viper.Viper, *log.Logger) (*gin.Engine, func(), error) {
在Nunu中可以使用以下命令编译Wire
```bash
nunu wire
nunu wire all
```
执行完上述命令后,我们选择`cmd/server/wire.go`文件,生成对应的`wire_gen.go`文件。

View File

@@ -38,7 +38,7 @@ var CmdWire = &cobra.Command{
}
switch len(wirePath) {
case 0:
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The cmd directory cannot be found in the current directory")
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The wire.go cannot be found in the current directory")
return
case 1:
for _, v := range wirePath {
@@ -65,6 +65,43 @@ var CmdWire = &cobra.Command{
},
}
var CmdWireAll = &cobra.Command{
Use: "all",
Short: "nunu wire all",
Long: "nunu wire all",
Example: "nunu wire all",
Run: func(cmd *cobra.Command, args []string) {
cmdArgs, _ := helper.SplitArgs(cmd, args)
var dir string
if len(cmdArgs) > 0 {
dir = cmdArgs[0]
}
base, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
}
if dir == "" {
// find the directory containing the cmd/*
wirePath, err := findWire(base)
if err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
}
switch len(wirePath) {
case 0:
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The wire.go cannot be found in the current directory")
return
default:
for _, v := range wirePath {
wire(v)
}
}
}
},
}
func wire(wirePath string) {
fmt.Println("wire.go path: ", wirePath)

View File

@@ -13,11 +13,6 @@ type {{ .FileName }}Handler interface {
Update{{ .FileName }}(ctx *gin.Context)
}
type {{ .FileNameTitleLower }}Handler struct {
*Handler
{{ .FileNameTitleLower }}Service service.{{ .FileName }}Service
}
func New{{ .FileName }}Handler(handler *Handler, {{ .FileNameTitleLower }}Service service.{{ .FileName }}Service) {{ .FileName }}Handler {
return &{{ .FileNameTitleLower }}Handler{
Handler: handler,
@@ -25,6 +20,11 @@ func New{{ .FileName }}Handler(handler *Handler, {{ .FileNameTitleLower }}Servic
}
}
type {{ .FileNameTitleLower }}Handler struct {
*Handler
{{ .FileNameTitleLower }}Service service.{{ .FileName }}Service
}
func (h *{{ .FileNameTitleLower }}Handler) Get{{ .FileName }}ById(ctx *gin.Context) {
var params struct {
Id int64 `form:"id" binding:"required"`

View File

@@ -7,9 +7,6 @@ import (
type {{ .FileName }}Repository interface {
FirstById(id int64) (*model.{{ .FileName }}, error)
}
type {{ .FileNameTitleLower }}Repository struct {
*Repository
}
func New{{ .FileName }}Repository(repository *Repository) {{ .FileName }}Repository {
return &{{ .FileNameTitleLower }}Repository{
@@ -17,6 +14,10 @@ func New{{ .FileName }}Repository(repository *Repository) {{ .FileName }}Reposit
}
}
type {{ .FileNameTitleLower }}Repository struct {
*Repository
}
func (r *{{ .FileNameTitleLower }}Repository) FirstById(id int64) (*model.{{ .FileName }}, error) {
var {{ .FileNameTitleLower }} model.{{ .FileName }}
// TODO: query db

View File

@@ -9,11 +9,6 @@ type {{ .FileName }}Service interface {
Get{{ .FileName }}ById(id int64) (*model.{{ .FileName }}, error)
}
type {{ .FileNameTitleLower }}Service struct {
*Service
{{ .FileNameTitleLower }}Repository repository.{{ .FileName }}Repository
}
func New{{ .FileName }}Service(service *Service, {{ .FileNameTitleLower }}Repository repository.{{ .FileName }}Repository) {{ .FileName }}Service {
return &{{ .FileNameTitleLower }}Service{
Service: service,
@@ -21,6 +16,11 @@ func New{{ .FileName }}Service(service *Service, {{ .FileNameTitleLower }}Reposi
}
}
type {{ .FileNameTitleLower }}Service struct {
*Service
{{ .FileNameTitleLower }}Repository repository.{{ .FileName }}Repository
}
func (s *{{ .FileNameTitleLower }}Service) Get{{ .FileName }}ById(id int64) (*model.{{ .FileName }}, error) {
return s.{{ .FileNameTitleLower }}Repository.FirstById(id)
}