Skip to content

Commit

Permalink
纯工具模式完成
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbihao committed Jul 21, 2024
1 parent f37f691 commit f7c46e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@

具备 Web 框架的基础能力

- go 1.21+

- gin v1.9.1
- middleware
- gorm v1.25.9
- swagger
- vue
- vue3
- ,,,

## 服务提供者

Expand Down
33 changes: 33 additions & 0 deletions app/console/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@ package console

import (
"github.com/chenbihao/gob/app/console/command/demo"
"github.com/chenbihao/gob/framework"
"github.com/chenbihao/gob/framework/cobra"
"github.com/chenbihao/gob/framework/command"
)

// RunRootCommand 初始化根Command并运行
func RunRootCommand(container framework.Container) error {
// 根Command
var rootCmd = &cobra.Command{
// 定义根命令的关键字
Use: "gob",
// 简短介绍
Short: "gob 命令",
// 根命令的详细介绍
Long: "gob 框架提供的命令行工具,使用这个命令行工具能很方便执行框架自带命令,也能很方便编写业务命令",
// 根命令的执行函数
RunE: func(cmd *cobra.Command, args []string) error {
cmd.InitDefaultHelpFlag()
return cmd.Help()
},
// 不需要出现cobra默认的completion子命令
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}

// 为根Command设置服务容器
rootCmd.SetContainer(container)

// 绑定框架的命令
command.AddKernelCommands(rootCmd)
// 绑定业务的命令
AddAppCommand(rootCmd)

// 执行RootCommand
return rootCmd.Execute()
}

// 绑定业务的命令(业务定义的命令我们使用 `app/console/kernel.go` 中的 `AddAppCommand`进行挂载)
func AddAppCommand(rootCmd *cobra.Command) {
// demo 例子
Expand Down
2 changes: 1 addition & 1 deletion app/http/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewHttpEngine(container framework.Container) (*gin.Engine, error) {
// 默认启动一个 Web 引擎 (Default 包含 Logger and Recovery)
r := gin.Default()

// 设置容器
// 设置框架容器
r.SetContainer(container)

// 业务绑定路由操作
Expand Down
46 changes: 9 additions & 37 deletions framework/command/kernel.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,29 @@
package command

import (
"github.com/chenbihao/gob/app/console"
"github.com/chenbihao/gob/framework"
"github.com/chenbihao/gob/framework/cobra"
"github.com/chenbihao/gob/framework/command/model"
"github.com/chenbihao/gob/framework/contract"
"github.com/robfig/cron/v3"
)

// RunRootCommand 初始化根Command并运行
func RunRootCommand(container framework.Container) error {
// 根Command
var rootCmd = &cobra.Command{
// 定义根命令的关键字
Use: "gob",
// 简短介绍
Short: "gob 命令",
// 根命令的详细介绍
Long: "gob 框架提供的命令行工具,使用这个命令行工具能很方便执行框架自带命令,也能很方便编写业务命令",
// 根命令的执行函数
RunE: func(cmd *cobra.Command, args []string) error {
cmd.InitDefaultHelpFlag()
return cmd.Help()
},
// 不需要出现cobra默认的completion子命令
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}

// 为根Command设置服务容器
rootCmd.SetContainer(container)

// 绑定框架的命令
if container.MustMake(contract.AppKey).(contract.App).IsToolMode() {
AddKernelToolCommands(rootCmd)
// AddKernelCommands will add all command/* to root command
func AddKernelCommands(root *cobra.Command) {
if root.GetContainer().MustMake(contract.AppKey).(contract.App).IsToolMode() {
AddKernelOnlyToolCommands(root)
} else {
AddKernelCommands(rootCmd)
// 绑定业务的命令
console.AddAppCommand(rootCmd)
AddKernelAllCommands(root)
}

// 执行RootCommand
return rootCmd.Execute()
}

// AddKernelToolCommands will add all command/* to root command
func AddKernelToolCommands(root *cobra.Command) {
// AddKernelToolCommands 添加 纯工具模式支持的命令到 root (go install 时运行的全局工具命令,尚未适配)
func AddKernelOnlyToolCommands(root *cobra.Command) {
root.AddCommand(initNewCommand()) // 挂载 new 命令
root.AddCommand(initVersionCommand()) // 挂载 version 命令
}

// AddKernelCommands will add all command/* to root command
func AddKernelCommands(root *cobra.Command) {
// AddKernelToolCommands 添加 command/* root
func AddKernelAllCommands(root *cobra.Command) {
InitCronCommands(root)
root.AddCommand(initAppCommand()) // 挂载 app 命令
root.AddCommand(initEnvCommand()) // 挂载 env 命令
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/chenbihao/gob/app/console"
"github.com/chenbihao/gob/app/http"
"github.com/chenbihao/gob/framework"
"github.com/chenbihao/gob/framework/command"
"github.com/chenbihao/gob/framework/provider/app"
"github.com/chenbihao/gob/framework/provider/cache"
"github.com/chenbihao/gob/framework/provider/config"
Expand Down Expand Up @@ -42,5 +42,5 @@ func main() {
_ = container.Bind(&kernel.KernelProvider{HttpEngine: engine})
}
// 运行root命令
_ = command.RunRootCommand(container)
_ = console.RunRootCommand(container)
}

0 comments on commit f7c46e6

Please sign in to comment.