From a9933f72662483ac78d58fc1521cdd730d7a9bf4 Mon Sep 17 00:00:00 2001 From: Juanjo Presa Date: Thu, 14 Aug 2025 13:24:36 +0200 Subject: [PATCH] Fix fatal error on missing config file by handling viper.ConfigFileNotFoundError Correctly identify Viper's ConfigFileNotFoundError in LoadConfig to log a warning and use defaults, unifying behavior with empty config files. Fixes fatal error when no config file is present for CLI commands relying on environment variables. --- hscontrol/types/config.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 44773a55..07c97274 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -336,11 +336,11 @@ func LoadConfig(path string, isFile bool) error { viper.SetDefault("prefixes.allocation", string(IPAllocationStrategySequential)) if err := viper.ReadInConfig(); err != nil { - if errors.Is(err, fs.ErrNotExist) { - log.Warn().Msg("No config file found, using defaults") - return nil - } - + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + log.Warn().Msg("No config file found, using defaults") + return nil + } + return fmt.Errorf("fatal error reading config file: %w", err) }