Skip to content

allow missing config file#763

Merged
matthyx merged 1 commit intomainfrom
noconfig
Mar 31, 2026
Merged

allow missing config file#763
matthyx merged 1 commit intomainfrom
noconfig

Conversation

@matthyx
Copy link
Copy Markdown
Contributor

@matthyx matthyx commented Mar 30, 2026

Summary by CodeRabbit

  • New Features
    • Configuration file loading now supports optional behavior for missing files, providing greater flexibility during application initialization.

Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

📝 Walkthrough

Walkthrough

A new LoadConfigOptional function was added to provide flexible config file loading with optional error handling for missing config files. The existing LoadConfig function was refactored to delegate to this new function, maintaining backward compatibility while allowing callers to control whether missing config files are treated as errors.

Changes

Cohort / File(s) Summary
Config Loading Refactoring
pkg/config/config.go
Added LoadConfigOptional(path string, errNotFound bool) function that conditionally ignores viper.ConfigFileNotFoundError based on the errNotFound parameter. Refactored LoadConfig to delegate to LoadConfigOptional with errNotFound=true. Updated error handling with inline variable declarations for cleaner code flow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A config so wise, now wears two faces—
One strict about missing file cases,
The other serene when files are gone,
The rabbit hops onward, refactoring done! 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'allow missing config file' directly describes the main functional change: making the config file loading behavior optional based on whether to error when the file is not found.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch noconfig

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
pkg/config/config.go (2)

143-143: Add a doc comment for this exported function.

The new exported function lacks documentation. Consider adding a doc comment explaining the purpose and the errNotFound parameter semantics.

📝 Suggested documentation
+// LoadConfigOptional reads configuration from file or environment variables.
+// If errNotFound is false, a missing config file is silently ignored and defaults are used.
+// If errNotFound is true, a missing config file results in an error (same as LoadConfig).
 func LoadConfigOptional(path string, errNotFound bool) (Config, error) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/config/config.go` at line 143, Add a Go doc comment for the exported
function LoadConfigOptional that briefly describes its purpose (loading
configuration from the given path) and explains the semantics of the errNotFound
boolean (e.g., when true, treat missing file as non-fatal and return
default/empty Config; when false, return an error if the file is missing), and
include any important behavior such as return values and error conditions; place
the comment immediately above the LoadConfigOptional function declaration.

223-228: Consider simplifying the double-negative condition for readability.

The condition !(errors.As(err, &notFound) && !errNotFound) is logically correct but hard to parse due to the double negation. An equivalent positive expression would be clearer.

♻️ Suggested simplification
 	if err := viper.ReadInConfig(); err != nil {
 		var notFound viper.ConfigFileNotFoundError
-		if !(errors.As(err, &notFound) && !errNotFound) {
+		if !errors.As(err, &notFound) || errNotFound {
 			return Config{}, err
 		}
+		// Config file not found but optional; continue with defaults
 	}

This reads as: "return the error if it's not a file-not-found error, or if file-not-found should be treated as an error."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/config/config.go` around lines 223 - 228, The current double-negative
condition after viper.ReadInConfig makes the logic hard to read; replace the
expression `!(errors.As(err, &notFound) && !errNotFound)` with the equivalent
positive check `!errors.As(err, &notFound) || errNotFound` so the block around
viper.ReadInConfig, the viper.ConfigFileNotFoundError variable, and the
errNotFound flag clearly reads: if the error is not a ConfigFileNotFoundError or
file-not-found should be treated as an error, return the error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/config/config.go`:
- Line 143: Add a Go doc comment for the exported function LoadConfigOptional
that briefly describes its purpose (loading configuration from the given path)
and explains the semantics of the errNotFound boolean (e.g., when true, treat
missing file as non-fatal and return default/empty Config; when false, return an
error if the file is missing), and include any important behavior such as return
values and error conditions; place the comment immediately above the
LoadConfigOptional function declaration.
- Around line 223-228: The current double-negative condition after
viper.ReadInConfig makes the logic hard to read; replace the expression
`!(errors.As(err, &notFound) && !errNotFound)` with the equivalent positive
check `!errors.As(err, &notFound) || errNotFound` so the block around
viper.ReadInConfig, the viper.ConfigFileNotFoundError variable, and the
errNotFound flag clearly reads: if the error is not a ConfigFileNotFoundError or
file-not-found should be treated as an error, return the error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0ce8072a-5f0a-44b2-9668-ba7fcb0b43f5

📥 Commits

Reviewing files that changed from the base of the PR and between 2b05e16 and 1f2306f.

📒 Files selected for processing (1)
  • pkg/config/config.go

@matthyx matthyx merged commit e444f1d into main Mar 31, 2026
27 checks passed
@matthyx matthyx deleted the noconfig branch March 31, 2026 07:40
@matthyx matthyx moved this to To Archive in KS PRs tracking Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants