|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/javoire/stackinator/internal/git" |
| 12 | + "github.com/javoire/stackinator/internal/ui" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var configStdin io.Reader = os.Stdin |
| 17 | + |
| 18 | +var configCmd = &cobra.Command{ |
| 19 | + Use: "config", |
| 20 | + Short: "Show stackinator configuration for this repository", |
| 21 | + Long: `Show stackinator configuration for this repository. |
| 22 | +
|
| 23 | +Use 'stack config set' to update settings.`, |
| 24 | + Run: func(cmd *cobra.Command, args []string) { |
| 25 | + gitClient := git.NewGitClient() |
| 26 | + if err := runConfigShow(gitClient); err != nil { |
| 27 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +var configGetCmd = &cobra.Command{ |
| 34 | + Use: "get", |
| 35 | + Short: "Show stackinator configuration for this repository", |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + gitClient := git.NewGitClient() |
| 38 | + if err := runConfigShow(gitClient); err != nil { |
| 39 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +var configSetCmd = &cobra.Command{ |
| 46 | + Use: "set", |
| 47 | + Short: "Interactively configure stackinator settings", |
| 48 | + Long: `Interactively configure stackinator settings. |
| 49 | +
|
| 50 | +Currently supports the worktrees directory location.`, |
| 51 | + Run: func(cmd *cobra.Command, args []string) { |
| 52 | + gitClient := git.NewGitClient() |
| 53 | + if err := runConfigSet(gitClient); err != nil { |
| 54 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +func init() { |
| 61 | + configCmd.AddCommand(configGetCmd) |
| 62 | + configCmd.AddCommand(configSetCmd) |
| 63 | +} |
| 64 | + |
| 65 | +func runConfigShow(gitClient git.GitClient) error { |
| 66 | + configured := strings.TrimSpace(gitClient.GetConfig("stack.worktreesDir")) |
| 67 | + if configured == "" { |
| 68 | + configured = "(default)" |
| 69 | + } |
| 70 | + |
| 71 | + effective, err := getWorktreesBaseDir(gitClient) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Println("Worktrees directory:") |
| 77 | + fmt.Printf(" Configured: %s\n", configured) |
| 78 | + fmt.Printf(" Effective: %s\n", effective) |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func runConfigSet(gitClient git.GitClient) error { |
| 84 | + defaultDir, err := getDefaultWorktreesBaseDir() |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + repoRoot, err := gitClient.GetRepoRoot() |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + projectDir := filepath.Join(repoRoot, ".worktrees") |
| 95 | + |
| 96 | + fmt.Println("Choose worktrees directory:") |
| 97 | + fmt.Printf(" 1) %s (default)\n", defaultDir) |
| 98 | + fmt.Printf(" 2) %s (this repo)\n", projectDir) |
| 99 | + fmt.Printf("Select [1/2] (default 1): ") |
| 100 | + |
| 101 | + reader := bufio.NewReader(configStdin) |
| 102 | + input, err := reader.ReadString('\n') |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to read input: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + choice := strings.TrimSpace(input) |
| 108 | + if choice == "" { |
| 109 | + choice = "1" |
| 110 | + } |
| 111 | + |
| 112 | + var configValue string |
| 113 | + switch choice { |
| 114 | + case "1": |
| 115 | + configValue = "~/.stack/worktrees" |
| 116 | + case "2": |
| 117 | + configValue = ".worktrees" |
| 118 | + default: |
| 119 | + return fmt.Errorf("invalid selection: %s", choice) |
| 120 | + } |
| 121 | + |
| 122 | + if err := gitClient.SetConfig("stack.worktreesDir", configValue); err != nil { |
| 123 | + return fmt.Errorf("failed to set worktrees directory: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + if !dryRun { |
| 127 | + fmt.Println(ui.Success(fmt.Sprintf("Worktrees directory set to %s", configValue))) |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
0 commit comments