GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

Bootstrap 命令行集成

go-wind-bootstrap 内置 Cobra 命令行支持:应用本身即是一个 Cobra 根命令,通过 -c 参数指定配置文件,开箱即用。

一、一行接入 Cobra

package main

import (
    "os"

    bootstrap "github.com/tx7do/go-wind-bootstrap"
)

func main() {
    if err := bootstrap.RunAppWithFlags(bootstrap.NewCommandFlags()); err != nil {
        os.Exit(1)
    }
}

RunAppWithFlags 创建 Cobra 根命令,解析 flag 后加载配置并启动应用,内部等价于 RunApp(flags.Conf)。

二、命令行参数

参数说明
-c, --conf配置文件路径(yaml/json/bin),默认 config.yaml
# 使用默认 config.yaml
go run main.go

# 指定配置文件
go run main.go -c /path/to/config.yaml
go run main.go --conf config.json

配置文件格式按扩展名自动识别:.yaml / .yml → YAML,.json → JSON,.bin / .pb → Protobuf 二进制。

三、扩展根命令

RunAppWithFlags 接受可选的命令定制函数,可添加子命令、额外 flag 或修改命令元信息:

flags := bootstrap.NewCommandFlags()

if err := bootstrap.RunAppWithFlags(flags, func(root *cobra.Command) {
    root.Use = "my-service"
    root.Short = "订单服务"

    // 添加自定义子命令
    root.AddCommand(&cobra.Command{
        Use:   "migrate",
        Short: "执行数据库迁移",
        RunE: func(cmd *cobra.Command, args []string) error {
            // 迁移逻辑(可复用 flags.Conf 读取同一份配置)
            return nil
        },
    })
}); err != nil {
    os.Exit(1)
}

需要更细的控制时,可直接使用底层构件:

// CommandFlags:CLI flag 集合(Conf 配置路径)
flags := bootstrap.NewCommandFlags()

// NewRootCmd:创建标准根命令,runE 在 flag 解析完成后调用
root := bootstrap.NewRootCmd(flags, func(cmd *cobra.Command, args []string) error {
    return bootstrap.RunApp(flags.Conf)
})

// AddFlags:把 -c/--conf 绑定到你自己的 cobra 命令
flags.AddFlags(myCmd)

return root.Execute()

四、程序化指定配置

不走命令行时,直接调用密封入口:

// 一行启动:加载配置 + 信号监听 + 运行 + 清理
if err := bootstrap.RunApp("config.yaml"); err != nil {
    log.Fatal(err)
}

// 先加载配置对象,自行加工后再运行
cfg, err := bootstrap.LoadConfigFromFile("config.yaml")
if err != nil {
    log.Fatal(err)
}
if err := bootstrap.Run(context.Background(), cfg); err != nil {
    log.Fatal(err)
}

相关文档

  • Bootstrap 介绍
  • Bootstrap 配置系统
  • Bootstrap 实战示例
  • 快速入门教程
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
声明式中间件编排
Next
Bootstrap 实战示例