Skip to content

Commit

Permalink
config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bxrne committed Jan 14, 2025
1 parent 40811fb commit aaf93e0
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 52 deletions.
27 changes: 22 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,50 @@ import (
"errors"
"fmt"
"os"
"sync"

"github.com/spf13/viper"
)

// GetConfig returns the application configuration
var (
once sync.Once

Check failure on line 13 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / lint-and-vet

var `once` is unused (unused)
cfg *Config
)

// GetConfig returns the application configuration as a singleton
func GetConfig() (*Config, error) {
var cfg *Config
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")

if err := v.ReadInConfig(); err != nil {
return nil, errors.New("failed to read config file")
cfg = nil
return nil, fmt.Errorf("failed to read config file: %s", err)
}

if err := v.Unmarshal(&cfg); err != nil {
return nil, errors.New("failed to unmarshal config")
cfg = nil
return nil, fmt.Errorf("failed to unmarshal config: %s", err)
}

if err := cfg.Validate(); err != nil {
return nil, err
cfg = nil
return nil, fmt.Errorf("failed to validate config: %s", err)
}

if cfg == nil {
return nil, errors.New("failed to load configuration")
}

return cfg, nil
}

// Reset resets the configuration singleton, useful for testing
func Reset() {
cfg = nil
}

// Validate checks the config to error on empty field
func (cfg *Config) Validate() error {
if cfg.App.Name == "" {
Expand Down
Loading

0 comments on commit aaf93e0

Please sign in to comment.