claude-code-switcher 0.11.2

A CLI tool for managing Claude Code setting snapshots and templates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Snapshot selector using the unified selector framework

use crate::selectors::{
    base::{SelectableItem, SelectionResult, Selector, SelectorConfig, prompt_rename},
    confirmation::ConfirmationService,
    error::{SelectorError, SelectorResult},
};
use crate::{
    Configurable,
    settings::{ClaudeSettings, format_settings_for_display},
    snapshots::{Snapshot, SnapshotScope, SnapshotStore},
    utils::get_snapshots_dir,
};
use std::io::Write;

/// Action for snapshot management
#[derive(Debug, Clone)]
pub enum SnapshotManagementAction {
    Apply(usize),
    Delete(usize),
    Rename(usize),
    CreateSnapshot,
    Back,
    Exit,
}

/// Snapshot selector using the unified framework
pub struct SnapshotSelector {
    snapshots: Vec<Snapshot>,
    store: SnapshotStore,
}

/// Display wrapper for snapshots
#[derive(Clone, Debug)]
struct SnapshotDisplayItem {
    index: usize,
    snapshot: Snapshot,
}

impl SelectableItem for SnapshotDisplayItem {
    fn display_name(&self) -> String {
        format!("{} ({})", self.snapshot.name, self.snapshot.scope)
    }

    fn format_for_list(&self) -> String {
        self.display_name()
    }

    fn id(&self) -> Option<String> {
        Some(self.snapshot.id.clone())
    }
}

impl SnapshotSelector {
    /// Create a new snapshot selector
    pub fn new() -> SelectorResult<Self> {
        let snapshots_dir = get_snapshots_dir();
        let store = SnapshotStore::new(snapshots_dir);
        let snapshots = store
            .list()
            .map_err(|e| SelectorError::Storage(format!("Failed to load snapshots: {}", e)))?;

        Ok(Self { snapshots, store })
    }

    /// Run interactive snapshot management
    pub fn run_management(&mut self) -> SelectorResult<()> {
        if self.snapshots.is_empty() {
            println!("No snapshots found. Let's create your first snapshot!");
            if self.create_snapshot()? {
                self.snapshots = self.store.list().map_err(|e| {
                    SelectorError::Storage(format!("Failed to reload snapshots: {}", e))
                })?;
            }

            if self.snapshots.is_empty() {
                return Ok(());
            }
        }

        loop {
            match self.select_snapshot_action()? {
                Some(SnapshotManagementAction::Apply(index)) => {
                    if self.apply_snapshot(index)? {
                        break;
                    }
                }
                Some(SnapshotManagementAction::Delete(index)) => {
                    if self.delete_snapshot(index)? && index < self.snapshots.len() {
                        self.snapshots.remove(index);
                    }
                }
                Some(SnapshotManagementAction::CreateSnapshot) => {
                    if self.create_snapshot()? {
                        self.snapshots = self.store.list().map_err(|e| {
                            SelectorError::Storage(format!("Failed to reload snapshots: {}", e))
                        })?;
                    }
                }
                Some(SnapshotManagementAction::Rename(index)) => {
                    if let Some(true) = self.rename_snapshot(index)? {
                        self.snapshots = self.store.list().map_err(|e| {
                            SelectorError::Storage(format!("Failed to reload snapshots: {}", e))
                        })?;
                    }
                }
                Some(SnapshotManagementAction::Back) => continue,
                Some(SnapshotManagementAction::Exit) => break,
                None => break,
            }

            if self.snapshots.is_empty() {
                println!("No more snapshots found.");
                break;
            }
        }

        Ok(())
    }

    /// Simple snapshot selection (for applying snapshots)
    pub fn select_snapshot() -> SelectorResult<Option<Snapshot>> {
        let selector = Self::new()?;

        if selector.snapshots.is_empty() {
            println!("No snapshots available.");
            return Ok(None);
        }

        let items: Vec<SnapshotDisplayItem> = selector
            .snapshots
            .iter()
            .enumerate()
            .map(|(i, s)| SnapshotDisplayItem {
                index: i,
                snapshot: s.clone(),
            })
            .collect();

        let config = SelectorConfig {
            allow_management: false,
            ..SelectorConfig::default()
        };

        let mut sel = Selector::new("Select a snapshot to apply:", items).with_config(config);

        match sel.prompt()? {
            SelectionResult::Selected(item) => Ok(Some(item.snapshot)),
            SelectionResult::Back => Ok(None),
            SelectionResult::Exit => {
                println!("Operation cancelled.");
                std::process::exit(0);
            }
            _ => Ok(None),
        }
    }

