Skip to content

Commit e7549d4

Browse files
committed
feat: add init command for environment validation
- Add 'codes init' command to check environment setup - Verify Claude CLI installation and version - Validate configuration file existence and format - Test default configuration API connectivity - Display all configurations with their status - Provide actionable suggestions when checks fail This command helps users quickly diagnose setup issues and ensures their environment is ready to use codes CLI. Updated README with init command documentation and examples.
1 parent fc78600 commit e7549d4

File tree

4 files changed

+174
-3
lines changed

4 files changed

+174
-3
lines changed

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ make build
5656

5757
## Quick Start
5858

59-
### 1. Add Your First Configuration
59+
### 1. Check Your Environment (Optional but Recommended)
60+
61+
```bash
62+
codes init
63+
```
64+
65+
This will verify that everything is set up correctly and guide you if something is missing.
66+
67+
### 2. Add Your First Configuration
6068

6169
```bash
6270
codes add
@@ -69,15 +77,15 @@ You'll be prompted to enter:
6977

7078
The tool will automatically test the API connection before saving.
7179

72-
### 2. Run Claude with Your Configuration
80+
### 3. Run Claude with Your Configuration
7381

7482
```bash
7583
codes
7684
```
7785

7886
This will launch Claude CLI with the selected configuration's environment variables.
7987

80-
### 3. Switch Between Configurations
88+
### 4. Switch Between Configurations
8189

8290
```bash
8391
codes select
@@ -87,6 +95,29 @@ An interactive menu will appear showing all your configurations. Select one to s
8795

8896
## Commands
8997

98+
### `codes init`
99+
100+
Check your environment and validate your configuration. This command performs comprehensive checks including:
101+
102+
- Verifies Claude CLI installation
103+
- Checks configuration file existence and validity
104+
- Tests API connectivity for the default configuration
105+
- Displays detailed status of all configurations
106+
107+
```bash
108+
codes init
109+
```
110+
111+
**Example output:**
112+
```
113+
✓ Claude CLI is installed
114+
✓ Configuration file exists
115+
✓ Found 3 configuration(s)
116+
✓ Default configuration is working
117+
```
118+
119+
This is a great command to run after installation or when troubleshooting issues.
120+
90121
### `codes` (no arguments)
91122

92123
Runs Claude CLI with the currently selected configuration. If Claude CLI is not installed, it will be automatically installed.

cmd/codes/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var rootCmd = &cobra.Command{
1616
}
1717

