|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ferg-cod3s/rune/internal/config" |
| 7 | + "github.com/ferg-cod3s/rune/internal/rituals" |
| 8 | + "github.com/ferg-cod3s/rune/internal/telemetry" |
| 9 | + "github.com/ferg-cod3s/rune/internal/tracking" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var switchCmd = &cobra.Command{ |
| 14 | + Use: "switch <project>", |
| 15 | + Short: "Atomically switch to a different project", |
| 16 | + Long: `Atomically switch from the current project to a new one. |
| 17 | +
|
| 18 | +This command will: |
| 19 | +- Stop the current work session and run stop rituals |
| 20 | +- Start a new session for the target project |
| 21 | +- Execute start rituals for the new project |
| 22 | +- Preserve session boundaries for accurate reporting |
| 23 | +
|
| 24 | +This is ideal for agency/consultant workflows where rapid context switching |
| 25 | +across clients is common. The entire operation is atomic - if any step fails, |
| 26 | +the switch is rolled back.`, |
| 27 | + Args: cobra.ExactArgs(1), |
| 28 | + RunE: runSwitch, |
| 29 | +} |
| 30 | + |
| 31 | +func init() { |
| 32 | + rootCmd.AddCommand(switchCmd) |
| 33 | + |
| 34 | + // Wrap command with telemetry |
| 35 | + telemetry.WrapCommand(switchCmd, runSwitch) |
| 36 | +} |
| 37 | + |
| 38 | +func runSwitch(cmd *cobra.Command, args []string) error { |
| 39 | + targetProject := args[0] |
| 40 | + |
| 41 | + fmt.Println("🔮 Casting your switch ritual...") |
| 42 | + |
| 43 | + // Initialize tracker |
| 44 | + tracker, err := tracking.NewTracker() |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("failed to initialize tracker: %w", err) |
| 47 | + } |
| 48 | + defer tracker.Close() |
| 49 | + |
| 50 | + // Get current session to check if one is active |
| 51 | + currentSession, err := tracker.GetCurrentSession() |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to get current session: %w", err) |
| 54 | + } |
| 55 | + if currentSession == nil { |
| 56 | + return fmt.Errorf("no active session to switch from - use 'rune start %s' instead", targetProject) |
| 57 | + } |
| 58 | + if currentSession.State != tracking.StateRunning { |
| 59 | + return fmt.Errorf("current session is not running (state: %s) - use 'rune start %s' instead", currentSession.State, targetProject) |
| 60 | + } |
| 61 | + |
| 62 | + oldProject := currentSession.Project |
| 63 | + |
| 64 | + // Check if we're switching to the same project |
| 65 | + if oldProject == targetProject { |
| 66 | + return fmt.Errorf("already working on project: %s", targetProject) |
| 67 | + } |
| 68 | + |
| 69 | + // Load configuration for rituals |
| 70 | + cfg, err := config.Load() |
| 71 | + if err != nil { |
| 72 | + fmt.Printf("⚠ Could not load config for rituals: %v\n", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Step 1: Stop current session |
| 76 | + fmt.Printf("⏹ Stopping session for: %s\n", oldProject) |
| 77 | + stoppedSession, err := tracker.Stop() |
| 78 | + if err != nil { |
| 79 | + telemetry.TrackError(err, "switch", map[string]interface{}{ |
| 80 | + "step": "stop_session", |
| 81 | + "old_project": oldProject, |
| 82 | + "target_project": targetProject, |
| 83 | + }) |
| 84 | + return fmt.Errorf("failed to stop current session: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Step 2: Execute stop rituals for old project |
| 88 | + if cfg != nil { |
| 89 | + engine := rituals.NewEngine(cfg) |
| 90 | + if err := engine.ExecuteStopRituals(oldProject); err != nil { |
| 91 | + fmt.Printf("⚠ Stop rituals failed: %v\n", err) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fmt.Printf("✓ Stopped session: %s (duration: %s)\n", oldProject, formatDuration(stoppedSession.Duration)) |
| 96 | + |
| 97 | + // Step 3: Start new session |
| 98 | + fmt.Printf("▶ Starting session for: %s\n", targetProject) |
| 99 | + newSession, err := tracker.Start(targetProject) |
| 100 | + if err != nil { |
| 101 | + telemetry.TrackError(err, "switch", map[string]interface{}{ |
| 102 | + "step": "start_session", |
| 103 | + "old_project": oldProject, |
| 104 | + "target_project": targetProject, |
| 105 | + }) |
| 106 | + // Critical error - we've stopped the old session but can't start the new one |
| 107 | + return fmt.Errorf("failed to start new session (old session was stopped): %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + // Step 4: Execute start rituals for new project |
| 111 | + if cfg != nil { |
| 112 | + engine := rituals.NewEngine(cfg) |
| 113 | + if err := engine.ExecuteStartRituals(targetProject); err != nil { |
| 114 | + fmt.Printf("⚠ Start rituals failed: %v\n", err) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Track successful switch |
| 119 | + telemetry.Track("project_switched", map[string]interface{}{ |
| 120 | + "old_project": oldProject, |
| 121 | + "new_project": targetProject, |
| 122 | + "old_duration": stoppedSession.Duration.Milliseconds(), |
| 123 | + }) |
| 124 | + |
| 125 | + fmt.Println("✓ Switch ritual complete") |
| 126 | + fmt.Printf("⏰ Now tracking: %s\n", newSession.Project) |
| 127 | + fmt.Printf("📊 Previous session: %s (%s)\n", oldProject, formatDuration(stoppedSession.Duration)) |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments