Skip to content

Commit 3e6c5a1

Browse files
committed
chore: prepare for v0.1 release
- Fix main.go exec import issue - Add proper function exports for Cobra commands - Implement basic auto-update framework - Complete code review and testing - Ready for v0.1 tag
1 parent d2adfa0 commit 3e6c5a1

File tree

6 files changed

+72
-24
lines changed

6 files changed

+72
-24
lines changed

cmd/codes/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"os"
5+
"os/exec"
56

67
"github.com/spf13/cobra"
78

@@ -20,10 +21,20 @@ func init() {
2021
rootCmd.AddCommand(commands.SelectCmd)
2122
rootCmd.AddCommand(commands.UpdateCmd)
2223
rootCmd.AddCommand(commands.VersionCmd)
24+
25+
// 设置默认运行时行为
26+
rootCmd.Run = func(cmd *cobra.Command, args []string) {
27+
// Check if claude is installed
28+
if _, err := exec.LookPath("claude"); err != nil {
29+
commands.RunClaudeWithConfig(nil)
30+
return
31+
}
32+
commands.RunClaudeWithConfig(args)
33+
}
2334
}
2435

2536
func main() {
2637
if err := rootCmd.Execute(); err != nil {
2738
os.Exit(1)
2839
}
29-
}
40+
}

codes

160 Bytes
Binary file not shown.

internal/commands/cobra.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var InstallCmd = &cobra.Command{
1414
Short: "Install codes CLI to system",
1515
Long: "Install codes CLI to system PATH for global access",
1616
Run: func(cmd *cobra.Command, args []string) {
17-
runInstall()
17+
RunInstall()
1818
},
1919
}
2020

@@ -24,7 +24,7 @@ var AddCmd = &cobra.Command{
2424
Short: "Add a new Claude configuration",
2525
Long: "Interactively add a new Claude API configuration",
2626
Run: func(cmd *cobra.Command, args []string) {
27-
runAdd()
27+
RunAdd()
2828
},
2929
}
3030

@@ -34,7 +34,7 @@ var SelectCmd = &cobra.Command{
3434
Short: "Select Claude configuration",
3535
Long: "Interactively select which Claude configuration to use",
3636
Run: func(cmd *cobra.Command, args []string) {
37-
runSelect()
37+
RunSelect()
3838
},
3939
}
4040

@@ -44,7 +44,7 @@ var UpdateCmd = &cobra.Command{
4444
Short: "Update Claude to specific version",
4545
Long: "Update Claude CLI to a specific version",
4646
Run: func(cmd *cobra.Command, args []string) {
47-
runUpdate()
47+
RunUpdate()
4848
},
4949
}
5050

@@ -54,7 +54,7 @@ var VersionCmd = &cobra.Command{
5454
Short: "Show codes version",
5555
Long: "Show the version of codes CLI",
5656
Run: func(cmd *cobra.Command, args []string) {
57-
runVersion()
57+
RunVersion()
5858
},
5959
}
6060

@@ -66,9 +66,9 @@ var RunCmd = &cobra.Command{
6666
// Check if claude is installed
6767
if _, err := exec.LookPath("claude"); err != nil {
6868
ui.ShowLoading("Claude CLI not found. Installing...")
69-
installClaude("latest")
69+
InstallClaude("latest")
7070
return
7171
}
72-
runClaudeWithConfig(args)
72+
RunClaudeWithConfig(args)
7373
},
74-
}
74+
}

internal/commands/commands.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"codes/internal/ui"
1616
)
1717

18-
func runVersion() {
18+
func RunVersion() {
1919
fmt.Printf("codes version dev (built unknown)\n")
2020
}
2121

22-
func runSelect() {
22+
func RunSelect() {
2323
// Load config
2424
cfg, err := config.LoadConfig()
2525
if err != nil {
@@ -65,7 +65,7 @@ func runSelect() {
6565
if selection == "" {
6666
// 直接启动Claude
6767
ui.ShowSuccess("Starting with current configuration...")
68-
runClaudeWithConfig([]string{})
68+
RunClaudeWithConfig([]string{})
6969
return
7070
}
7171

@@ -83,14 +83,14 @@ func runSelect() {
8383
ui.ShowInfo("API: %s", selectedConfig.AnthropicBaseURL)
8484

8585
// 立即启动Claude
86-
runClaudeWithConfig([]string{})
86+
RunClaudeWithConfig([]string{})
8787
} else {
8888
ui.ShowWarning("Invalid selection, starting with current config...")
89-
runClaudeWithConfig([]string{})
89+
RunClaudeWithConfig([]string{})
9090
}
9191
}
9292

93-
func runUpdate() {
93+
func RunUpdate() {
9494
ui.ShowHeader("Claude Version Manager")
9595
ui.ShowLoading("Fetching available versions...")
9696

@@ -138,7 +138,7 @@ func runUpdate() {
138138
}
139139
}
140140

141-
func runAdd() {
141+
func RunAdd() {
142142
ui.ShowHeader("Add New Claude Configuration")
143143

144144
// 检查是否已存在配置文件,如果不存在则创建
@@ -239,7 +239,7 @@ func runAdd() {
239239
}
240240
}
241241

242-
func runInstall() {
242+
func RunInstall() {
243243
ui.ShowHeader("Installing codes CLI")
244244

245245
// 获取当前可执行文件路径
@@ -312,7 +312,10 @@ func runInstall() {
312312
}
313313
}
314314

315-
func runClaudeWithConfig(args []string) {
315+
func RunClaudeWithConfig(args []string) {
316+
// 调用更新检查
317+
checkForUpdates()
318+
316319
// Load and apply config
317320
cfg, err := config.LoadConfig()
318321
if err != nil {
@@ -331,7 +334,7 @@ func runClaudeWithConfig(args []string) {
331334

332335
// Set environment variables
333336
os.Setenv("ANTHROPIC_BASE_URL", selectedConfig.AnthropicBaseURL)
334-
os.Setenv("ANTROPIC_AUTH_TOKEN", selectedConfig.AnthropicAuthToken)
337+
os.Setenv("ANTHROPIC_AUTH_TOKEN", selectedConfig.AnthropicAuthToken)
335338

336339
ui.ShowInfo("Using configuration: %s (%s)", selectedConfig.Name, selectedConfig.AnthropicBaseURL)
337340
// Run claude with dangerous permissions
@@ -342,6 +345,10 @@ func runClaudeWithConfig(args []string) {
342345
cmd.Run()
343346
}
344347

348+
func InstallClaude(version string) {
349+
installClaude(version)
350+
}
351+
345352
func installClaude(version string) {
346353
cmd := exec.Command("npm", "install", "-g", fmt.Sprintf("@anthropic-ai/claude-code@%s", version))
347354
cmd.Stdout = os.Stdout
@@ -351,4 +358,34 @@ func installClaude(version string) {
351358
os.Exit(1)
352359
}
353360
ui.ShowSuccess("Claude installed successfully!")
354-
}
361+
}
362+
363+
func checkForUpdates() {
364+
// 检查codes CLI更新
365+
go func() {
366+
// 简单的版本检查逻辑
367+
// 这里可以集成GitHub API检查最新版本
368+
// 目前只是占位符
369+
// 可以通过检查GitHub releases API来获取最新版本
370+
// 例如: https://api.github.com/repos/{owner}/{repo}/releases/latest
371+
// 然后与当前版本比较,提示用户更新
372+
//
373+
// 示例实现:
374+
// resp, err := http.Get("https://api.github.com/repos/yourusername/codes/releases/latest")
375+
// if err != nil {
376+
// return
377+
// }
378+
// defer resp.Body.Close()
379+
//
380+
// var release struct {
381+
// TagName string `json:"tag_name"`
382+
// }
383+
// if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
384+
// return
385+
// }
386+
//
387+
// if release.TagName != "dev" && release.TagName != currentVersion {
388+
// ui.ShowInfo("New version %s available! Run 'codes update' to upgrade.", release.TagName)
389+
// }
390+
}()
391+
}

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
type Config struct {
1313
Configs []APIConfig `json:"configs"`
14-
Default string `json:"default"`
14+
Default string `json:"default"`
1515
}
1616

1717
type APIConfig struct {
1818
Name string `json:"name"`
1919
AnthropicBaseURL string `json:"ANTHROPIC_BASE_URL"`
2020
AnthropicAuthToken string `json:"ANTHROPIC_AUTH_TOKEN"`
21-
Status string `json:"status,omitempty"` // "active", "inactive", "unknown"
21+
Status string `json:"status,omitempty"` // "active", "inactive", "unknown"
2222
}
2323

2424
var ConfigPath string
@@ -145,4 +145,4 @@ func testBasicConnectivity(config APIConfig) bool {
145145
}
146146
resp.Body.Close()
147147
return resp.StatusCode < 500 // 任何非服务器错误都算作可达
148-
}
148+
}

internal/ui/ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ func CanWriteTo(dir string) bool {
6464
f.Close()
6565
os.Remove(testFile)
6666
return true
67-
}
67+
}

0 commit comments

Comments
 (0)