1818
func init() {
19+
rootCmd.AddCommand(commands.InitCmd)
1920
rootCmd.AddCommand(commands.InstallCmd)
2021
rootCmd.AddCommand(commands.AddCmd)
2122
rootCmd.AddCommand(commands.SelectCmd)

internal/commands/cobra.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import (
88
"codes/internal/ui"
99
)
1010

11+
// InitCmd represents the init command
12+
var InitCmd = &cobra.Command{
13+
Use: "init",
14+
Short: "Check environment and configuration",
15+
Long: "Verify Claude CLI installation and validate configuration files",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
RunInit()
18+
},
19+
}
20+
1121
// InstallCmd represents the install command
1222
var InstallCmd = &cobra.Command{
1323
Use: "install",

internal/commands/commands.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,135 @@ func installClaude(version string) {
360360
ui.ShowSuccess("Claude installed successfully!")
361361
}
362362

363+
func RunInit() {
364+
ui.ShowHeader("Codes CLI Environment Check")
365+
fmt.Println()
366+
367+
allGood := true
368+
369+
// 1. Check if Claude CLI is installed
370+
ui.ShowInfo("Checking Claude CLI installation...")
371+
if _, err := exec.LookPath("claude"); err != nil {
372+
ui.ShowError("✗ Claude CLI not found", nil)
373+
ui.ShowWarning(" Run 'codes update' to install Claude CLI")
374+
allGood = false
375+
} else {
376+
ui.ShowSuccess("✓ Claude CLI is installed")
377+
378+
// Check Claude version
379+
cmd := exec.Command("claude", "--version")
380+
output, err := cmd.Output()
381+
if err == nil {
382+
version := strings.TrimSpace(string(output))
383+
ui.ShowInfo(" Version: %s", version)
384+
}
385+
}
386+
fmt.Println()
387+
388+
// 2. Check if config file exists
389+
ui.ShowInfo("Checking configuration file...")
390+
if _, err := os.Stat(config.ConfigPath); err != nil {
391+
ui.ShowError("✗ Configuration file not found", nil)
392+
ui.ShowInfo(" Expected location: %s", config.ConfigPath)
393+
ui.ShowWarning(" Run 'codes add' to create your first configuration")
394+
allGood = false
395+
} else {
396+
ui.ShowSuccess("✓ Configuration file exists")
397+
ui.ShowInfo(" Location: %s", config.ConfigPath)
398+
399+
// 3. Validate configuration
400+
cfg, err := config.LoadConfig()
401+
if err != nil {
402+
ui.ShowError("✗ Failed to load configuration", err)
403+
ui.ShowWarning(" Your config file may be corrupted")
404+
allGood = false
405+
} else {
406+
if len(cfg.Configs) == 0 {
407+
ui.ShowWarning("✗ No configurations found in config file")
408+
ui.ShowWarning(" Run 'codes add' to add a configuration")
409+
allGood = false
410+
} else {
411+
ui.ShowSuccess("✓ Found %d configuration(s)", len(cfg.Configs))
412+
413+
// Show configurations with status
414+
fmt.Println()
415+
ui.ShowInfo("Configurations:")
416+
for i, c := range cfg.Configs {
417+
isDefault := ""
418+
if c.Name == cfg.Default {
419+
isDefault = " (default)"
420+
}
421+
422+
statusIcon := "?"
423+
statusText := "unknown"
424+
if c.Status == "active" {
425+
statusIcon = "✓"
426+
statusText = "active"
427+
} else if c.Status == "inactive" {
428+
statusIcon = "✗"
429+
statusText = "inactive"
430+
}
431+
432+
fmt.Printf(" %d. %s %s%s - %s [%s]\n",
433+
i+1, statusIcon, c.Name, isDefault, c.AnthropicBaseURL, statusText)
434+
}
435+
436+
// 4. Test default configuration
437+
if cfg.Default != "" {
438+
fmt.Println()
439+
ui.ShowInfo("Testing default configuration '%s'...", cfg.Default)
440+
441+
var defaultConfig *config.APIConfig
442+
for i := range cfg.Configs {
443+
if cfg.Configs[i].Name == cfg.Default {
444+
defaultConfig = &cfg.Configs[i]
445+
break
446+
}
447+
}
448+
449+
if defaultConfig != nil {
450+
if config.TestAPIConfig(*defaultConfig) {
451+
ui.ShowSuccess("✓ Default configuration is working")
452+
} else {
453+
ui.ShowWarning("✗ Default configuration test failed")
454+
ui.ShowWarning(" API may be unreachable or credentials may be invalid")
455+
ui.ShowInfo(" Run 'codes add' to add a new configuration")
456+
allGood = false
457+
}
458+
} else {
459+
ui.ShowWarning("✗ Default configuration '%s' not found", cfg.Default)
460+
ui.ShowWarning(" Run 'codes select' to choose a valid configuration")
461+
allGood = false
462+
}
463+
}
464+
}
465+
}
466+
}
467+
468+
fmt.Println()
469+
ui.ShowInfo("─────────────────────────────────")
470+
fmt.Println()
471+
472+
if allGood {
473+
ui.ShowSuccess("✓ All checks passed! You're ready to use codes.")
474+
fmt.Println()
475+
ui.ShowInfo("Quick commands:")
476+
ui.ShowInfo(" codes - Run Claude with current configuration")
477+
ui.ShowInfo(" codes select - Switch between configurations")
478+
ui.ShowInfo(" codes add - Add a new configuration")
479+
} else {
480+
ui.ShowWarning("⚠ Some checks failed. Please review the messages above.")
481+
fmt.Println()
482+
ui.ShowInfo("Suggested actions:")
483+
if _, err := exec.LookPath("claude"); err != nil {
484+
ui.ShowInfo(" 1. Install Claude CLI: codes update")
485+
}
486+
if _, err := os.Stat(config.ConfigPath); err != nil {
487+
ui.ShowInfo(" 2. Add a configuration: codes add")
488+
}
489+
}
490+
}
491+
363492
func checkForUpdates() {
364493
// 检查codes CLI更新
365494
go func() {

0 commit comments

Comments
 (0)