Repository navigation
Expand file tree
/
Copy pathuninstall_cmd.rs
More file actions
979 lines (853 loc) · 29.7 KB
/
Copy pathuninstall_cmd.rs
File metadata and controls
979 lines (853 loc) · 29.7 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! Uninstall command - safely remove Cortex CLI and associated data.
//!
//! Provides functionality to:
//! - Detect installation method (cargo, manual, installer)
//! - Remove binary from system paths
//! - Clean up configuration and data directories
//! - Remove shell completions
//! - Optionally keep config/data
//! - Show what would be deleted (dry-run mode)
//! - Create backup before removal
use crate::styled_output::{print_info, print_warning};
use anyhow::{Context, Result, bail};
use clap::Parser;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
/// Uninstall CLI command.
#[derive(Debug, Parser)]
pub struct UninstallCli {
/// Keep configuration files (config.toml, etc.)
#[arg(long, short = 'c', conflicts_with = "purge")]
pub keep_config: bool,
/// Keep session data and history
#[arg(long, short = 'd', conflicts_with = "purge")]
pub keep_data: bool,
/// Show what would be deleted without actually deleting
#[arg(long)]
pub dry_run: bool,
/// Skip confirmation prompt
#[arg(long, short = 'f')]
pub force: bool,
/// Auto-confirm (alias for --force)
#[arg(long, short = 'y')]
pub yes: bool,
/// Create backup before uninstalling
#[arg(long)]
pub backup: bool,
/// Complete removal: delete everything including config and data.
/// This is equivalent to not using --keep-config or --keep-data.
#[arg(long, short = 'p')]
pub purge: bool,
}
/// Installation detection result.
#[derive(Debug, Clone, PartialEq)]
enum InstallMethod {
Cargo,
Manual,
Installer,
Unknown,
}
impl std::fmt::Display for InstallMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InstallMethod::Cargo => write!(f, "cargo install"),
InstallMethod::Manual => write!(f, "manual"),
InstallMethod::Installer => write!(f, "system installer"),
InstallMethod::Unknown => write!(f, "unknown"),
}
}
}
/// Item to be removed during uninstall.
#[derive(Debug, Clone)]
struct RemovalItem {
path: PathBuf,
description: String,
size: u64,
requires_sudo: bool,
category: RemovalCategory,
}
/// Category of items to remove.
#[derive(Debug, Clone, PartialEq)]
enum RemovalCategory {
Binary,
Config,
Data,
Completions,
Plugins,
}
impl UninstallCli {
/// Run the uninstall command.
pub async fn run(self) -> Result<()> {
println!("Cortex CLI Uninstaller");
println!("{}", "=".repeat(50));
// Detect installation method
let install_method = detect_installation_method();
println!("Detected installation: {install_method}");
// Collect all items to remove
let all_items = collect_removal_items()?;
// Filter items based on flags
// --purge removes everything (equivalent to neither --keep-config nor --keep-data)
let items_to_remove: Vec<RemovalItem> = all_items
.into_iter()
.filter(|item| match item.category {
RemovalCategory::Config => self.purge || !self.keep_config,
RemovalCategory::Data => self.purge || !self.keep_data,
_ => true,
})
.filter(|item| item.path.exists())
.collect();
if items_to_remove.is_empty() {
println!("\nNothing to uninstall. Cortex CLI may not be installed or already removed.");
return Ok(());
}
// Display what will be removed
println!("\nItems to remove:");
println!("{}", "-".repeat(50));
let mut total_size: u64 = 0;
let mut has_sudo_items = false;
let mut items_by_category: HashMap<String, Vec<&RemovalItem>> = HashMap::new();
for item in &items_to_remove {
let category_name = match item.category {
RemovalCategory::Binary => "Binaries",
RemovalCategory::Config => "Configuration",
RemovalCategory::Data => "Session Data",
RemovalCategory::Completions => "Shell Completions",
RemovalCategory::Plugins => "Plugins & Skills",
};
items_by_category
.entry(category_name.to_string())
.or_default()
.push(item);
total_size += item.size;
if item.requires_sudo {
has_sudo_items = true;
}
}
for (category, items) in &items_by_category {
println!("\n {category}:");
for item in items {
let size_str = format_size(item.size);
let sudo_indicator = if item.requires_sudo {
" (requires sudo)"
} else {
""
};
println!(
" {} ({}){}",
item.path.display(),
size_str,
sudo_indicator
);
if !item.description.is_empty() {
println!(" └─ {}", item.description);
}
}
}
println!("\n{}", "-".repeat(50));
println!("Total space to free: {}", format_size(total_size));
if has_sudo_items {
println!();
print_warning("Some items require elevated privileges to remove.");
println!(" You may be prompted for your password.");
}
// Dry run mode - stop here
if self.dry_run {
println!("\n[DRY RUN] No files were deleted.");
return Ok(());
}
// Confirm before proceeding (Issue #3682: confirm BEFORE creating backup)
if !self.force && !self.yes {
println!("\nAre you sure you want to uninstall Cortex CLI? [y/N]");
if !prompt_yes_no()? {
println!("Uninstall cancelled.");
return Ok(());
}
}
// Backup if requested (only after user confirms)
if self.backup {
print_info("Creating backup...");
if let Err(e) = create_backup(&items_to_remove) {
print_warning(&format!("Failed to create backup: {e}"));
if !self.force && !self.yes {
println!("Continue without backup? [y/N]");
if !prompt_yes_no()? {
print_info("Uninstall cancelled.");
return Ok(());
}
}
}
}
// Perform the uninstall
println!("\nUninstalling...");
let mut errors: Vec<(PathBuf, String)> = Vec::new();
let mut removed_count = 0;
for item in &items_to_remove {
print!(" Removing {}... ", item.path.display());
match remove_item(item) {
Ok(()) => {
println!("✓");
removed_count += 1;
}
Err(e) => {
println!("✗");
errors.push((item.path.clone(), e.to_string()));
}
}
}
// Clean up shell completions from rc files
if items_to_remove
.iter()
.any(|i| i.category == RemovalCategory::Completions)
{
print_info("Cleaning shell configuration...");
if let Err(e) = clean_shell_completions() {
print_warning(&format!("Failed to clean shell config: {e}"));
}
}
// Summary
println!("\n{}", "=".repeat(50));
println!("Uninstall Summary:");
println!(" Items removed: {removed_count}/{}", items_to_remove.len());
println!(" Space freed: {}", format_size(total_size));
if !errors.is_empty() {
println!();
print_warning("Some items could not be removed:");
for (path, error) in &errors {
println!(" {} - {error}", path.display());
}
println!("\n You may need to remove these manually or with elevated privileges.");
} else {
println!("\n✓ Cortex CLI has been successfully uninstalled.");
}
if self.keep_config {
println!("\n Configuration files were preserved.");
}
if self.keep_data {
println!(" Session data was preserved.");
}
Ok(())
}
}
/// Detect how Cortex was installed.
fn detect_installation_method() -> InstallMethod {
let current_exe = match std::env::current_exe() {
Ok(exe) => exe,
Err(_) => return InstallMethod::Unknown,
};
let exe_path = current_exe.to_string_lossy();
// Check if in cargo bin directory
if exe_path.contains(".cargo") && exe_path.contains("bin") {
return InstallMethod::Cargo;
}
// Check common installation paths
#[cfg(target_os = "linux")]
{
if exe_path.contains("/usr/local/bin") || exe_path.contains("/usr/bin") {
return InstallMethod::Installer;
}
if exe_path.contains(".local/bin") {
return InstallMethod::Manual;
}
}
#[cfg(target_os = "macos")]
{
if exe_path.contains("/usr/local/bin") || exe_path.contains("/opt/homebrew") {
return InstallMethod::Installer;
}
if exe_path.contains(".local/bin") {
return InstallMethod::Manual;
}
}
#[cfg(target_os = "windows")]
{
if exe_path.contains("Program Files") || exe_path.contains("AppData\\Local\\Programs") {
return InstallMethod::Installer;
}
}
InstallMethod::Manual
}
/// Collect all items that should be removed during uninstall.
fn collect_removal_items() -> Result<Vec<RemovalItem>> {
let mut items = Vec::new();
// Get home directory
let home_dir = dirs::home_dir().context("Could not determine home directory")?;
// 1. Binary locations
items.extend(collect_binary_locations(&home_dir)?);
// 2. Cortex home directory (~/.cortex)
items.extend(collect_cortex_home_items(&home_dir)?);
// 3. Platform-specific locations
#[cfg(target_os = "windows")]
items.extend(collect_windows_items()?);
// 4. Shell completions
items.extend(collect_completion_items(&home_dir)?);
Ok(items)
}
/// Collect binary installation locations.
fn collect_binary_locations(home_dir: &Path) -> Result<Vec<RemovalItem>> {
let mut items = Vec::new();
// Current executable
if let Ok(current_exe) = std::env::current_exe()
&& current_exe.exists()
{
items.push(RemovalItem {
path: current_exe.clone(),
description: "Current Cortex binary".to_string(),
size: get_file_size(¤t_exe),
requires_sudo: path_requires_sudo(¤t_exe),
category: RemovalCategory::Binary,
});
// Check for backup file (cortex.old)
let backup_path = current_exe.with_extension("old");
if backup_path.exists() {
items.push(RemovalItem {
path: backup_path.clone(),
description: "Upgrade backup binary".to_string(),
size: get_file_size(&backup_path),
requires_sudo: path_requires_sudo(&backup_path),
category: RemovalCategory::Binary,
});
}
}
// Common binary locations
let binary_locations = [
home_dir.join(".local").join("bin").join("Cortex"),
home_dir.join(".cargo").join("bin").join("Cortex"),
#[cfg(not(target_os = "windows"))]
PathBuf::from("/usr/local/bin/cortex"),
#[cfg(target_os = "windows")]
home_dir.join(".cargo").join("bin").join("cortex.exe"),
];
for path in binary_locations {
if path.exists() && !items.iter().any(|i| i.path == path) {
items.push(RemovalItem {
path: path.clone(),
description: String::new(),
size: get_file_size(&path),
requires_sudo: path_requires_sudo(&path),
category: RemovalCategory::Binary,
});
}
}
Ok(items)
}
/// Collect items from the ~/.cortex directory.
fn collect_cortex_home_items(home_dir: &Path) -> Result<Vec<RemovalItem>> {
let mut items = Vec::new();
let cortex_home = home_dir.join(".cortex");
if !cortex_home.exists() {
return Ok(items);
}
// Configuration files
let config_files = [
("config.toml", "Main configuration file"),
("credentials.json", "Authentication credentials"),
("auth.json", "OAuth tokens"),
];
for (file, desc) in config_files {
let path = cortex_home.join(file);
if path.exists() {
items.push(RemovalItem {
path: path.clone(),
description: desc.to_string(),
size: get_file_size(&path),
requires_sudo: false,
category: RemovalCategory::Config,
});
}
}
// Session data directory
let sessions_dir = cortex_home.join("sessions");
if sessions_dir.exists() {
items.push(RemovalItem {
path: sessions_dir.clone(),
description: "Session history and data".to_string(),
size: get_dir_size(&sessions_dir),
requires_sudo: false,
category: RemovalCategory::Data,
});
}
// Logs directory
let logs_dir = cortex_home.join("logs");
if logs_dir.exists() {
items.push(RemovalItem {
path: logs_dir.clone(),
description: "Log files".to_string(),
size: get_dir_size(&logs_dir),
requires_sudo: false,
category: RemovalCategory::Data,
});
}
// Plugins directory
let plugins_dir = cortex_home.join("plugins");
if plugins_dir.exists() {
items.push(RemovalItem {
path: plugins_dir.clone(),
description: "Installed plugins".to_string(),
size: get_dir_size(&plugins_dir),
requires_sudo: false,
category: RemovalCategory::Plugins,
});
}
// Skills directory
let skills_dir = cortex_home.join("skills");
if skills_dir.exists() {
items.push(RemovalItem {
path: skills_dir.clone(),
description: "Custom skills".to_string(),
size: get_dir_size(&skills_dir),
requires_sudo: false,
category: RemovalCategory::Plugins,
});
}
// MCP servers directory
let mcp_dir = cortex_home.join("mcp");
if mcp_dir.exists() {
items.push(RemovalItem {
path: mcp_dir.clone(),
description: "MCP server configurations".to_string(),
size: get_dir_size(&mcp_dir),
requires_sudo: false,
category: RemovalCategory::Config,
});
}
// Agents directory
let agents_dir = cortex_home.join("agents");
if agents_dir.exists() {
items.push(RemovalItem {
path: agents_dir.clone(),
description: "Custom agents".to_string(),
size: get_dir_size(&agents_dir),
requires_sudo: false,
category: RemovalCategory::Plugins,
});
}
// Cache directory
let cache_dir = cortex_home.join("cache");
if cache_dir.exists() {
items.push(RemovalItem {
path: cache_dir.clone(),
description: "Cached data".to_string(),
size: get_dir_size(&cache_dir),
requires_sudo: false,
category: RemovalCategory::Data,
});
}
// If nothing specific was found but the directory exists, add it entirely
// (this catches any subdirectories we might have missed)
if items
.iter()
.filter(|i| i.path.starts_with(&cortex_home))
.count()
== 0
{
items.push(RemovalItem {
path: cortex_home.clone(),
description: "Cortex home directory".to_string(),
size: get_dir_size(&cortex_home),
requires_sudo: false,
category: RemovalCategory::Config,
});
} else {
// Add the parent directory itself at the end (to be removed after contents)
items.push(RemovalItem {
path: cortex_home,
description: "Cortex home directory (if empty)".to_string(),
size: 0,
requires_sudo: false,
category: RemovalCategory::Config,
});
}
Ok(items)
}
/// Collect Windows-specific locations.
#[cfg(target_os = "windows")]
fn collect_windows_items() -> Result<Vec<RemovalItem>> {
let mut items = Vec::new();
// %LOCALAPPDATA%\cortex
if let Some(local_app_data) = dirs::data_local_dir() {
let cortex_dir = local_app_data.join("Cortex");
if cortex_dir.exists() {
items.push(RemovalItem {
path: cortex_dir.clone(),
description: "Windows local app data".to_string(),
size: get_dir_size(&cortex_dir),
requires_sudo: false,
category: RemovalCategory::Data,
});
}
}
// %APPDATA%\cortex
if let Some(roaming_data) = dirs::config_dir() {
let cortex_dir = roaming_data.join("Cortex");
if cortex_dir.exists() {
items.push(RemovalItem {
path: cortex_dir.clone(),
description: "Windows roaming app data".to_string(),
size: get_dir_size(&cortex_dir),
requires_sudo: false,
category: RemovalCategory::Config,
});
}
}
Ok(items)
}
/// Collect shell completion file locations.
fn collect_completion_items(home_dir: &Path) -> Result<Vec<RemovalItem>> {
let mut items = Vec::new();
#[cfg(not(target_os = "windows"))]
{
// Bash completions
let bash_completions = [
home_dir.join(".local/share/bash-completion/completions/cortex"),
PathBuf::from("/etc/bash_completion.d/cortex"),
PathBuf::from("/usr/local/etc/bash_completion.d/cortex"),
];
for path in bash_completions {
if path.exists() {
items.push(RemovalItem {
path: path.clone(),
description: "Bash completion script".to_string(),
size: get_file_size(&path),
requires_sudo: path_requires_sudo(&path),
category: RemovalCategory::Completions,
});
}
}
// Zsh completions
let zsh_completions = [
home_dir.join(".zfunc/_cortex"),
home_dir.join(".local/share/zsh/site-functions/_cortex"),
PathBuf::from("/usr/local/share/zsh/site-functions/_cortex"),
];
for path in zsh_completions {
if path.exists() {
items.push(RemovalItem {
path: path.clone(),
description: "Zsh completion script".to_string(),
size: get_file_size(&path),
requires_sudo: path_requires_sudo(&path),
category: RemovalCategory::Completions,
});
}
}
// Fish completions
let fish_completion = home_dir.join(".config/fish/completions/cortex.fish");
if fish_completion.exists() {
items.push(RemovalItem {
path: fish_completion.clone(),
description: "Fish completion script".to_string(),
size: get_file_size(&fish_completion),
requires_sudo: false,
category: RemovalCategory::Completions,
});
}
}
#[cfg(target_os = "windows")]
{
// PowerShell profile additions are handled separately
// Check for PowerShell completion module
if let Some(documents) = dirs::document_dir() {
let ps_module = documents
.join("PowerShell")
.join("Modules")
.join("CortexCompletion");
if ps_module.exists() {
items.push(RemovalItem {
path: ps_module.clone(),
description: "PowerShell completion module".to_string(),
size: get_dir_size(&ps_module),
requires_sudo: false,
category: RemovalCategory::Completions,
});
}
}
}
Ok(items)
}
/// Get the size of a file.
fn get_file_size(path: &Path) -> u64 {
fs::metadata(path).map(|m| m.len()).unwrap_or(0)
}
/// Get the total size of a directory recursively.
fn get_dir_size(path: &Path) -> u64 {
let mut total = 0;
if let Ok(entries) = fs::read_dir(path) {
for entry in entries.flatten() {
let entry_path = entry.path();
if entry_path.is_dir() {
total += get_dir_size(&entry_path);
} else {
total += get_file_size(&entry_path);
}
}
}
total
}
/// Format bytes as human-readable size.
fn format_size(bytes: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{bytes} bytes")
}
}
/// Check if a path requires sudo to modify.
fn path_requires_sudo(path: &Path) -> bool {
let path_str = path.to_string_lossy();
#[cfg(not(target_os = "windows"))]
{
path_str.starts_with("/usr/local/")
|| path_str.starts_with("/usr/bin/")
|| path_str.starts_with("/etc/")
|| path_str.starts_with("/opt/")
}
#[cfg(target_os = "windows")]
{
path_str.contains("Program Files") || path_str.contains("Windows")
}
}
/// Prompt user for yes/no confirmation.
fn prompt_yes_no() -> Result<bool> {
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
Ok(input.trim().eq_ignore_ascii_case("y") || input.trim().eq_ignore_ascii_case("yes"))
}
/// Create a backup of items before removal.
fn create_backup(items: &[RemovalItem]) -> Result<()> {
let backup_dir = dirs::home_dir()
.context("Could not determine home directory")?
.join(".cortex-backup")
.join(chrono::Local::now().format("%Y%m%d_%H%M%S").to_string());
fs::create_dir_all(&backup_dir)?;
println!(" Backup location: {}", backup_dir.display());
for item in items {
if !item.path.exists() {
continue;
}
let relative_path = item
.path
.strip_prefix(dirs::home_dir().unwrap_or_default())
.unwrap_or(&item.path);
let backup_path = backup_dir.join(relative_path);
if let Some(parent) = backup_path.parent() {
fs::create_dir_all(parent)?;
}
if item.path.is_dir() {
copy_dir_all(&item.path, &backup_path)?;
} else {
fs::copy(&item.path, &backup_path)?;
}
}
println!(" ✓ Backup created successfully");
Ok(())
}
/// Copy a directory recursively.
fn copy_dir_all(src: &Path, dst: &Path) -> Result<()> {
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
if src_path.is_dir() {
copy_dir_all(&src_path, &dst_path)?;
} else {
fs::copy(&src_path, &dst_path)?;
}
}
Ok(())
}
/// Remove a single item (file or directory).
fn remove_item(item: &RemovalItem) -> Result<()> {
// Safety check: never delete root or home directory
validate_path_safety(&item.path)?;
if !item.path.exists() {
return Ok(());
}
if item.path.is_dir() {
// For the cortex home directory, only remove if empty
if item.description.contains("if empty") {
if fs::read_dir(&item.path)?.next().is_none() {
fs::remove_dir(&item.path)?;
}
} else {
fs::remove_dir_all(&item.path)?;
}
} else {
fs::remove_file(&item.path)?;
}
Ok(())
}
/// Validate that a path is safe to delete.
fn validate_path_safety(path: &Path) -> Result<()> {
let path_str = path.to_string_lossy();
let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let canonical_str = canonical.to_string_lossy();
// Never delete root directories
let forbidden_paths = [
"/",
"/home",
"/Users",
"/root",
"C:\\",
"C:\\Windows",
"C:\\Users",
"C:\\Program Files",
];
for forbidden in forbidden_paths {
if canonical_str == forbidden || path_str == forbidden {
bail!("Refusing to delete protected path: {}", path.display());
}
}
// Never delete home directory itself
if let Some(home) = dirs::home_dir()
&& canonical == home
{
bail!("Refusing to delete home directory");
}
// Ensure path contains "Cortex" somewhere (sanity check)
if !path_str.to_lowercase().contains("Cortex") {
// Allow common binary locations even without "Cortex" in parent path
let is_binary = path
.file_name()
.map(|n| {
let name = n.to_string_lossy().to_lowercase();
name == "Cortex" || name == "cortex.exe" || name == "cortex.old"
})
.unwrap_or(false);
if !is_binary {
bail!(
"Path does not appear to be Cortex-related: {}",
path.display()
);
}
}
Ok(())
}
/// Clean up shell completion references from rc files.
fn clean_shell_completions() -> Result<()> {
let home_dir = dirs::home_dir().context("Could not determine home directory")?;
#[cfg(not(target_os = "windows"))]
{
// Clean .bashrc
let bashrc = home_dir.join(".bashrc");
if bashrc.exists() {
clean_rc_file(
&bashrc,
&["Cortex", "# cortex", "eval \"$(cortex completion"],
)?;
}
// Clean .zshrc
let zshrc = home_dir.join(".zshrc");
if zshrc.exists() {
clean_rc_file(
&zshrc,
&["Cortex", "# cortex", "eval \"$(cortex completion"],
)?;
}
// Clean .bash_profile
let bash_profile = home_dir.join(".bash_profile");
if bash_profile.exists() {
clean_rc_file(&bash_profile, &["Cortex", "# cortex"])?;
}
// Clean .profile
let profile = home_dir.join(".profile");
if profile.exists() {
clean_rc_file(&profile, &["Cortex"])?;
}
// Clean fish config
let fish_config = home_dir.join(".config/fish/config.fish");
if fish_config.exists() {
clean_rc_file(&fish_config, &["Cortex"])?;
}
}
#[cfg(target_os = "windows")]
{
// Note: Cleaning PowerShell profile is more complex and should be done carefully
println!(" Note: You may need to manually remove Cortex from your PowerShell profile.");
}
Ok(())
}
/// Remove lines containing cortex-related content from an rc file.
fn clean_rc_file(path: &Path, patterns: &[&str]) -> Result<()> {
let content = fs::read_to_string(path)?;
let mut new_lines: Vec<&str> = Vec::new();
let mut removed = false;
for line in content.lines() {
let should_remove = patterns.iter().any(|p| {
line.contains(p)
&& (line.contains("completion")
|| line.contains("source")
|| line.contains("eval")
|| line.trim().starts_with('#') && line.to_lowercase().contains("Cortex"))
});
if should_remove {
removed = true;
} else {
new_lines.push(line);
}
}
if removed {
let new_content = new_lines.join("\n");
fs::write(path, new_content)?;
println!(" Cleaned: {}", path.display());
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_size() {
assert_eq!(format_size(0), "0 bytes");
assert_eq!(format_size(512), "512 bytes");
assert_eq!(format_size(1024), "1.00 KB");
assert_eq!(format_size(1536), "1.50 KB");
assert_eq!(format_size(1048576), "1.00 MB");
assert_eq!(format_size(1073741824), "1.00 GB");
}
#[test]
fn test_validate_path_safety() {
// Test that root paths are rejected
#[cfg(unix)]
{
assert!(validate_path_safety(Path::new("/")).is_err());
assert!(validate_path_safety(Path::new("/home")).is_err());
}
#[cfg(windows)]
{
assert!(validate_path_safety(Path::new("C:\\")).is_err());
assert!(validate_path_safety(Path::new("C:\\Windows")).is_err());
}
// Test binary names (these don't exist so may fail canonicalization but shouldn't panic)
#[cfg(unix)]
{
let binary_path = Path::new("/usr/local/bin/cortex");
let _ = validate_path_safety(binary_path); // May succeed or fail depending on path existence
}
#[cfg(windows)]
{
let binary_path = Path::new("C:\\Program Files\\cortex\\cortex.exe");
let _ = validate_path_safety(binary_path); // May succeed or fail depending on path existence
}
}
#[test]
fn test_detect_installation_method() {
// This is somewhat environment-dependent
let method = detect_installation_method();
// Just ensure it returns something valid
match method {
InstallMethod::Cargo
| InstallMethod::Manual
| InstallMethod::Installer
| InstallMethod::Unknown => {}
}
}
}