Skip to main content

Cobra and Viper

Cobra and Viper

CLI and configuration libraries for Go applications

Cobra and Viper are foundational libraries used throughout the Control Plane for building CLIs and managing configuration.

Command and configuration framework

Cobra provides a framework for building sophisticated command-line interfaces, while Viper offers a complete configuration solution. Together, they form the backbone of the Control Plane's CLI tools and configuration management.

Overview

Cobra and Viper are complementary libraries developed by the same team that together provide a powerful framework for building command-line applications with sophisticated configuration management.

Cobra Logo

Cobra

Cobra is a library for creating powerful command-line interfaces in Go. It's used in the Control Plane for building tools like:

  • rover-ctl - The Control Plane management CLI
  • route-tester - API route testing utility
  • snapshotter - State capture tool
  • Admin and maintenance utilities

Viper

Viper is a configuration solution for Go applications that prioritizes configuration sources and works with:

  • JSON, TOML, YAML, and other config formats
  • Environment variables
  • Command line flags
  • Remote config systems
  • Live configuration reloading

Cobra in the Control Plane

CLI development framework

Cobra's structured approach to command-line applications helps maintain consistency across all Control Plane CLIs.

Usage in rover-ctl

The Control Plane's primary CLI tool, rover-ctl, uses Cobra to define its command structure:

rover-ctl
├── deploy # Deploy Control Plane components
│ ├── operator # Deploy specific operator
│ └── service # Deploy specific service
├── config # Manage configurations
│ ├── view # View current configuration
│ ├── set # Set configuration value
│ └── import # Import configuration from file
├── get # Get Control Plane resources
│ ├── operator # List operators
│ └── service # List services
└── logs # View logs for Control Plane components

🧩 Modular Commands

Cobra enables modular command organization that scales with application complexity.

🚩 Integrated Flags

Built-in flag support with Pflag library for consistent flag parsing.

📚 Automatic Help

Automatic generation of help text based on command definitions.

⚙️ Shell Completions

Generation of shell completion scripts for bash, zsh, and other shells.

🔄 Middleware Support

Pre and post-run hooks for command execution.

🧪 Testable Commands

Design that makes commands easy to test in isolation.

Best Practices for Cobra in Control Plane

CLI design patterns

The Control Plane follows these best practices for CLI design with Cobra.

Command Organization

Structure commands logically with a noun-verb pattern:

  • Group related commands as subcommands
  • Use consistent naming conventions
  • Keep command hierarchy shallow (max 2-3 levels)
  • Use clear, descriptive command names

Flag Design

Design flags for usability and consistency:

  • Use short flags for common options
  • Provide sensible defaults
  • Use consistent flag names across commands
  • Group related flags logically

Error Handling

Handle errors gracefully and informatively:

  • Return detailed error messages
  • Set appropriate exit codes
  • Use color for error highlighting
  • Provide suggestions for common errors

User Experience

Enhance user experience with:

  • Progress indicators for long-running operations
  • Colorized output for readability
  • Confirmation prompts for destructive actions
  • Verbose mode for debugging

Viper in the Control Plane

Configuration management

Viper provides a complete configuration solution that gives the Control Plane flexibility in how configurations are loaded and prioritized.

🗃️ Multiple Formats

Support for JSON, YAML, TOML, HCL, and other configuration formats.

🔄 Live Reloading

Watch and reload configuration changes without application restart.

🧩 Nested Configuration

Support for nested configuration structures with dot notation access.

🔌 Remote Config

Integration with remote configuration systems like etcd or Consul.

🌐 Environment Variable Binding

Automatic binding of environment variables to configuration keys.

🧬 Configuration Aliasing

Create aliases for configuration keys for backward compatibility.

Best Practices for Viper in Control Plane

Configuration patterns

The Control Plane follows these best practices for configuration management with Viper.

Configuration Structure

Organize configuration logically:

  • Group related settings under namespaces
  • Use consistent naming conventions
  • Keep configuration flat when possible
  • Document all configuration options

Environment Variables

Standardized environment variable usage:

  • Use CP_ prefix for all environment variables
  • Use uppercase with underscores
  • Map to dot notation config keys
  • Document environment variable mappings

Secret Management

Handle sensitive configuration carefully:

  • Never store secrets in version control
  • Use environment variables for secrets
  • Support external secret managers
  • Mask secrets in logs and output

Validation and Safety

Ensure configuration is valid and safe:

  • Validate configuration at startup
  • Provide helpful error messages for invalid config
  • Set reasonable defaults for all settings
  • Use typed configuration access

Integration of Cobra and Viper

Seamless integration

Cobra and Viper are designed to work together seamlessly, creating a powerful framework for CLI applications with sophisticated configuration management.

Control Plane CLI Components

The Control Plane includes several CLI tools built with Cobra and Viper:

rover-ctl

The primary management CLI for the Control Plane:

  • Resource deployment and management
  • Configuration management
  • Monitoring and diagnostics
  • User and access management

route-tester

A utility for testing API routes:

  • Route validation
  • Performance testing
  • Authentication testing
  • Response validation

snapshotter

A tool for capturing system state:

  • Resource state capture
  • Configuration export
  • Diagnostic information collection
  • Snapshot comparison

controller-gen

Code generation utility for controllers:

  • CRD generation from Go types
  • RBAC manifest generation
  • OpenAPI schema generation
  • Webhook configuration generation

Common Configuration Patterns

Configuration management patterns

Common patterns for configuration management in the Control Plane.

🔒 Secure Configuration

Support for encrypted configuration values and external secret managers.

📊 Environment-Specific Config

Environment-aware configuration loading (dev, staging, production).

📝 Schema Validation

JSON Schema validation for configuration files to catch errors early.

🔄 Configuration Reloading

Hot reloading of configuration changes without service restart.

📦 Cascading Configuration

Layered configuration from system to user to project levels.

🌐 Remote Configuration

Support for loading configuration from centralized services.

Go Language

Learn about the foundation language used for all Control Plane components.

CLI Overview

Explore the CLI tools available in the Control Plane.

Official Cobra Documentation

Visit the official Cobra documentation for comprehensive guides.

Official Viper Documentation

Visit the official Viper documentation for detailed configuration options.