    /// Select snapshot action using the selector framework
    fn select_snapshot_action(&mut self) -> SelectorResult<Option<SnapshotManagementAction>> {
        let snapshot_items: Vec<SnapshotDisplayItem> = self
            .snapshots
            .iter()
            .enumerate()
            .map(|(i, s)| SnapshotDisplayItem {
                index: i,
                snapshot: s.clone(),
            })
            .collect();

        let title = format!(
            "Select a snapshot to manage ({} total):",
            self.snapshots.len()
        );

        let config = SelectorConfig {
            allow_create: true,
            show_filter: true,
            ..SelectorConfig::default()
        };

        let mut selector = Selector::new(&title, snapshot_items).with_config(config);

        match selector.prompt()? {
            SelectionResult::Selected(item) | SelectionResult::ViewDetails(item) => {
                // Clear screen before showing details
                print!("\x1b[2J\x1b[H");
                std::io::stdout().flush().ok();
                self.manage_snapshot(item.index).map(Some)
            }
            SelectionResult::Delete(item) => {
                Ok(Some(SnapshotManagementAction::Delete(item.index)))
            }
            SelectionResult::Rename(item) => {
                Ok(Some(SnapshotManagementAction::Rename(item.index)))
            }
            SelectionResult::Create => Ok(Some(SnapshotManagementAction::CreateSnapshot)),
            SelectionResult::Back => Ok(None),
            SelectionResult::Exit => std::process::exit(0),
            _ => Ok(None),
        }
    }

    /// Show snapshot details and action menu
    fn manage_snapshot(&self, index: usize) -> SelectorResult<SnapshotManagementAction> {
        if index >= self.snapshots.len() {
            return Err(SelectorError::NotFound);
        }

        let snapshot = &self.snapshots[index];

        // Print snapshot details
        println!("\n📋 Snapshot: {} ({})", snapshot.name, snapshot.scope);
        println!("  Created: {}", snapshot.created_at);
        println!("  Updated: {}", snapshot.updated_at);
        if let Some(ref desc) = snapshot.description {
            println!("  Description: {}", desc);
        }

        // Show action menu
        let options = vec!["Apply", "Rename", "Delete", "Back"];

        let action = inquire::Select::new(&format!("Action for '{}':", snapshot.name), options)
            .with_help_message("↑/↓: Navigate, Enter: Select, Esc: Back")
            .prompt()
            .map_err(|e| {
                let msg = e.to_string();
                if msg.contains("canceled") || msg.contains("cancelled") {
                    SelectorError::Cancelled
                } else {
                    SelectorError::Failed(format!("Selection failed: {}", e))
                }
            })?;

        match action {
            "Apply" => Ok(SnapshotManagementAction::Apply(index)),
            "Rename" => Ok(SnapshotManagementAction::Rename(index)),
            "Delete" => Ok(SnapshotManagementAction::Delete(index)),
            _ => Ok(SnapshotManagementAction::Back),
        }
    }

    /// Create a new snapshot interactively
    fn create_snapshot(&self) -> SelectorResult<bool> {
        println!("\n📝 Creating a new snapshot...\n");

        // Step 1: Select configuration path
        let config_options = vec![
            "Local (.claude/settings.json) - Project-specific settings",
            "Global (~/.claude/settings.json) - User-wide settings",
        ];

        let config_selection = inquire::Select::new("Select configuration to snapshot:", config_options)
            .with_help_message("↑/↓: Navigate, Enter: Select")
            .prompt()
            .map_err(|e| {
                let msg = e.to_string();
                if msg.contains("canceled") || msg.contains("cancelled") {
                    SelectorError::Cancelled
                } else {
                    SelectorError::Failed(format!("Selection failed: {}", e))
                }
            })?;

        let settings_path = if config_selection.starts_with("Local") {
            crate::utils::get_local_settings_path()
        } else {
            crate::utils::get_settings_path(None)
        };

        // Step 2: Show current configuration preview
        println!("\n📋 Current Configuration Preview:");
        println!("📁 Path: {}", settings_path.display());

        if !settings_path.exists() {
            println!("⚠️  Settings file not found at this location.");

            let continue_confirmation =
                ConfirmationService::confirm_action("Continue creating snapshot anyway?")?;

            if !continue_confirmation {
                println!("Snapshot creation cancelled.");
                return Ok(false);
            }
        } else {
            // Load and display masked settings
            match ClaudeSettings::from_file(&settings_path) {
                Ok(settings) => {
                    let masked_settings = settings.clone().mask_sensitive_data();
                    println!("{}", format_settings_for_display(&masked_settings, false));

                    // Show additional info
                    if let Some(model) = &settings.model {
                        println!("🤖 Model: {}", model);
                    }
                    if let Some(hooks) = &settings.hooks {
                        let hook_count = hooks.pre_command.as_ref().map_or(0, |v| v.len())
                            + hooks.post_command.as_ref().map_or(0, |v| v.len());
                        if hook_count > 0 {
                            println!("🪝 Hooks: {} configured", hook_count);
                        }
                    }
                    if let Some(permissions) = &settings.permissions {
                        let rule_count = permissions.allow.as_ref().map_or(0, |v| v.len())
                            + permissions.ask.as_ref().map_or(0, |v| v.len())
                            + permissions.deny.as_ref().map_or(0, |v| v.len());
                        if rule_count > 0 {
                            println!("🔐 Permissions: {} rules", rule_count);
                        }
                    }
                }
                Err(e) => {
                    println!("❌ Failed to load settings: {}", e);

                    let continue_confirmation =
                        ConfirmationService::confirm_action("Continue anyway?")?;

                    if !continue_confirmation {
                        println!("Snapshot creation cancelled.");
                        return Ok(false);
                    }
                }
            }
        }

        println!(); // Add spacing

        // Step 3: Get snapshot name
        let name = inquire::Text::new("Enter snapshot name:")
            .with_help_message("A descriptive name (e.g., 'development-setup', 'production-config')")
            .prompt()
            .map_err(|e| {
                let msg = e.to_string();
                if msg.contains("canceled") || msg.contains("cancelled") {
                    SelectorError::Cancelled
                } else {
                    SelectorError::Failed(format!("Input failed: {}", e))
                }
            })?;

        if name.trim().is_empty() {
            println!("❌ Snapshot name cannot be empty.");
            return Ok(false);
        }

        // Get description (optional)
        let description = inquire::Text::new("Enter description (optional):")
            .with_help_message("Optional description to help you remember what this snapshot is for")
            .prompt()
            .ok()
            .and_then(|d| {
                let trimmed = d.trim().to_string();
                if trimmed.is_empty() {
                    None
                } else {
                    Some(trimmed)
                }
            });

        // Select scope
        let scope_options = vec![
            "common - Common settings only (model, hooks, permissions)",
            "env - Environment variables only",
            "all - All settings (common + environment)",
        ];

        let scope_selection = inquire::Select::new("Select snapshot scope:", scope_options)
            .with_help_message("↑/↓: Navigate, Enter: Select")
            .prompt()
            .map_err(|e| {
                let msg = e.to_string();
                if msg.contains("canceled") || msg.contains("cancelled") {
                    SelectorError::Cancelled
                } else {
                    SelectorError::Failed(format!("Selection failed: {}", e))
                }
            })?;

        let scope = match scope_selection.split_once(" - ") {
            Some((scope_name, _)) => scope_name
                .parse::<SnapshotScope>()
                .map_err(|e| SelectorError::InvalidInput(format!("Invalid scope: {}", e)))?,
            None => SnapshotScope::Common,
        };

        // Show summary before confirmation
        println!("\n📋 Snapshot Summary:");
        println!("  Name: {}", name);
        println!("  Path: {}", settings_path.display());
        println!("  Scope: {}", scope);
        if let Some(ref desc) = description {
            println!("  Description: {}", desc);
        }

        // Confirm creation
        let confirmation = ConfirmationService::confirm_action("Create this snapshot?")?;

        if !confirmation {
            println!("Snapshot creation cancelled.");
            return Ok(false);
        }

        // Check if snapshot already exists
        if self.store.exists_by_name(&name) {
            let overwrite_confirmation = ConfirmationService::confirm_overwrite(&name, "snapshot")?;

            if !overwrite_confirmation {
                println!("Snapshot creation cancelled.");
                return Ok(false);
            }
        }

        // Create the snapshot
        let settings = if settings_path.exists() {
            ClaudeSettings::from_file(&settings_path)
                .map_err(|e| SelectorError::Failed(format!("Failed to load settings: {}", e)))?
        } else {
            ClaudeSettings::default()
        };

        // Capture environment variables if needed
        let mut snapshot_settings = settings;
        if matches!(scope, SnapshotScope::All | SnapshotScope::Env) {
            snapshot_settings.env = Some(ClaudeSettings::capture_environment());
        }

        let snapshot = Snapshot::new(name.clone(), snapshot_settings, scope, description);

        self.store.save(&snapshot).map_err(|e| {
            SelectorError::OperationFailed(format!("Failed to save snapshot: {}", e))
        })?;

        println!("✓ Snapshot '{}' created successfully!", name);

        Ok(true)
    }

    /// Apply a snapshot with confirmation
    fn apply_snapshot(&self, index: usize) -> SelectorResult<bool> {
        if index >= self.snapshots.len() {
            return Err(SelectorError::NotFound);
        }

        let snapshot = &self.snapshots[index];

        let confirmation =
            ConfirmationService::confirm_action(&format!("Apply snapshot '{}'?", snapshot.name))?;

        if confirmation {
            // Load current settings for comparison
            let settings_path = crate::utils::get_settings_path(None);
            let _existing_settings = ClaudeSettings::from_file(&settings_path).map_err(|e| {
                SelectorError::Failed(format!("Failed to load current settings: {}", e))
            })?;

            // Backup current settings
            let backup_path = settings_path.with_extension("json.backup");
            std::fs::copy(&settings_path, &backup_path).map_err(|e| {
                SelectorError::OperationFailed(format!("Failed to create backup: {}", e))
            })?;

            println!("✓ Settings backed up to: {}", backup_path.display());

            // Apply snapshot settings
            snapshot
                .settings
                .clone()
                .to_file(&settings_path)
                .map_err(|e| {
                    SelectorError::OperationFailed(format!("Failed to apply snapshot: {}", e))
                })?;

            println!("✓ Applied snapshot '{}' successfully!", snapshot.name);
            Ok(true)
        } else {
            println!("Apply cancelled.");
            Ok(false)
        }
    }

    /// Delete a snapshot with confirmation
    fn delete_snapshot(&self, index: usize) -> SelectorResult<bool> {
        if index >= self.snapshots.len() {
            return Err(SelectorError::NotFound);
        }

        let snapshot = &self.snapshots[index];

        let confirmation = ConfirmationService::confirm_deletion(&snapshot.name, "snapshot")?;

        if confirmation {
            self.store.delete(&snapshot.id).map_err(|e| {
                SelectorError::OperationFailed(format!("Failed to delete snapshot: {}", e))
            })?;
            println!("✓ Snapshot deleted successfully!");
            Ok(true)
        } else {
            println!("Deletion cancelled.");
            Ok(false)
        }
    }

    /// Rename a snapshot with confirmation
    fn rename_snapshot(&self, index: usize) -> SelectorResult<Option<bool>> {
        if index >= self.snapshots.len() {
            return Err(SelectorError::NotFound);
        }

        let snapshot = &self.snapshots[index];
        let new_name = prompt_rename(&snapshot.name, "snapshot")?;

        if new_name != snapshot.name {
            // Check if snapshot already exists with new name
            if self.store.exists_by_name(&new_name) {
                let overwrite_confirmation =
                    ConfirmationService::confirm_overwrite(&new_name, "snapshot")?;
                if !overwrite_confirmation {
                    println!("Rename cancelled.");
                    return Ok(Some(true));
                }
            }

            // Create new snapshot with updated name
            let mut updated_snapshot = snapshot.clone();
            updated_snapshot.name = new_name.clone();
            updated_snapshot.updated_at = chrono::Utc::now().to_rfc3339();

            // Save updated snapshot
            self.store.save(&updated_snapshot).map_err(|e| {
                SelectorError::OperationFailed(format!("Failed to rename snapshot: {}", e))
            })?;

            // Delete old snapshot
            self.store.delete(&snapshot.id).map_err(|e| {
                SelectorError::OperationFailed(format!("Failed to delete old snapshot: {}", e))
            })?;

            println!("✓ Snapshot renamed to '{}' successfully!", new_name);
        }

        Ok(Some(true))
    }
}