Skip to content

Commit a887ed4

Browse files
authored
Refactor configuration loading to accept command flags (#20)
- Updated loadConfig function to accept command flags for better flexibility. - Enhanced error handling for configuration loading based on command line inputs. - Adjusted viper.Unmarshal usage to avoid conflicts when a config file is auto-loaded.
1 parent e249f27 commit a887ed4

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

cmd/mcpproxy/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func main() {
7676

7777
func runServer(cmd *cobra.Command, _ []string) error {
7878
// Load configuration first to get logging settings
79-
cfg, err := loadConfig()
79+
cfg, err := loadConfig(cmd)
8080
if err != nil {
8181
return fmt.Errorf("failed to load configuration: %w", err)
8282
}
@@ -228,7 +228,7 @@ func runServer(cmd *cobra.Command, _ []string) error {
228228
return nil
229229
}
230230

231-
func loadConfig() (*config.Config, error) {
231+
func loadConfig(cmd *cobra.Command) (*config.Config, error) {
232232
var cfg *config.Config
233233
var err error
234234

@@ -243,11 +243,11 @@ func loadConfig() (*config.Config, error) {
243243
return nil, fmt.Errorf("failed to load configuration: %w", err)
244244
}
245245

246-
// Override with command line flags if provided
246+
// Override with command line flags ONLY if they were explicitly set
247247
if dataDir != "" {
248248
cfg.DataDir = dataDir
249249
}
250-
if listen != "" {
250+
if cmd.Flags().Changed("listen") {
251251
cfg.Listen = listen
252252
}
253253
if toolResponseLimit != 0 {

internal/config/loader.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func Load() (*Config, error) {
5757

5858
// Load from config file if specified
5959
configPath := viper.GetString("config")
60+
configFileAutoLoaded := false
6061
if configPath != "" {
6162
if err := loadConfigFile(configPath, cfg); err != nil {
6263
return nil, fmt.Errorf("failed to load config file %s: %w", configPath, err)
@@ -67,6 +68,7 @@ func Load() (*Config, error) {
6768
if err != nil && configFound {
6869
return nil, err // Only return error if config was found but couldn't be loaded
6970
}
71+
configFileAutoLoaded = configFound
7072

7173
// If no config file was found, create a default one
7274
if !configFound {
@@ -94,9 +96,13 @@ func Load() (*Config, error) {
9496
}
9597
}
9698

97-
// Override with viper (CLI flags and env vars)
98-
if err := viper.Unmarshal(cfg); err != nil {
99-
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
99+
// Only use viper.Unmarshal if no config file was auto-loaded
100+
// When config file is auto-loaded, CLI flags are handled in main.go
101+
if !configFileAutoLoaded {
102+
// Override with viper (CLI flags and env vars)
103+
if err := viper.Unmarshal(cfg); err != nil {
104+
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
105+
}
100106
}
101107

102108
// Set data directory if not specified
@@ -169,7 +175,6 @@ func findAndLoadConfigFile(cfg *Config) (found bool, path string, err error) {
169175
return true, location, loadConfigFile(location, cfg)
170176
}
171177
}
172-
173178
return false, "", nil
174179
}
175180

0 commit comments

Comments
 (0)