Skip to content

Commit e96d7de

Browse files
committed
fix: show warning when config is invalid
1 parent 57ff47b commit e96d7de

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
Fixed:
11+
- 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
12+
1013
## [0.7.8] - 2025-11-05
1114

1215
Fixed:

cmd/root.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package cmd
1919

2020
import (
21+
"errors"
2122
"fmt"
2223
"os"
2324
"strings"
@@ -51,6 +52,33 @@ func Execute() {
5152
}
5253
}
5354

55+
// isDebugMode checks if debug mode is enabled via --debug flag or FGA_DEBUG env var.
56+
// We are not following cobra's built-in flag checking here because we want to determine
57+
// debug mode status before cobra parses flags (to control logging during initConfig).
58+
// The precedence is:
59+
// 1. Command-line flag --debug
60+
// 2. Environment variable FGA_DEBUG
61+
// Other areas in the code should parse the flag using cobra after initialization rather
62+
// than rely on this function.
63+
func isDebugMode() bool {
64+
// Command-line flag takes precedence
65+
for _, arg := range os.Args {
66+
if arg == "--debug=true" {
67+
return true
68+
}
69+
if arg == "--debug=false" {
70+
return false
71+
}
72+
}
73+
74+
// Check environment variable first
75+
if os.Getenv("FGA_DEBUG") == "true" {
76+
return true
77+
}
78+
79+
return false
80+
}
81+
5482
func init() {
5583
cobra.OnInitialize(initConfig)
5684

@@ -119,6 +147,18 @@ func initConfig() {
119147
// If a config file is found, read it in.
120148
if err := viperInstance.ReadInConfig(); err == nil {
121149
fmt.Fprintln(os.Stderr, "Using config file:", viperInstance.ConfigFileUsed())
150+
} else {
151+
// Check if error is due to config file not found (this is OK, we continue silently)
152+
var configFileNotFoundError viper.ConfigFileNotFoundError
153+
if !errors.As(err, &configFileNotFoundError) {
154+
// Config file exists but failed to parse - show warning
155+
if isDebugMode() {
156+
fmt.Fprintf(os.Stderr, "Warning: Failed to load config file %s: %v\n",
157+
viperInstance.ConfigFileUsed(), err)
158+
} else {
159+
fmt.Fprintln(os.Stderr, "Warning: Failed to load config file. Use --debug=true or set FGA_DEBUG=true for details.")
160+
}
161+
}
122162
}
123163

124164
viperInstance.SetEnvPrefix("FGA")

0 commit comments

Comments
 (0)