package make import ( "fmt" "github.com/runningwater/gohub/pkg/console" "github.com/spf13/cobra" ) var CmdMakeCMD = &cobra.Command{ Use: "cmd", Short: "Create a command, should be snake case, like: make cmd test_cmd", Run: runMakeCMD, Args: cobra.ExactArgs(1), // 只允许且必须传 1 个参数 } // runMakeCMD 运行命令行 // 该函数会被 cobra 自动调用 func runMakeCMD(cmd *cobra.Command, args []string) { // 格式化模型名称,返回一个 Model 实例 model := makeModelFromString(args[0]) filePath := fmt.Sprintf("app/cmd/%s.go", model.PackageName) // 从模板中创建文件 createFileFromTpl(filePath, "cmd", model) // 输出成功信息 console.Success(fmt.Sprintf("Create %s success", filePath)) console.Info(fmt.Sprintf("command name: %s", model.PackageName)) console.Info(fmt.Sprintf("command variable name: cmd.Cmd %s", model.StructName)) console.Warning("Please edit main.go's app.Commands slice to register this command") }