golang自动配置和依赖注入的CLI应用框架插件库hiboot cli的使用
Golang自动配置和依赖注入的CLI应用框架插件库hiboot cli的使用
Hiboot cli应用是基于cobra构建的,同时集成了Hiboot依赖注入和自动配置功能。
完整示例Demo
下面是一个使用hiboot cli创建命令行应用的完整示例:
package main
import (
"fmt"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/cli"
"github.com/hidevopsio/hiboot/pkg/starter/cobra"
)
// 定义一个命令
type helloCommand struct {
// 嵌入cli.Command表示这是一个命令
cli.Command
// 通过tag注入参数
Name string `flag:"name" default:"world" usage:"your name"`
}
// 初始化命令
func init() {
// 注册命令到应用
app.Register(newHelloCommand)
}
// 构造函数
func newHelloCommand() *helloCommand {
cmd := &helloCommand{}
// 设置命令使用说明
cmd.Use = "hello"
cmd.Short = "say hello"
cmd.Long = "This command will print hello message"
// 设置命令执行函数
cmd.Run = func(cmd *cobra.Command, args []string) {
fmt.Printf("Hello, %s!\n", cmd.Name)
}
return cmd
}
// 主函数
func main() {
// 创建cli应用
cliApp := cli.NewApplication("demo").
// 设置简短描述
SetDescription("A demo cli application").
// 设置版本
SetVersion("1.0.0").
// 添加cobra starter
AddStarter(cobra.NewStarter())
// 运行应用
cliApp.Run()
}
代码说明
-
命令定义:
helloCommand
结构体定义了一个命令,嵌入了cli.Command
- 通过tag
flag:"name"
定义了一个可选参数
-
命令注册:
- 在
init()
函数中通过app.Register()
注册命令
- 在
-
命令配置:
Use
- 命令名称Short
- 简短描述Long
- 详细描述Run
- 命令执行函数
-
应用配置:
cli.NewApplication()
创建CLI应用SetDescription()
设置应用描述SetVersion()
设置版本号AddStarter()
添加cobra starter
使用方式
编译后可以通过以下方式使用:
# 查看帮助
./demo --help
# 运行hello命令
./demo hello
# 带参数运行
./demo hello --name=hiboot
特性
-
依赖注入:
- 自动注入命令依赖
- 通过tag注入参数
-
自动配置:
- 自动配置命令和参数
- 自动生成帮助信息
-
插件化:
- 通过starter机制支持插件
- 可扩展性强
这个示例展示了hiboot cli的基本用法,包括命令定义、参数注入和自动配置等功能。
更多关于golang自动配置和依赖注入的CLI应用框架插件库hiboot cli的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang自动配置和依赖注入的CLI应用框架插件库hiboot cli的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用Hiboot CLI构建Golang自动配置和依赖注入的CLI应用
Hiboot是一个基于Go的依赖注入和自动配置框架,其CLI模块(hiboot-cli)专门用于构建命令行应用程序。下面我将详细介绍如何使用Hiboot CLI来开发具有自动配置和依赖注入功能的CLI应用。
安装Hiboot CLI
首先需要安装Hiboot CLI:
go get github.com/hidevopsio/hiboot
go get github.com/hidevopsio/hiboot/pkg/app/cli
基本使用示例
1. 创建简单CLI应用
package main
import (
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/app/cli"
"github.com/hidevopsio/hiboot/pkg/log"
)
// 定义根命令
type rootCommand struct {
cli.RootCommand
}
func newRootCommand() *rootCommand {
return &rootCommand{
RootCommand: cli.RootCommand{
Use: "myapp",
Short: "A simple CLI application",
},
}
}
// 定义子命令
type helloCommand struct {
cli.SubCommand
Name string `flag:"name" default:"world" usage:"your name"`
}
func newHelloCommand() *helloCommand {
return &helloCommand{
SubCommand: cli.SubCommand{
Use: "hello",
Short: "Say hello",
},
}
}
// 命令执行逻辑
func (c *helloCommand) Run() error {
log.Printf("Hello, %s!", c.Name)
return nil
}
func main() {
// 创建应用
app.NewApp().
// 设置CLI模式
SetProperty(app.Profile, cli.Profile).
// 注册命令
Register(newRootCommand).
Register(newHelloCommand).
// 运行应用
Run()
}
2. 运行应用
编译后运行:
./myapp hello --name=John
# 输出: Hello, John!
核心功能
1. 自动配置
Hiboot支持自动配置,只需定义配置结构体并添加@configuration
注解:
package config
import "github.com/hidevopsio/hiboot/pkg/at"
type DatabaseConfig struct {
at.Configuration
Host string `value:"${db.host}" default:"localhost"`
Port int `value:"${db.port}" default:"3306"`
Username string `value:"${db.username}" default:"root"`
Password string `value:"${db.password}"`
}
2. 依赖注入
Hiboot支持构造函数注入和属性注入:
// 服务定义
type DatabaseService struct {
Config *DatabaseConfig
}
func NewDatabaseService(config *DatabaseConfig) *DatabaseService {
return &DatabaseService{Config: config}
}
// 命令中使用服务
type queryCommand struct {
cli.SubCommand
DatabaseService *DatabaseService `inject:""`
}
func (c *queryCommand) Run() error {
log.Printf("Connecting to %s:%d",
c.DatabaseService.Config.Host,
c.DatabaseService.Config.Port)
return nil
}
3. 配置文件支持
创建application.yml
:
db:
host: db.example.com
port: 5432
username: admin
password: secret
4. 高级命令配置
type complexCommand struct {
cli.SubCommand
// 必填参数
InputFile string `arg:"input" validate:"required"`
// 可选参数
OutputFile string `arg:"output"`
// 标志参数
Verbose bool `flag:"v" default:"false" usage:"verbose output"`
// 选择参数
Mode string `flag:"mode" default:"fast" options:"fast,normal,slow"`
}
func (c *complexCommand) Run() error {
if c.Verbose {
log.Printf("Processing %s -> %s in %s mode",
c.InputFile, c.OutputFile, c.Mode)
}
// 业务逻辑...
return nil
}
最佳实践
- 模块化设计:将不同功能拆分为独立的命令和子命令
- 配置分离:将配置参数放在YAML文件中
- 依赖注入:利用DI管理服务依赖
- 验证输入:使用
validate
标签确保参数有效性 - 日志记录:使用Hiboot内置的日志系统
总结
Hiboot CLI提供了一种优雅的方式来构建复杂的命令行应用程序,通过其自动配置和依赖注入功能,开发者可以专注于业务逻辑而不是基础设施代码。它的设计理念类似于Spring Boot,但专为Go语言优化,非常适合构建企业级CLI工具。
要了解更多高级功能,可以参考Hiboot的官方文档和示例代码库。