liveBook · Manning

Cobra

As explained in Build and Orchestrator in Go (From Scratch)

Cobra is a Go library and framework for building command-line applications with commands, arguments, flags, and handlers.

Cobra is a Go framework for building command-line applications from standardized components. A CLI parses commands, arguments, and flags, invokes a matching handler, and presents its output. Cobra packages those responsibilities so applications need not implement flag handling and command plumbing from scratch. section 12.2

A Cobra application is organized as a command tree. The root command is a *cobra.Command; its Use, Short, and Long fields supply usage and help text, while Execute runs the tree once from main. A command's Run function receives the selected command and argument slice, then performs the requested work. section 12.5 section 12.6

Build a command-oriented application incrementally:

  • Initialize a project with cobra-cli init, which generates main.go plus cmd/root.go. section 12.3
  • Add a command skeleton with cobra-cli add; register it with rootCmd.AddCommand to make it available. section 12.6
  • Define local flags for one command or persistent flags inherited by its children, usually in init. section 12.5
  • Read flag values in Run and use them to configure or start the underlying service. section 12.6

A worker command

A worker command exposes host, port, name, and datastore-type flags, with long and short forms such as --host and -H. Its Run handler reads those values, creates a worker and API, starts task-related processes, and starts the API server. Built-in help displays the command usage and available flags. section 12.6

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
    Use:   "cube",
    Short: "A brief description of your application",
    Long: `A longer description that spans multiple lines and likely
     contains
examples and usage of using your application. For example:
 
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    // Uncomment the following line if your bare application
    // has an action associated with it:
    // Run: func(cmd *cobra.Command, args []string) { },
}

Inspect the generated root command to see the minimal command definition: usage text, short and long descriptions, and an optional Run handler. section 12.5

This structure separates operational roles that were previously bundled into one main program. Independent worker and manager commands replace a monolith, while run, stop, status, and node commands provide direct cluster interaction instead of manual curl calls to a manager API. section 12.0 section 12.12

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Explains generated command structure, help fields, flag scopes, registration, and Run handlers, then implements a worker command.
Examples
Demonstrates cobra-cli initialization, a root command, a worker skeleton, help output, host and port flags, and worker startup.
What it assumes
Assumes a Go project and existing orchestration components while introducing Cobra command structure and configuration from generated code.
What you can do after
Build it from scratch by scaffolding Cobra commands, defining flags, registering children, and implementing Run handlers.
How it connects
Connects command-line interfaces with commands, arguments, flags, handlers, help systems, Go init functions, and pflag.

Also explained in

Compare how 2 books explain Cobra →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.