Files
goplt/internal/config/config.go
0x1d e673fcae6f
Some checks failed
CI / Test (pull_request) Successful in 53s
CI / Lint (pull_request) Failing after 26s
CI / Build (pull_request) Successful in 39s
CI / Format Check (pull_request) Failing after 2s
fix(lint): fix all linting errors
- Check BindEnv return values in config.go
- Add bounds checks for int->int32/uint32 conversions to prevent overflow
- Remove unused test helper functions
2025-11-07 09:34:38 +01:00

115 lines
3.5 KiB
Go

// Package config provides the Viper-based implementation of the ConfigProvider interface.
package config
import (
"fmt"
"strings"
"time"
"git.dcentral.systems/toolz/goplt/pkg/config"
"github.com/spf13/viper"
)
// viperConfig implements the ConfigProvider interface using Viper.
type viperConfig struct {
v *viper.Viper
}
// NewViperConfig creates a new Viper-based configuration provider.
func NewViperConfig(v *viper.Viper) config.ConfigProvider {
return &viperConfig{v: v}
}
// Get retrieves a configuration value by key.
func (vc *viperConfig) Get(key string) any {
return vc.v.Get(key)
}
// Unmarshal unmarshals the entire configuration into the provided struct.
func (vc *viperConfig) Unmarshal(v any) error {
return vc.v.Unmarshal(v)
}
// GetString retrieves a string value by key.
func (vc *viperConfig) GetString(key string) string {
return vc.v.GetString(key)
}
// GetInt retrieves an integer value by key.
func (vc *viperConfig) GetInt(key string) int {
return vc.v.GetInt(key)
}
// GetBool retrieves a boolean value by key.
func (vc *viperConfig) GetBool(key string) bool {
return vc.v.GetBool(key)
}
// GetStringSlice retrieves a string slice value by key.
func (vc *viperConfig) GetStringSlice(key string) []string {
return vc.v.GetStringSlice(key)
}
// GetDuration retrieves a duration value by key.
func (vc *viperConfig) GetDuration(key string) time.Duration {
return vc.v.GetDuration(key)
}
// IsSet checks if a configuration key is set.
func (vc *viperConfig) IsSet(key string) bool {
return vc.v.IsSet(key)
}
// LoadConfig loads configuration from files and environment variables.
// It follows this precedence order (highest to lowest):
// 1. Environment variables
// 2. Environment-specific YAML files (development.yaml, production.yaml)
// 3. Default YAML file (default.yaml)
//
// The env parameter determines which environment-specific file to load.
// Supported values: "development", "production", or empty string for default only.
func LoadConfig(env string) (config.ConfigProvider, error) {
v := viper.New()
// Set default configuration file name
v.SetConfigName("default")
v.SetConfigType("yaml")
v.AddConfigPath("config")
// Read default configuration
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read default config: %w", err)
}
// Load environment-specific configuration if specified
if env != "" {
v.SetConfigName(env)
// Merge environment-specific config (if it exists)
if err := v.MergeInConfig(); err != nil {
// Environment-specific file is optional, so we only warn
// but don't fail if it doesn't exist
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("failed to merge environment config: %w", err)
}
}
}
// Enable environment variable support
v.AutomaticEnv()
// Set env key replacer to convert UPPER_SNAKE_CASE to nested keys
// e.g., DATABASE_DSN -> database.dsn, SERVER_PORT -> server.port
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// Bind specific environment variables to config keys
if err := v.BindEnv("database.dsn", "DATABASE_DSN"); err != nil {
return nil, fmt.Errorf("failed to bind DATABASE_DSN: %w", err)
}
if err := v.BindEnv("registry.consul.address", "REGISTRY_CONSUL_ADDRESS"); err != nil {
return nil, fmt.Errorf("failed to bind REGISTRY_CONSUL_ADDRESS: %w", err)
}
if err := v.BindEnv("registry.type", "REGISTRY_TYPE"); err != nil {
return nil, fmt.Errorf("failed to bind REGISTRY_TYPE: %w", err)
}
return NewViperConfig(v), nil
}