|
1 | 1 | package main |
2 | 2 |
|
3 | | -import ( |
4 | | - "log" |
5 | | - "os" |
6 | | - |
7 | | - "github.com/atotto/clipboard" |
8 | | - "github.com/dfanso/commit-msg/internal/chatgpt" |
9 | | - "github.com/dfanso/commit-msg/internal/claude" |
10 | | - "github.com/dfanso/commit-msg/internal/display" |
11 | | - "github.com/dfanso/commit-msg/internal/gemini" |
12 | | - "github.com/dfanso/commit-msg/internal/git" |
13 | | - "github.com/dfanso/commit-msg/internal/grok" |
14 | | - "github.com/dfanso/commit-msg/internal/stats" |
15 | | - "github.com/dfanso/commit-msg/pkg/types" |
16 | | - "github.com/pterm/pterm" |
17 | | -) |
| 3 | +import cmd "github.com/dfanso/commit-msg/cmd/cli" |
18 | 4 |
|
19 | 5 | // main is the entry point of the commit message generator |
20 | 6 | func main() { |
21 | | - // Validate COMMIT_LLM and required API keys |
22 | | - commitLLM := os.Getenv("COMMIT_LLM") |
23 | | - var apiKey string |
24 | | - |
25 | | - switch commitLLM { |
26 | | - case "google": |
27 | | - apiKey = os.Getenv("GOOGLE_API_KEY") |
28 | | - if apiKey == "" { |
29 | | - log.Fatalf("GOOGLE_API_KEY is not set") |
30 | | - } |
31 | | - case "grok": |
32 | | - apiKey = os.Getenv("GROK_API_KEY") |
33 | | - if apiKey == "" { |
34 | | - log.Fatalf("GROK_API_KEY is not set") |
35 | | - } |
36 | | - case "chatgpt": |
37 | | - apiKey = os.Getenv("OPENAI_API_KEY") |
38 | | - if apiKey == "" { |
39 | | - log.Fatalf("OPENAI_API_KEY is not set") |
40 | | - } |
41 | | - case "claude": |
42 | | - apiKey = os.Getenv("CLAUDE_API_KEY") |
43 | | - if apiKey == "" { |
44 | | - log.Fatalf("CLAUDE_API_KEY is not set") |
45 | | - } |
46 | | - default: |
47 | | - log.Fatalf("Invalid COMMIT_LLM value: %s", commitLLM) |
48 | | - } |
49 | | - |
50 | | - // Get current directory |
51 | | - currentDir, err := os.Getwd() |
52 | | - if err != nil { |
53 | | - log.Fatalf("Failed to get current directory: %v", err) |
54 | | - } |
55 | | - |
56 | | - // Check if current directory is a git repository |
57 | | - if !git.IsRepository(currentDir) { |
58 | | - log.Fatalf("Current directory is not a Git repository: %s", currentDir) |
59 | | - } |
60 | | - |
61 | | - // Create a minimal config for the API |
62 | | - config := &types.Config{ |
63 | | - GrokAPI: "https://api.x.ai/v1/chat/completions", |
64 | | - } |
65 | | - |
66 | | - // Create a repo config for the current directory |
67 | | - repoConfig := types.RepoConfig{ |
68 | | - Path: currentDir, |
69 | | - } |
70 | | - |
71 | | - // Get file statistics before fetching changes |
72 | | - fileStats, err := stats.GetFileStatistics(&repoConfig) |
73 | | - if err != nil { |
74 | | - log.Fatalf("Failed to get file statistics: %v", err) |
75 | | - } |
76 | | - |
77 | | - // Display header |
78 | | - pterm.DefaultHeader.WithFullWidth(). |
79 | | - WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)). |
80 | | - WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)). |
81 | | - Println("🚀 Commit Message Generator") |
82 | | - |
83 | | - pterm.Println() |
84 | | - |
85 | | - // Display file statistics with icons |
86 | | - display.ShowFileStatistics(fileStats) |
87 | | - |
88 | | - if fileStats.TotalFiles == 0 { |
89 | | - pterm.Warning.Println("No changes detected in the Git repository.") |
90 | | - return |
91 | | - } |
92 | | - |
93 | | - // Get the changes |
94 | | - changes, err := git.GetChanges(&repoConfig) |
95 | | - if err != nil { |
96 | | - log.Fatalf("Failed to get Git changes: %v", err) |
97 | | - } |
98 | | - |
99 | | - if len(changes) == 0 { |
100 | | - pterm.Warning.Println("No changes detected in the Git repository.") |
101 | | - return |
102 | | - } |
103 | | - |
104 | | - pterm.Println() |
105 | | - |
106 | | - // Show generating spinner |
107 | | - spinnerGenerating, err := pterm.DefaultSpinner. |
108 | | - WithSequence("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"). |
109 | | - Start("🤖 Generating commit message...") |
110 | | - if err != nil { |
111 | | - log.Fatalf("Failed to start spinner: %v", err) |
112 | | - } |
113 | | - |
114 | | - var commitMsg string |
115 | | - if os.Getenv("COMMIT_LLM") == "google" { |
116 | | - commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey) |
117 | | - } else if os.Getenv("COMMIT_LLM") == "chatgpt" { |
118 | | - commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey) |
119 | | - } else if os.Getenv("COMMIT_LLM") == "claude" { |
120 | | - commitMsg, err = claude.GenerateCommitMessage(config, changes, apiKey) |
121 | | - } else { |
122 | | - commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey) |
123 | | - } |
124 | | - |
125 | | - if err != nil { |
126 | | - spinnerGenerating.Fail("Failed to generate commit message") |
127 | | - log.Fatalf("Error: %v", err) |
128 | | - } |
129 | | - |
130 | | - spinnerGenerating.Success("✅ Commit message generated successfully!") |
131 | | - |
132 | | - pterm.Println() |
133 | | - |
134 | | - // Display the commit message in a styled panel |
135 | | - display.ShowCommitMessage(commitMsg) |
136 | | - |
137 | | - // Copy to clipboard |
138 | | - err = clipboard.WriteAll(commitMsg) |
139 | | - if err != nil { |
140 | | - pterm.Warning.Printf("⚠️ Could not copy to clipboard: %v\n", err) |
141 | | - } else { |
142 | | - pterm.Success.Println("📋 Commit message copied to clipboard!") |
143 | | - } |
144 | | - |
145 | | - pterm.Println() |
146 | | - |
147 | | - // Display changes preview |
148 | | - display.ShowChangesPreview(fileStats) |
| 7 | + cmd.Execute() |
149 | 8 | } |
0 commit comments