cli-config

Configure Go CLI applications with Cobra and Viper config precedence.

1|Updated Dec 3, 2025
One-click install
npx skills add https://github.com/yurifrl/cly --skill cli-config
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cli-config
Source: https://github.com/yurifrl/cly/tree/main/.opencode/skill/cli-config
Command: npx skills add https://github.com/yurifrl/cly --skill cli-config

SYSTEM DOCUMENTATION & REQUIREMENTS

## What problem does it solve? This Skill helps developers configure CLI applications using Cobra and Viper, simplifying how settings flow from defaults, config files, environment variables, and flags.

## Core Features & Use Cases

  • Bind Cobra flags to Viper values for centralized config management.
  • Support multiple formats (yaml/json/toml) for configuration files.
  • Provide helper commands (init/show/validate) to manage and validate config.

### Quick Start

  1. Create a new Go module and import github.com/spf13/cobra and github.com/spf13/viper.
  2. Create a sample config.yaml with keys like server.port and server.host, and bind command flags to Viper keys.
  3. Run a simple workflow: init config, show config, and validate configuration.

Frequently Asked Questions about cli-config

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I bind Cobra flags to Viper configuration values in a Go CLI?

To bind Cobra flags to Viper configuration values, you link command flags to Viper keys for centralized config management. This allows your Go CLI to read settings directly from the bound flags while maintaining a single configuration source.

What is the precedence order for environment variables and config files in Cobra and Viper?

In Cobra and Viper integration, precedence is enforced as flags overriding environment variables, which override config files, which finally override defaults. This ensures command-line flags always take priority for hierarchical CLI configuration.

How do I set up hierarchical configuration with multiple formats in a Go CLI?

Hierarchical configuration with multiple formats is set up by integrating Cobra and Viper to support YAML, JSON, and TOML files. You define nested keys like server.port and bind them to commands to manage layered settings.

Can I add helper commands to initialize and validate Viper configuration in Go?

You can add helper commands to initialize, show, and validate Viper configuration within your Go CLI. These commands simplify managing and validating config files, ensuring your environment variables and defaults are correctly structured.

Does this approach work for managing environment overrides in Go-based CLIs?

This approach works for managing environment overrides in Go-based CLIs by applying Viper to handle environment variables. It automatically maps environment variables to configuration keys, ensuring overrides correctly follow the established precedence.

What's the best way to structure nested configuration keys when using Viper?

The best way to structure nested configuration keys when using Viper is to use dot notation for hierarchical access, such as server.host and server.port. This pattern integrates cleanly with Cobra command flags and multi-format config files.