-
Notifications
You must be signed in to change notification settings - Fork 18
Update: Handle .env load errors gracefully #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Changes godotenv.Load() to ignore errors, allowing - fallback to system environment variables for - robustness in various environments.
WalkthroughThe main program restructures initialization and control flow in cmd/commit-msg/main.go: .env loading is made non-fatal, COMMIT_LLM validation occurs earlier, repository checks and config initialization are reordered, generator dispatch is moved near spinner start, and UI/clipboard steps are resequenced. No public API changes. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant User
participant CLI as commit-msg CLI
participant Env as .env Loader
participant Repo as Git Repo
participant Gen as LLM Generator
participant CB as Clipboard
User->>CLI: Run commit-msg
CLI->>Env: Load .env (non-fatal)
Note right of Env: Ignore failure
CLI->>CLI: Read COMMIT_LLM / API keys
CLI->>CLI: Validate COMMIT_LLM and required keys
CLI->>Repo: Validate repository state
Repo-->>CLI: OK / No changes
alt No changes
CLI-->>User: Exit early (nothing to commit)
else Changes present
CLI->>CLI: Collect file stats/changes
CLI->>CLI: Start spinner
CLI->>Gen: Dispatch by LLM (ollama/grok/chatgpt/claude/gemini)
Gen-->>CLI: Generate commit message
CLI->>CLI: Stop spinner
CLI-->>User: Display commit message
CLI->>CB: Copy to clipboard (if enabled)
CB-->>CLI: Success/Failure
CLI-->>User: Show changes preview
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
cmd/commit-msg/main.go (2)
56-57: Enhance error message with supported values.The error message for invalid COMMIT_LLM values would be more helpful if it listed the supported options.
Apply this diff to improve the error message:
default: - log.Fatalf("Invalid COMMIT_LLM value: %s", commitLLM) + log.Fatalf("Invalid COMMIT_LLM value: %s. Supported values: gemini, grok, chatgpt, claude, ollama", commitLLM)
124-144: Make grok case explicit in switch statement.The
grokcase is currently handled by thedefaultclause. While functionally correct (since invalid values are caught earlier), this pattern is confusing and fragile. An explicit case forgrokwould improve code clarity and maintainability.Apply this diff to make the grok case explicit:
var commitMsg string switch commitLLM { case "gemini": commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey) case "chatgpt": commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey) case "claude": commitMsg, err = claude.GenerateCommitMessage(config, changes, apiKey) + case "grok": + commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey) case "ollama": url := os.Getenv("OLLAMA_URL") if url == "" { url = "http://localhost:11434/api/generate" } model := os.Getenv("OLLAMA_MODEL") if model == "" { model = "llama3:latest" } commitMsg, err = ollama.GenerateCommitMessage(config, changes, url, model) default: - commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey) + // This should be unreachable since we validate commitLLM earlier + log.Fatalf("Unexpected COMMIT_LLM value: %s", commitLLM) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
cmd/commit-msg/main.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
cmd/commit-msg/main.go (9)
internal/git/operations.go (2)
IsRepository(16-23)GetChanges(26-130)pkg/types/types.go (2)
Config(4-7)RepoConfig(10-13)internal/stats/statistics.go (1)
GetFileStatistics(14-74)internal/display/display.go (3)
ShowFileStatistics(20-96)ShowCommitMessage(99-115)ShowChangesPreview(118-133)internal/chatgpt/chatgpt.go (1)
GenerateCommitMessage(13-32)internal/claude/claude.go (1)
GenerateCommitMessage(33-85)internal/ollama/ollama.go (1)
GenerateCommitMessage(24-75)internal/gemini/gemini.go (1)
GenerateCommitMessage(13-44)internal/grok/grok.go (1)
GenerateCommitMessage(15-87)
🔇 Additional comments (3)
cmd/commit-msg/main.go (3)
24-26: LGTM! Non-fatal .env loading improves robustness.The change to silently ignore .env loading errors allows the application to gracefully fall back to system environment variables, improving portability across different deployment environments.
60-79: LGTM! Logical initialization order.The reordering of initialization steps makes sense: environment validation occurs first, followed by filesystem and git repository checks. This fail-fast approach improves user experience by catching configuration errors early.
146-169: LGTM! Proper error handling and user feedback.The error handling, success messaging, and clipboard operations are well-implemented with appropriate user feedback for both success and failure cases.
Description
Handle .env load errors gracefully
Type of Change
Changes godotenv.Load() to ignore errors, allowing
fallback to system environment variables for
robustness in various environments.
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
Documentation update
Code refactoring
Performance improvement
Other (please describe):
Related Issue
Fixes #(issue number)
Changes Made
Testing
Checklist
Screenshots (if applicable)
Additional Notes
For Hacktoberfest Participants
Thank you for your contribution! 🎉
Summary by CodeRabbit
Bug Fixes
Refactor