cli

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cli provides a public API for extending and customizing the Flow CLI.

This package allows external projects to:

  • Build custom CLIs that include Flow's commands
  • Add cross-cutting hooks (PreRun/PostRun) to commands
  • Override existing commands with custom implementations
  • Add new commands alongside Flow's built-in commands

Basic Usage

Build a custom CLI with all Flow commands:

ctx := context.NewContext(...)
rootCmd := cli.BuildRootCommand(ctx)
cli.RegisterAllCommands(ctx, rootCmd)
if err := cli.Execute(ctx, rootCmd); err != nil {
    log.Fatal(err)
}

Customizing the Root Command

Use functional options to customize the root command:

rootCmd := cli.BuildRootCommand(ctx,
    cli.WithVersion("1.0.0-custom"),
    cli.WithShort("My custom Flow CLI"),
    cli.WithPersistentPreRun(myPreRunHook),
)

Adding Hooks

Add hooks to individual commands or recursively to all commands:

// Add to a single command
cli.AddPreRunHook(cmd, telemetryHook)

// Add to all commands recursively
cli.ApplyHooksRecursive(rootCmd, preHook, postHook)

Command Registry Operations

Manipulate the command tree:

// Find a command
wsCmd := cli.FindCommand(rootCmd, "workspace")

// Replace a command
cli.ReplaceCommand(rootCmd, "exec", customExecCmd)

// Walk all commands
cli.WalkCommands(rootCmd, func(cmd *cobra.Command) {
    // Do something with each command
})

Thread Safety

This package is not thread-safe. Command building and modification should be done during CLI initialization, not concurrently.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPersistentPostRunHook

func AddPersistentPostRunHook(cmd *cobra.Command, hook HookFunc)

AddPersistentPostRunHook adds a PersistentPostRun hook to a command, preserving any existing hook. If the command already has a PersistentPostRun hook, the new hook will run after it.

PersistentPostRun hooks run after all commands in the subtree.

func AddPersistentPreRunHook

func AddPersistentPreRunHook(cmd *cobra.Command, hook HookFunc)

AddPersistentPreRunHook adds a PersistentPreRun hook to a command, preserving any existing hook. If the command already has a PersistentPreRun hook, the new hook will run before it.

PersistentPreRun hooks run before all commands in the subtree.

func AddPostRunHook

func AddPostRunHook(cmd *cobra.Command, hook HookFunc)

AddPostRunHook adds a PostRun hook to a command, preserving any existing PostRun hook. If the command already has a PostRun hook, the new hook will run after it.

func AddPreRunHook

func AddPreRunHook(cmd *cobra.Command, hook HookFunc)

AddPreRunHook adds a PreRun hook to a command, preserving any existing PreRun hook. If the command already has a PreRun hook, the new hook will run before it.

func BuildRootCommand

func BuildRootCommand(ctx *context.Context, opts ...RootOption) *cobra.Command

BuildRootCommand creates a new root command with optional configuration. The root command is the main entry point for the CLI.

By default, this creates a root command with Flow's standard configuration. Use RootOption functions to customize the command.

func Execute

func Execute(ctx *context.Context, rootCmd *cobra.Command) error

Execute runs the root command. This is a convenience wrapper around cobra's Execute.

func FindCommand

func FindCommand(rootCmd *cobra.Command, name string) *cobra.Command

FindCommand finds a command by name in the command tree. It performs a breadth-first search starting from the root.

Returns nil if the command is not found.

func FindCommandPath

func FindCommandPath(rootCmd *cobra.Command, path string) *cobra.Command

FindCommandPath finds a command by its full path (e.g., "workspace add"). The path should be space-separated command names.

Returns nil if the command is not found.

func GetSubcommands

func GetSubcommands(cmd *cobra.Command) map[string]*cobra.Command

GetSubcommands returns a map of subcommand names to commands for a given command. This is a convenience wrapper around cobra's Commands() method.

func ListCommands

func ListCommands(rootCmd *cobra.Command) []string

ListCommands returns a list of all command names in the tree. The list includes the root command and all subcommands.

func RegisterAllCommands

func RegisterAllCommands(ctx *context.Context, rootCmd *cobra.Command)

RegisterAllCommands registers all Flow commands to the root command.

func RemoveCommand

func RemoveCommand(rootCmd *cobra.Command, name string) error

RemoveCommand removes a command by name from the command tree.

Returns an error if the command is not found or if it's the root command.

func ReplaceCommand

func ReplaceCommand(rootCmd *cobra.Command, oldName string, newCmd *cobra.Command) error

ReplaceCommand replaces a command with the given name with a new command. The new command will be added to the same parent as the old command.

Returns an error if the old command is not found or if the replacement fails.

func WalkCommands

func WalkCommands(rootCmd *cobra.Command, fn func(*cobra.Command))

WalkCommands traverses the command tree starting from the root and applies a function to each command (including the root).

The traversal is depth-first, visiting parent commands before their children.

Types

type HookFunc

type HookFunc func(cmd *cobra.Command, args []string)

HookFunc is a function that can be used as a PreRun or PostRun hook for a command. It receives the command being executed and its arguments.

type RootConfig

type RootConfig struct {
	// Use is the one-line usage message (defaults to "flow")
	Use string

	// Short is the short description shown in help (defaults to Flow's standard description)
	Short string

	// Long is the long description shown in help (defaults to Flow's standard description)
	Long string

	// Version is the version string (defaults to Flow's current version)
	Version string
}

RootConfig holds configuration options for building the root command.

type RootOption

type RootOption func(*RootConfig)

RootOption is a functional option for configuring the root command.

func WithLong

func WithLong(long string) RootOption

WithLong sets the Long description for the root command.

func WithShort

func WithShort(short string) RootOption

WithShort sets the Short description for the root command.

func WithUse

func WithUse(use string) RootOption

WithUse sets the Use field for the root command.

func WithVersion

func WithVersion(version string) RootOption

WithVersion sets the Version string for the root command.

Directories

Path Synopsis
Package main demonstrates basic usage of the Flow CLI extension API.
Package main demonstrates basic usage of the Flow CLI extension API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL