- Add package comments to all packages (pkg/config, pkg/logger, internal/*, cmd/platform)
- Fix context key warnings by using custom ContextKey type
- Export ContextKey type to avoid unexported-return warnings
- Update all context value operations to use ContextKey instead of string
- Update RequestIDKey() and UserIDKey() to return ContextKey
- Fix error checking issues (errcheck)
- Properly handle os.Chdir errors in defer statements
- Properly handle os.Setenv/os.Unsetenv errors in tests
- Fix security warnings (gosec)
- Change directory permissions from 0755 to 0750 in tests
- Change file permissions from 0644 to 0600 in tests
- Fix unused parameter warnings (revive)
- Replace unused parameters with _ in:
* RegisterLifecycleHooks lifecycle functions
* Mock logger implementations
* noOpLogger methods
- Fix type assertion issues (staticcheck)
- Remove unnecessary type assertions in tests
- Use simpler compile-time checks
- Fix exported type stuttering warning
- Add nolint directive for ConfigProvider (standard interface pattern)
- Update golangci-lint configuration
- Add version: 2 field (required for newer versions)
- Remove unsupported linters (typecheck, gosimple)
- Move formatters (gofmt, goimports) to separate formatters section
- Simplify linter list to only well-supported linters
All linting issues resolved (0 issues reported by golangci-lint).
All tests pass and code compiles successfully.
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
// Package config provides the Viper-based implementation of the ConfigProvider interface.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"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()
|
|
// Environment variables can be set in UPPER_SNAKE_CASE format
|
|
// and will automatically map to nested keys (e.g., SERVER_PORT -> server.port)
|
|
// Viper handles this automatically with AutomaticEnv()
|
|
|
|
return NewViperConfig(v), nil
|
|
}
|