cmd

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package cmd provides CLI flag registration using a modular pattern.

To add a new flag without causing merge conflicts: 1. Create a new file (e.g., flags_myfeature.go) 2. Define your flag variable and default at the top 3. Create an init() function that calls RegisterFlag()

Example (flags_myfeature.go):

package cmd

var myFeatureEnabled bool

func init() {
	RegisterFlag(func(cmd *cobra.Command) {
		cmd.Flags().BoolVar(&myFeatureEnabled, "my-feature", false, "Enable my feature")
	})
}

getDefault*() Flag Helper Pattern

Each flag whose default value can be overridden by an environment variable has a corresponding getDefault*() helper function that follows this pattern:

func getDefaultXxx() T {
    return envutil.GetEnvT("MCP_GATEWAY_XXX", defaultXxx)
}

Current helpers and their environment variables:

flags_logging.go  getDefaultLogDir()              → MCP_GATEWAY_LOG_DIR
flags_logging.go  getDefaultPayloadDir()          → MCP_GATEWAY_PAYLOAD_DIR
flags_logging.go  getDefaultPayloadPathPrefix()   → MCP_GATEWAY_PAYLOAD_PATH_PREFIX
flags_logging.go  getDefaultPayloadSizeThreshold() → MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD
flags_difc.go     getDefaultDIFCMode()             → MCP_GATEWAY_GUARDS_MODE
flags_difc.go     getDefaultDIFCSinkServerIDs()    → MCP_GATEWAY_GUARDS_SINK_SERVER_IDS

This pattern is intentionally kept in individual feature files because:

  • Each helper names the specific environment variable it reads, making the coupling between flag and env var explicit and discoverable.
  • The one-liner wrappers are trivial and unlikely to diverge.
  • Go's type system (string/int/bool) prevents a single generic helper without sacrificing readability.

When adding a new flag with an environment variable override:

  1. Add a defaultXxx constant and a getDefaultXxx() function in the feature file.
  2. Add the new helper to the table above.

Index

Constants

View Source
const (
	// DefaultListenIPv4 is the default interface used by the HTTP server.
	DefaultListenIPv4 = "127.0.0.1"
	// DefaultListenPort is the default port used by the HTTP server.
	DefaultListenPort = "3000"
)

Exported constants for use by other packages

Variables

This section is empty.

Functions

func Execute

func Execute()

Execute runs the root command

func RegisterFlag

func RegisterFlag(fn FlagRegistrar)

RegisterFlag adds a flag registrar to be called during init This allows each feature to register its own flags without modifying root.go

func SetVersion

func SetVersion(v string)

SetVersion sets the version string for the CLI

func ValidateDIFCMode added in v0.1.10

func ValidateDIFCMode(mode string) error

ValidateDIFCMode validates the guards mode flag value and returns an error if invalid

Types

type FlagRegistrar

type FlagRegistrar func(cmd *cobra.Command)

FlagRegistrar is a function that registers flags on a command

Jump to

Keyboard shortcuts

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