|
1 | 1 | package console |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/gohade/hade/app/console/command/demo" |
4 | 5 | "github.com/gohade/hade/framework" |
5 | 6 | "github.com/gohade/hade/framework/cobra" |
6 | | - hadeCommand "github.com/gohade/hade/framework/command" |
7 | | - commandUtil "github.com/gohade/hade/framework/command/util" |
| 7 | + "github.com/gohade/hade/framework/command" |
8 | 8 | ) |
9 | 9 |
|
10 | | -// RunCommand is command |
| 10 | +// RunCommand 初始化根Command并运行 |
11 | 11 | func RunCommand(container framework.Container) error { |
| 12 | + // 根Command |
12 | 13 | var rootCmd = &cobra.Command{ |
13 | | - Use: "hade", |
14 | | - Short: "main", |
15 | | - Long: "hade commands", |
| 14 | + // 定义根命令的关键字 |
| 15 | + Use: "hade", |
| 16 | + // 简短介绍 |
| 17 | + Short: "hade 命令", |
| 18 | + // 根命令的详细介绍 |
| 19 | + Long: "hade 框架提供的命令行工具,使用这个命令行工具能很方便执行框架自带命令,也能很方便编写业务命令", |
| 20 | + // 根命令的执行函数 |
| 21 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 22 | + cmd.InitDefaultHelpFlag() |
| 23 | + return cmd.Help() |
| 24 | + }, |
| 25 | + // 不需要出现cobra默认的completion子命令 |
| 26 | + CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true}, |
16 | 27 | } |
17 | 28 |
|
18 | | - ctx := commandUtil.RegiestContainer(container, rootCmd) |
| 29 | + // 为根Command设置服务容器 |
| 30 | + rootCmd.SetContainer(container) |
| 31 | + // 绑定框架的命令 |
| 32 | + command.AddKernelCommands(rootCmd) |
| 33 | + // 绑定业务的命令 |
| 34 | + AddAppCommand(rootCmd) |
19 | 35 |
|
20 | | - hadeCommand.AddKernelCommands(rootCmd) |
| 36 | + // 执行RootCommand |
| 37 | + return rootCmd.Execute() |
| 38 | +} |
| 39 | + |
| 40 | +// 绑定业务的命令 |
| 41 | +func AddAppCommand(rootCmd *cobra.Command) { |
| 42 | + rootCmd.AddCommand(demo.FooCommand) |
21 | 43 |
|
22 | | - // rootCmd.AddCronCommand("* * * * *", command.DemoCommand) |
| 44 | + // 每秒调用一次Foo命令 |
| 45 | + //rootCmd.AddCronCommand("* * * * * *", demo.FooCommand) |
23 | 46 |
|
24 | | - return rootCmd.ExecuteContext(ctx) |
| 47 | + // 启动一个分布式任务调度,调度的服务名称为init_func_for_test,每个节点每5s调用一次Foo命令,抢占到了调度任务的节点将抢占锁持续挂载2s才释放 |
| 48 | + //rootCmd.AddDistributedCronCommand("foo_func_for_test", "*/5 * * * * *", demo.FooCommand, 2*time.Second) |
25 | 49 | } |
0 commit comments