Skip to content

Commit 15d6c36

Browse files
committed
refactor(config): upgrade deps and remove stdout printing
- validator v9 → v10, go-defaults → creasty/defaults - Remove fmt.Println on missing config file
1 parent 1f44bcf commit 15d6c36

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"reflect"
1010
"strings"
1111

12-
"github.com/go-playground/validator"
13-
"github.com/mcuadros/go-defaults"
12+
"github.com/creasty/defaults"
13+
"github.com/go-playground/validator/v10"
1414
"github.com/spf13/pflag"
1515
"github.com/spf13/viper"
1616
"gopkg.in/yaml.v3"
@@ -96,7 +96,7 @@ func (l *Loader) Load(config interface{}) error {
9696
}
9797

9898
// Apply default values before reading configuration
99-
defaults.SetDefaults(config)
99+
defaults.Set(config)
100100

101101
// Bind flags dynamically using reflection on `cmdx` tags if a flag set is provided
102102
if l.flags != nil {
@@ -116,11 +116,11 @@ func (l *Loader) Load(config interface{}) error {
116116
}
117117
}
118118

119-
// Attempt to read the configuration file
119+
// Attempt to read the configuration file (missing file is not an error).
120120
if err := l.v.ReadInConfig(); err != nil {
121121
var configFileNotFoundError viper.ConfigFileNotFoundError
122-
if errors.As(err, &configFileNotFoundError) {
123-
fmt.Println("Warning: Config file not found. Falling back to defaults and environment variables.")
122+
if !errors.As(err, &configFileNotFoundError) && !os.IsNotExist(err) {
123+
return fmt.Errorf("failed to read config file: %w", err)
124124
}
125125
}
126126

@@ -139,7 +139,7 @@ func (l *Loader) Load(config interface{}) error {
139139

140140
// Init initializes the configuration file with default values.
141141
func (l *Loader) Init(config interface{}) error {
142-
defaults.SetDefaults(config)
142+
defaults.Set(config)
143143

144144
path := l.v.ConfigFileUsed()
145145
if fileExists(path) {

config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/mcuadros/go-defaults"
8+
"github.com/creasty/defaults"
99
"github.com/raystack/salt/config"
1010
"github.com/spf13/pflag"
1111
)
@@ -145,7 +145,7 @@ log_level: "info"
145145
loader := config.NewLoader(config.WithFile(configFilePath))
146146

147147
// Apply defaults
148-
defaults.SetDefaults(cfg)
148+
defaults.Set(cfg)
149149
cfg.Server.Port = 3000 // Default value
150150
cfg.Server.Host = "default-host.com"
151151
cfg.LogLevel = "debug"

config/example_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package config_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/raystack/salt/config"
8+
)
9+
10+
func ExampleNewLoader() {
11+
type Config struct {
12+
Server struct {
13+
Port int `mapstructure:"port" default:"8080"`
14+
Host string `mapstructure:"host" default:"localhost"`
15+
} `mapstructure:"server"`
16+
LogLevel string `mapstructure:"log_level" default:"info"`
17+
}
18+
19+
var cfg Config
20+
loader := config.NewLoader(
21+
config.WithFile("./config.yaml"),
22+
config.WithEnvPrefix("MYAPP"),
23+
)
24+
25+
if err := loader.Load(&cfg); err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
fmt.Printf("server: %s:%d, log: %s\n", cfg.Server.Host, cfg.Server.Port, cfg.LogLevel)
30+
}

0 commit comments

Comments
 (0)