Skip to content

Commit d9186f8

Browse files
committed
implement config merge logic with defaults and env overrides
1 parent cd741fe commit d9186f8

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

config.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,37 @@ func getConfigPath() (string, error) {
3434
}
3535

3636
func loadConfig() (*Config, error) {
37-
// Try .env first (for development)
38-
spotifyClientID := os.Getenv("SPOTIFY_CLIENT_ID")
39-
spotifyClientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET")
40-
lastfmAPIKey := os.Getenv("LASTFM_API_KEY")
41-
42-
if spotifyClientID != "" && spotifyClientSecret != "" && lastfmAPIKey != "" {
43-
return &Config{
44-
SpotifyClientID: spotifyClientID,
45-
SpotifyClientSecret: spotifyClientSecret,
46-
LastFmAPIKey: lastfmAPIKey,
47-
}, nil
37+
// defaults
38+
config := &Config{
39+
DefaultCount: 10,
40+
OutputDir: "./foraged-tracks",
41+
QuietMode: false,
42+
IncludeSource: false,
43+
UseText: false,
4844
}
49-
50-
// Try config file
45+
5146
configPath, err := getConfigPath()
52-
if err != nil {
53-
return nil, err
54-
}
55-
56-
data, err := os.ReadFile(configPath)
57-
if err != nil {
58-
if os.IsNotExist(err) {
59-
return nil, fmt.Errorf("config file not found")
47+
if err == nil {
48+
if data, err := os.ReadFile(configPath); err == nil {
49+
_ = yaml.Unmarshal(data, config)
6050
}
61-
return nil, err
6251
}
63-
64-
var config Config
65-
if err := yaml.Unmarshal(data, &config); err != nil {
66-
return nil, err
52+
53+
if envID := os.Getenv("SPOTIFY_CLIENT_ID"); envID != "" {
54+
config.SpotifyClientID = envID
6755
}
68-
56+
if envSecret := os.Getenv("SPOTIFY_CLIENT_SECRET"); envSecret != "" {
57+
config.SpotifyClientSecret = envSecret
58+
}
59+
if envKey := os.Getenv("LASTFM_API_KEY"); envKey != "" {
60+
config.LastFmAPIKey = envKey
61+
}
62+
6963
if config.SpotifyClientID == "" || config.SpotifyClientSecret == "" || config.LastFmAPIKey == "" {
7064
return nil, fmt.Errorf("missing credentials")
7165
}
72-
73-
return &config, nil
66+
67+
return config, nil
7468
}
7569

7670
func handleConfig() {

0 commit comments

Comments
 (0)