Skip to content

Commit 9547765

Browse files
committed
Update: Handle .env load errors gracefully
- Changes godotenv.Load() to ignore errors, allowing - fallback to system environment variables for - robustness in various environments.
1 parent 4082d3a commit 9547765

1 file changed

Lines changed: 122 additions & 122 deletions

File tree

cmd/commit-msg/main.go

Lines changed: 122 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -21,150 +21,150 @@ import (
2121
// main is the entry point of the commit message generator
2222
func main() {
2323
// Load the .env file
24-
if err := godotenv.Load(); err != nil {
25-
log.Printf("warning: unable to load .env file: %v", err)
26-
}
27-
28-
// Validate COMMIT_LLM and required API keys
29-
commitLLM := os.Getenv("COMMIT_LLM")
30-
var apiKey string
31-
32-
switch commitLLM {
33-
case "gemini":
34-
apiKey = os.Getenv("GEMINI_API_KEY")
35-
if apiKey == "" {
36-
log.Fatalf("GEMINI_API_KEY is not set")
37-
}
38-
case "grok":
39-
apiKey = os.Getenv("GROK_API_KEY")
40-
if apiKey == "" {
41-
log.Fatalf("GROK_API_KEY is not set")
42-
}
43-
case "chatgpt":
44-
apiKey = os.Getenv("OPENAI_API_KEY")
45-
if apiKey == "" {
46-
log.Fatalf("OPENAI_API_KEY is not set")
47-
}
48-
case "claude":
49-
apiKey = os.Getenv("CLAUDE_API_KEY")
50-
if apiKey == "" {
51-
log.Fatalf("CLAUDE_API_KEY is not set")
52-
}
24+
// Try to load .env file, but don't fail if it doesn't exist
25+
// System environment variables will be used as fallback
26+
_ = godotenv.Load()
27+
28+
// Validate COMMIT_LLM and required API keys
29+
commitLLM := os.Getenv("COMMIT_LLM")
30+
var apiKey string
31+
32+
switch commitLLM {
33+
case "gemini":
34+
apiKey = os.Getenv("GEMINI_API_KEY")
35+
if apiKey == "" {
36+
log.Fatalf("GEMINI_API_KEY is not set")
37+
}
38+
case "grok":
39+
apiKey = os.Getenv("GROK_API_KEY")
40+
if apiKey == "" {
41+
log.Fatalf("GROK_API_KEY is not set")
42+
}
43+
case "chatgpt":
44+
apiKey = os.Getenv("OPENAI_API_KEY")
45+
if apiKey == "" {
46+
log.Fatalf("OPENAI_API_KEY is not set")
47+
}
48+
case "claude":
49+
apiKey = os.Getenv("CLAUDE_API_KEY")
50+
if apiKey == "" {
51+
log.Fatalf("CLAUDE_API_KEY is not set")
52+
}
5353
case "ollama":
5454
// No API key required to run a local LLM
5555
apiKey = ""
56-
default:
57-
log.Fatalf("Invalid COMMIT_LLM value: %s", commitLLM)
58-
}
59-
60-
// Get current directory
61-
currentDir, err := os.Getwd()
62-
if err != nil {
63-
log.Fatalf("Failed to get current directory: %v", err)
64-
}
56+
default:
57+
log.Fatalf("Invalid COMMIT_LLM value: %s", commitLLM)
58+
}
6559

66-
// Check if current directory is a git repository
67-
if !git.IsRepository(currentDir) {
68-
log.Fatalf("Current directory is not a Git repository: %s", currentDir)
69-
}
60+
// Get current directory
61+
currentDir, err := os.Getwd()
62+
if err != nil {
63+
log.Fatalf("Failed to get current directory: %v", err)
64+
}
7065

71-
// Create a minimal config for the API
72-
config := &types.Config{
73-
GrokAPI: "https://api.x.ai/v1/chat/completions",
74-
}
66+
// Check if current directory is a git repository
67+
if !git.IsRepository(currentDir) {
68+
log.Fatalf("Current directory is not a Git repository: %s", currentDir)
69+
}
7570

76-
// Create a repo config for the current directory
77-
repoConfig := types.RepoConfig{
78-
Path: currentDir,
79-
}
71+
// Create a minimal config for the API
72+
config := &types.Config{
73+
GrokAPI: "https://api.x.ai/v1/chat/completions",
74+
}
8075

81-
// Get file statistics before fetching changes
82-
fileStats, err := stats.GetFileStatistics(&repoConfig)
83-
if err != nil {
84-
log.Fatalf("Failed to get file statistics: %v", err)
85-
}
76+
// Create a repo config for the current directory
77+
repoConfig := types.RepoConfig{
78+
Path: currentDir,
79+
}
8680

87-
// Display header
88-
pterm.DefaultHeader.WithFullWidth().
89-
WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)).
90-
WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)).
91-
Println("🚀 Commit Message Generator")
81+
// Get file statistics before fetching changes
82+
fileStats, err := stats.GetFileStatistics(&repoConfig)
83+
if err != nil {
84+
log.Fatalf("Failed to get file statistics: %v", err)
85+
}
9286

