crud

package
v0.0.0-...-49cc056 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "crud",
	Short: "生成模块的crud功能",
	Long: `
字段设定格式: 字段注释(:字典备注:组件名称)
如: 
状态 -  仅解析名称, 组件根据类型或字段后缀推导
状态:-1=禁用,0=待审核,1=正常 - 解析名称并设置下拉
状态:-1=禁用,0=待审核,1=正常:el-checkbox 
`,
	Run: func(cmd *cobra.Command, args []string) {
		logger.GetDefault().SetReportCaller(false)
		InitDB()
		if !IsDebug() {
			logger.Print("非Debug模式,不支持 crud 命令")
			return
		}
		table, _ := cmd.Flags().GetString("table")
		force, _ := cmd.Flags().GetBool("force")
		onlyInfo, _ := cmd.Flags().GetBool("info")
		frontendPath, _ := cmd.Flags().GetString("path")
		if len(table) == 0 {
			_ = cmd.Help()
			return
		}
		metas, err := Orm().DBMetas()
		if err != nil {
			pine.Logger().Error(err)
			return
		}
		var tableMata *schemas.Table
		for _, meta := range metas {
			if meta.Name == getTableName(table) {
				tableMata = meta
				break
			}
		}
		if tableMata == nil {
			logger.Errorf("无法获取数据表[%s]元信息", getTableName(table))
			return
		}
		for _, v := range tableMata.Columns() {
			cols[v.Name] = v
		}
		controllerName, controllerPath := getControllerName(table)
		tablePath := tableDir + table + goExt
		if !force {
			for _, s := range []string{frontendPath + "/" + feModuleDir + table, controllerPath, tablePath} {
				if _, err := os.Stat(s); !os.IsNotExist(err) {
					logger.Print("已有存在: " + s)
					return
				}
			}
		}

		helper.PanicErr(genTableFileAndFrontendFile(onlyInfo, table, tableDir+table+goExt, frontendPath))
		helper.PanicErr(genControllerFile(onlyInfo, controllerName, table, controllerPath))

		byts, err := os.ReadFile(routerFile)
		helper.PanicErr(err)
		controllerNamespace := `"github.com/xiusin/pinecms/src/application/controllers/backend"`
		pineNamespace := `"github.com/xiusin/pine"`
		holder := "// holder"
		if !bytes.Contains(byts, []byte(controllerNamespace)) {
			byts = bytes.Replace(byts, []byte(pineNamespace), []byte(pineNamespace+"\r\n\t"+controllerNamespace), 1)
		}
		byts = bytes.Replace(byts, []byte(holder), []byte(holder+"\r\n\t"+`backendRouter.Handle(new(backend.`+controllerName+`), "/`+util.SnakeString(table)+`")`), 1)
		os.WriteFile(routerFile, byts, os.ModePerm)
		logger.Print("创建模块文件成功, 已注册路由信息至: " + routerFile)
	},
}

Functions

func FormatEnum

func FormatEnum(field string, opts []map[string]interface{}, item map[string]interface{})

func FormatSet

func FormatSet(field string, opts []map[string]interface{}, item map[string]interface{})

func JSONMarshal

func JSONMarshal(t interface{}) ([]byte, error)

JSONMarshal 不转义字符串编码

Types

type SQLColumn

type SQLColumn struct {
	Name, Type    string
	IsPrimaryKey  bool
	IsUnique      bool
	Length        string
	EnumValues    []string
	AutoIncrement bool
	NotNull       bool
	Default       string
}

SQLColumn 描述结构体

type SQLTable

type SQLTable struct {
	Name string
	Cols []SQLColumn
}

SQLTable 表名结构体

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL