GoWindGoWind
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
  • Introduction

    • GoWind Framework
    • Framework Architecture
  • go-wind Core

    • go-wind Core Framework
    • App Lifecycle Management
    • Context Propagation
    • Transport Abstraction
    • Log Facade Interface
  • go-wind-plugins

    • go-wind-plugins Plugin Overview
    • Plugin Configuration System
    • Plugin Registration Mechanism (SPI)
    • Logging Adapter Plugins
    • Transport Protocol Plugins
    • Message Middleware Plugins
    • Encoding Plugins
    • Security and Authentication Plugins
    • Distributed Tracing Plugins
    • Cache Plugins
    • Object Storage Plugins (OSS)
    • Rate Limiting Plugins
    • Metrics Plugins
    • AI Plugins
    • Workflow Plugins
    • Database and Cache Plugins
    • Bootstrap Integration and Gateway Plugins
    • Compression and Serialization Plugins
    • Template Rendering and Validation Plugins
  • go-wind-bootstrap

    • go-wind-bootstrap: The Declarative Bootstrap
    • The Bootstrap Configuration System
    • The Bootstrap SPI Mechanism
    • Declarative Middleware Orchestration
    • Bootstrap Command-Line Integration
    • Bootstrap Practical Examples
  • go-wind-flow Workflow Engine

    • GoWind Flow Process Engine
  • Tutorials

    • Quick Start Tutorial
    • Custom Plugin Development Tutorial
    • Multi-Protocol Listening Tutorial
    • Framework Migration Guide

Bootstrap Command-Line Integration

go-wind-bootstrap ships with built-in Cobra command-line support: the application itself is a Cobra root command that specifies the configuration file via the -c flag — out of the box.

1. One-Line Cobra Integration

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 creates the Cobra root command, parses flags, loads the configuration, and starts the application — internally equivalent to RunApp(flags.Conf).

2. Command-Line Flags

FlagDescription
-c, --confConfiguration file path (yaml/json/bin), default config.yaml
# Use the default config.yaml
go run main.go

# Specify a configuration file
go run main.go -c /path/to/config.yaml
go run main.go --conf config.json

The configuration file format is auto-detected by extension: .yaml / .yml → YAML, .json → JSON, .bin / .pb → Protobuf binary.

3. Extending the Root Command

RunAppWithFlags accepts an optional command customization function for adding subcommands, extra flags, or modifying command metadata:

flags := bootstrap.NewCommandFlags()

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

    // Add a custom subcommand
    root.AddCommand(&cobra.Command{
        Use:   "migrate",
        Short: "Run database migrations",
        RunE: func(cmd *cobra.Command, args []string) error {
            // Migration logic (flags.Conf can be reused to read the same configuration)
            return nil
        },
    })
}); err != nil {
    os.Exit(1)
}

For finer-grained control, use the lower-level building blocks directly:

// CommandFlags: the CLI flag set (Conf holds the configuration path)
flags := bootstrap.NewCommandFlags()

// NewRootCmd: creates the standard root command; runE is called after flags are parsed
root := bootstrap.NewRootCmd(flags, func(cmd *cobra.Command, args []string) error {
    return bootstrap.RunApp(flags.Conf)
})

// AddFlags: binds -c/--conf to your own cobra command
flags.AddFlags(myCmd)

return root.Execute()

4. Specifying Configuration Programmatically

When not going through the command line, call the sealed entry points directly:

// One-line startup: load configuration + signal listening + run + cleanup
if err := bootstrap.RunApp("config.yaml"); err != nil {
    log.Fatal(err)
}

// Load the configuration object first, process it yourself, then run
cfg, err := bootstrap.LoadConfigFromFile("config.yaml")
if err != nil {
    log.Fatal(err)
}
if err := bootstrap.Run(context.Background(), cfg); err != nil {
    log.Fatal(err)
}

Related Documentation

  • Bootstrap Introduction
  • The Bootstrap Configuration System
  • Bootstrap Practical Examples
  • Quick Start Tutorial
Edit this page
Last Updated:: 11/3/26, 2:14 PM
Contributors: Bobo
Prev
Declarative Middleware Orchestration
Next
Bootstrap Practical Examples