diff --git a/root/init.go b/root/init.go index 6421dbf..9c24429 100644 --- a/root/init.go +++ b/root/init.go @@ -2,6 +2,10 @@ package root import ( "fmt" + "os" + "path/filepath" + "strings" + "github.com/spf13/cobra" ) @@ -38,4 +42,61 @@ end func init() { rootCmd.AddCommand(initCmd) + rootCmd.AddCommand(setupCmd) +} + +var setupCmd = &cobra.Command{ + Use: "setup", + Short: "Automatically setup iris shell integration and install binary", + Run: func(cmd *cobra.Command, args []string) { + home, _ := os.UserHomeDir() + + localBin := filepath.Join(home, ".local", "bin") + _ = os.MkdirAll(localBin, 0755) + + exe, _ := os.Executable() + targetExe := filepath.Join(localBin, "iris") + + fmt.Printf("Installing iris to %s...\n", targetExe) + input, _ := os.ReadFile(exe) + _ = os.WriteFile(targetExe, input, 0755) + + shellPath := os.Getenv("SHELL") + shellName := filepath.Base(shellPath) + var configFile string + var evalCmd string + + switch shellName { + case "zsh": + configFile = filepath.Join(home, ".zshrc") + evalCmd = `eval "$(iris init zsh)"` + case "bash": + configFile = filepath.Join(home, ".bashrc") + evalCmd = `eval "$(iris init bash)"` + case "fish": + configFile = filepath.Join(home, ".config", "fish", "config.fish") + evalCmd = `iris init fish | source` + default: + fmt.Printf("Unsupported shell: %s. Please add iris init manually.\n", shellName) + return + } + + content, _ := os.ReadFile(configFile) + if strings.Contains(string(content), "iris init") { + fmt.Printf("Iris is already configured in %s\n", configFile) + } else { + f, err := os.OpenFile(configFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + fmt.Printf("Failed to update %s: %v\n", configFile, err) + return + } + defer f.Close() + + f.WriteString("\n# Iris Autocomplete\n" + evalCmd + "\n") + fmt.Printf("✓ Added iris integration to %s\n", configFile) + } + + fmt.Println("\nSetup complete! Please restart your terminal or run:") + fmt.Printf(" source %s\n", configFile) + }, }