feat:完善活动信息

This commit is contained in:
henry
2021-11-25 09:58:08 +08:00
parent 5475320171
commit 474e33750a
16 changed files with 574 additions and 33 deletions

View File

@ -0,0 +1,73 @@
package controller
import (
"SciencesServer/utils"
"bytes"
"fmt"
"go/format"
"text/template"
)
const ControllerTemplate = `package {{.Name}}
import (
"ArmedPolice/app/controller/basic"
"ArmedPolice/app/service"
)
type {{.StrutName}} struct{ *service.Session }
type {{.StrutName}}Handle func(session *service.Session) *{{.StrutName}}
func (c *{{.StrutName}}) List() (*basic.PageDataResponse, error) {
return &basic.PageDataResponse{Data: nil, Count: 0}, nil
}
func (c *{{.StrutName}}) Form() error {
return nil
}
func (c *{{.StrutName}}) Delete(id uint64) error {
return nil
}
func New{{.StrutName}}() {{.StrutName}}Handle {
return func(session *service.Session) *{{.StrutName}} {
return &{{.StrutName}}{session}
}
}`
type ControllerFile struct {
// Name is the plugin name. Snack style.
Name string
// StrutName is the struct name.
StrutName string
}
func (v *ControllerFile) Execute(file, tmplName, tmpl string) error {
fmt.Println(file)
f, err := utils.Open(file)
if err != nil {
return err
}
defer f.Close()
temp := new(template.Template)
if temp, err = template.New(tmplName).Parse(tmpl); err != nil {
return err
}
buf := new(bytes.Buffer)
if err = temp.Execute(buf, v); err != nil {
return err
}
out := make([]byte, 0)
if out, err = format.Source(buf.Bytes()); err != nil {
return err
}
_, err = f.Write(out)
return err
}

View File

@ -0,0 +1,75 @@
package controller
import (
"SciencesServer/config"
"SciencesServer/serve/logger"
"SciencesServer/utils"
"fmt"
"os"
"path"
"strings"
"github.com/spf13/cobra"
)
var ControllerCommand = &cobra.Command{
Use: "make:controller",
Short: "quick make controller",
Example: "ctl make:controller -f sys_user -t sys_user",
Version: *config.Version,
}
var (
file string
address string
)
func init() {
ControllerCommand.Flags().StringVarP(&file, "file", "f", "", "The file name.")
ControllerCommand.Flags().StringVarP(&address, "address", "a", "", "The file controller address")
ControllerCommand.Run = func(cmd *cobra.Command, args []string) {
utils.TryCatch(func() {
if file == "" {
logger.ErrorF("Filename Not Nil")
return
}
output := "../../app/controller"
if address == "" {
logger.ErrorF("Address Not Nil")
return
}
// 解析文件地址
address = strings.Replace(address, "/", "", -1)
addresss := strings.Split(address, "/")
file := strings.ToLower(file)
dir, err := os.Getwd()
output = path.Join(dir, output+"/"+address)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
if err = utils.PrepareOutput(output); err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
fmt.Println(output)
modelFile := &ControllerFile{
Name: addresss[len(addresss)-1],
StrutName: utils.ToSnake(file, "_"),
}
err = modelFile.Execute(path.Join(output, file+".go"), "controller", ControllerTemplate)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
return
})
}
}

View File

@ -42,7 +42,7 @@ func init() {
dir, err := os.Getwd()
output := "/app/common/model"
output := "../../app/common/model"
output = path.Join(dir, output)

View File

@ -1,9 +1,9 @@
package main
import (
"SciencesServer/cmd/ctl/command/controller"
"SciencesServer/cmd/ctl/command/model"
"SciencesServer/config"
"github.com/spf13/cobra"
)
@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(controller.ControllerCommand)
rootCmd.AddCommand(model.ModelCommand)
}