93-
pterm.Println()
87+
// Display header
88+
pterm.DefaultHeader.WithFullWidth().
89+
WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)).
90+
WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)).
91+
Println("🚀 Commit Message Generator")
9492

95-
// Display file statistics with icons
96-
display.ShowFileStatistics(fileStats)
93+
pterm.Println()
9794

98-
if fileStats.TotalFiles == 0 {
99-
pterm.Warning.Println("No changes detected in the Git repository.")
100-
return
101-
}
95+
// Display file statistics with icons
96+
display.ShowFileStatistics(fileStats)
10297

103-
// Get the changes
104-
changes, err := git.GetChanges(&repoConfig)
105-
if err != nil {
106-
log.Fatalf("Failed to get Git changes: %v", err)
107-
}
98+
if fileStats.TotalFiles == 0 {
99+
pterm.Warning.Println("No changes detected in the Git repository.")
100+
return
101+
}
108102

109-
if len(changes) == 0 {
110-
pterm.Warning.Println("No changes detected in the Git repository.")
111-
return
112-
}
103+
// Get the changes
104+
changes, err := git.GetChanges(&repoConfig)
105+
if err != nil {
106+
log.Fatalf("Failed to get Git changes: %v", err)
107+
}
113108

114-
pterm.Println()
109+
if len(changes) == 0 {
110+
pterm.Warning.Println("No changes detected in the Git repository.")
111+
return
112+
}
115113

116-
// Show generating spinner
117-
spinnerGenerating, err := pterm.DefaultSpinner.
118-
WithSequence("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏").
119-
Start("🤖 Generating commit message...")
120-
if err != nil {
121-
log.Fatalf("Failed to start spinner: %v", err)
122-
}
114+
pterm.Println()
123115

124-
var commitMsg string
125-
switch commitLLM {
126-
case "gemini":
127-
commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey)
128-
case "chatgpt":
129-
commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey)
130-
case "claude":
131-
commitMsg, err = claude.GenerateCommitMessage(config, changes, apiKey)
132-
case "ollama":
133-
url := os.Getenv("OLLAMA_URL")
134-
if url == "" {
135-
url = "http://localhost:11434/api/generate"
136-
}
137-
model := os.Getenv("OLLAMA_MODEL")
138-
if model == "" {
139-
model = "llama3:latest"
140-
}
141-
commitMsg, err = ollama.GenerateCommitMessage(config, changes, url, model)
142-
default:
143-
commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey)
144-
}
116+
// Show generating spinner
117+
spinnerGenerating, err := pterm.DefaultSpinner.
118+
WithSequence("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏").
119+
Start("🤖 Generating commit message...")
120+
if err != nil {
121+
log.Fatalf("Failed to start spinner: %v", err)
122+
}
145123

146-
if err != nil {
147-
spinnerGenerating.Fail("Failed to generate commit message")
148-
log.Fatalf("Error: %v", err)
124+
var commitMsg string
125+
switch commitLLM {
126+
case "gemini":
127+
commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey)
128+
case "chatgpt":
129+
commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey)
130+
case "claude":
131+
commitMsg, err = claude.GenerateCommitMessage(config, changes, apiKey)
132+
case "ollama":
133+
url := os.Getenv("OLLAMA_URL")
134+
if url == "" {
135+
url = "http://localhost:11434/api/generate"
149136
}
137+
model := os.Getenv("OLLAMA_MODEL")
138+
if model == "" {
139+
model = "llama3:latest"
140+
}
141+
commitMsg, err = ollama.GenerateCommitMessage(config, changes, url, model)
142+
default:
143+
commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey)
144+
}
150145

151-
spinnerGenerating.Success("✅ Commit message generated successfully!")
146+
if err != nil {
147+
spinnerGenerating.Fail("Failed to generate commit message")
148+
log.Fatalf("Error: %v", err)
149+
}
152150

153-
pterm.Println()
151+
spinnerGenerating.Success("✅ Commit message generated successfully!")
154152

155-
// Display the commit message in a styled panel
156-
display.ShowCommitMessage(commitMsg)
153+
pterm.Println()
157154

158-
// Copy to clipboard
159-
err = clipboard.WriteAll(commitMsg)
160-
if err != nil {
161-
pterm.Warning.Printf("⚠️ Could not copy to clipboard: %v\n", err)
162-
} else {
163-
pterm.Success.Println("📋 Commit message copied to clipboard!")
164-
}
155+
// Display the commit message in a styled panel
156+
display.ShowCommitMessage(commitMsg)
157+
158+
// Copy to clipboard
159+
err = clipboard.WriteAll(commitMsg)
160+
if err != nil {
161+
pterm.Warning.Printf("⚠️ Could not copy to clipboard: %v\n", err)
162+
} else {
163+
pterm.Success.Println("📋 Commit message copied to clipboard!")
164+
}
165165

166-
pterm.Println()
166+
pterm.Println()
167167

168-
// Display changes preview
169-
display.ShowChangesPreview(fileStats)
168+
// Display changes preview
169+
display.ShowChangesPreview(fileStats)
170170
}

0 commit comments

Comments
 (0)