$ show spf13/viper
Viper is a mature, widely-used Go configuration library that provides a unified API for reading configuration from multiple sources: files (YAML, JSON, TOML, dotenv), environment variables, command-line flags (via pflag), remote key-value stores (etcd, Consul, Firestore, NATS), and explicit overrides. It implements a priority-based merging system where later sources override earlier ones. The library offers nested key access with dot notation, aliasing, live file watching, and unmarshaling to structs via mapstructure. The architecture is a single Viper struct with internal maps for each source, plus a pluggable encoding layer and a finder interface for config file discovery. It is designed as a library to be embedded in Go applications, not as a standalone service.
The LLM identified 3 files worth highlighting for reuse:
Defines Encoder, Decoder, Codec interfaces and a DefaultCodecRegistry that allows registering custom codecs for different configuration formats. This is a clean, self-contained abstraction that can be extracted and reused in any project needing pluggable serialization/deserialization. The registry supports case-insensitive format lookup and includes built-in codecs for YAML, JSON, TOML, and dotenv.
Defines the Finder interface for locating configuration files in an afero filesystem, along with a Finders function that combines multiple finders. This is a reusable pattern for abstracting file discovery logic. The combinedFinder aggregates results from multiple finders, handling errors gracefully. It can be used independently of Viper for any file-searching task.
Defines FlagValue and FlagValueSet interfaces to abstract away the specific flag library (pflag). This allows Viper to bind to any flag system that implements these interfaces. The pflagValue and pflagValueSet adapters are provided for pflag. This pattern is reusable for integrating different flag libraries with a configuration system.