diff --git a/CHANGELOG.md b/CHANGELOG.md index 60a59711..f739f510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +Fixed: +- CLI now warns users when `.fga.yaml` config file has YAML parsing errors instead of silently ignoring them. Use `--debug` flag or set `FGA_DEBUG=true` for detailed error messages + ## [0.7.8] - 2025-11-05 Fixed: diff --git a/cmd/root.go b/cmd/root.go index 46fa3cd1..493b8ffc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ limitations under the License. package cmd import ( + "errors" "fmt" "os" "strings" @@ -51,6 +52,33 @@ func Execute() { } } +// isDebugMode checks if debug mode is enabled via --debug flag or FGA_DEBUG env var. +// We are not following cobra's built-in flag checking here because we want to determine +// debug mode status before cobra parses flags (to control logging during initConfig). +// The precedence is: +// 1. Command-line flag --debug +// 2. Environment variable FGA_DEBUG +// Other areas in the code should parse the flag using cobra after initialization rather +// than rely on this function. +func isDebugMode() bool { + // Command-line flag takes precedence + for _, arg := range os.Args { + if arg == "--debug=true" { + return true + } + if arg == "--debug=false" { + return false + } + } + + // Check environment variable first + if os.Getenv("FGA_DEBUG") == "true" { + return true + } + + return false +} + func init() { cobra.OnInitialize(initConfig) @@ -119,6 +147,18 @@ func initConfig() { // If a config file is found, read it in. if err := viperInstance.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viperInstance.ConfigFileUsed()) + } else { + // Check if error is due to config file not found (this is OK, we continue silently) + var configFileNotFoundError viper.ConfigFileNotFoundError + if !errors.As(err, &configFileNotFoundError) { + // Config file exists but failed to parse - show warning + if isDebugMode() { + fmt.Fprintf(os.Stderr, "Warning: Failed to load config file %s: %v\n", + viperInstance.ConfigFileUsed(), err) + } else { + fmt.Fprintln(os.Stderr, "Warning: Failed to load config file. Use --debug=true or set FGA_DEBUG=true for details.") + } + } } viperInstance.SetEnvPrefix("FGA")