make_cmd.go 986 B

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