kopi 0.2.3

Kopi is a JDK version management tool
Documentation
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
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::KopiConfig;
use crate::error::{KopiError, Result};
use crate::indicator::{ProgressConfig, ProgressFactory, ProgressStyle, StatusReporter};
use crate::locking::{InstalledScopeResolver, LockBackend, LockController, ScopedPackageLockGuard};
use crate::models::distribution::Distribution;
use crate::storage::formatting::format_size;
use crate::storage::{InstalledJdk, JdkRepository};
use crate::uninstall::feedback::{
    display_batch_uninstall_confirmation, display_batch_uninstall_summary,
};
use crate::uninstall::progress::ProgressReporter;
use crate::version::VersionRequest;
use log::{debug, info, warn};
use std::str::FromStr;
use std::sync::{Arc, Mutex};

pub struct BatchUninstaller<'a> {
    config: &'a KopiConfig,
    repository: &'a JdkRepository<'a>,
    no_progress: bool,
}

impl<'a> BatchUninstaller<'a> {
    pub fn new(
        config: &'a KopiConfig,
        repository: &'a JdkRepository<'a>,
        no_progress: bool,
    ) -> Self {
        Self {
            config,
            repository,
            no_progress,
        }
    }

    pub fn uninstall_all(&self, spec: Option<&str>, force: bool, dry_run: bool) -> Result<()> {
        let jdks = if let Some(spec_str) = spec {
            // Build the list of all known distributions (built-in + additional)
            let mut all_distributions: Vec<String> = Distribution::known_distributions()
                .into_iter()
                .map(|s| s.to_string())
                .collect();
            all_distributions.extend(self.config.additional_distributions.clone());

            // Check if spec is a distribution name (case-insensitive)
            let is_distribution = all_distributions
                .iter()
                .any(|dist| dist.eq_ignore_ascii_case(spec_str));

            if is_distribution {
                // Filter installed JDKs by distribution
                debug!("Filtering JDKs by distribution: {spec_str}");
                let all_jdks = self.repository.list_installed_jdks()?;
                all_jdks
                    .into_iter()
                    .filter(|jdk| jdk.distribution.eq_ignore_ascii_case(spec_str))
                    .collect()
            } else {
                // Parse as version request
                let version_request = VersionRequest::from_str(spec_str)?;
                debug!("Parsed version request: {version_request:?}");
                self.repository.find_matching_jdks(&version_request)?
            }
        } else {
            // List all installed JDKs if no spec provided
            self.repository.list_installed_jdks()?
        };

        if jdks.is_empty() {
            return Err(KopiError::JdkNotInstalled {
                jdk_spec: spec.unwrap_or("all").to_string(),
                version: None,
                distribution: None,
                auto_install_enabled: false,
                auto_install_failed: None,
                user_declined: false,
                install_in_progress: false,
            });
        }

        self.uninstall_batch(jdks, force, dry_run)
    }

    pub fn uninstall_batch(
        &self,
        jdks: Vec<InstalledJdk>,
        force: bool,
        dry_run: bool,
    ) -> Result<()> {
        if jdks.is_empty() {
            return Ok(());
        }

        // Calculate total size
        let total_size = self.calculate_total_size(&jdks)?;

        // Show summary only in dry-run mode
        if dry_run {
            self.display_batch_summary(&jdks, total_size)?;
            return Ok(());
        }

        // Confirm unless forced
        if !force && !self.confirm_batch_removal(&jdks)? {
            return Err(KopiError::SystemError(
                "User cancelled operation".to_string(),
            ));
        }

        // Add a newline after confirmation for cleaner output
        println!();

        // Perform batch removal with transaction-like behavior
        self.execute_batch_removal(jdks, total_size, force)
    }

    fn calculate_total_size(&self, jdks: &[InstalledJdk]) -> Result<u64> {
        let mut total = 0u64;
        for jdk in jdks {
            total += self.repository.get_jdk_size(&jdk.path)?;
        }
        Ok(total)
    }

    fn display_batch_summary(&self, jdks: &[InstalledJdk], total_size: u64) -> Result<()> {
        let lock_feedback = Arc::new(Mutex::new(ProgressFactory::create(self.no_progress)));
        if let Ok(mut indicator) = lock_feedback.lock() {
            indicator.start(ProgressConfig::new(ProgressStyle::Status));
        }
        let reporter = StatusReporter::with_shared_indicator(lock_feedback.clone());

        reporter.operation("JDKs to be removed", "");

        for jdk in jdks {
            let size = self.repository.get_jdk_size(&jdk.path)?;
            reporter.step(&format!(
                "- {}@{} ({})",
                jdk.distribution,
                jdk.version,
                format_size(size)
            ));
        }

        reporter.step(&format!(
            "Total: {} JDKs, {}",
            jdks.len(),
            format_size(total_size)
        ));

        Ok(())
    }

    fn confirm_batch_removal(&self, jdks: &[InstalledJdk]) -> Result<bool> {
        let total_size = self.calculate_total_size(jdks)?;
        display_batch_uninstall_confirmation(jdks, total_size)
    }

    fn execute_batch_removal(
        &self,
        jdks: Vec<InstalledJdk>,
        total_size: u64,
        force: bool,
    ) -> Result<()> {
        let mut progress_reporter = ProgressReporter::new_batch(self.no_progress);
        let overall_pb = progress_reporter.create_batch_removal_bar(jdks.len() as u64);

        let mut removed_count = 0;
        let mut failed_jdks = Vec::new();
        let mut removed_jdks = Vec::new();
        let mut log_messages = Vec::new(); // Collect log messages to output after progress

        let lock_feedback = Arc::new(Mutex::new(ProgressFactory::create(self.no_progress)));
        if let Ok(mut indicator) = lock_feedback.lock() {
            indicator.start(ProgressConfig::new(ProgressStyle::Status));
        }
        let reporter = StatusReporter::with_shared_indicator(lock_feedback.clone());
        let controller = LockController::with_default_inspector(
            self.config.kopi_home().to_path_buf(),
            &self.config.locking,
        );
        let scope_resolver = InstalledScopeResolver::new(self.repository);

        for jdk in &jdks {
            log_messages.push(format!("Removing {}@{}", jdk.distribution, jdk.version));

            // Update overall progress bar message for current JDK
            overall_pb.set_message(format!("Removing {}@{}...", jdk.distribution, jdk.version));

            let removal_result = (|| -> Result<()> {
                let scope = scope_resolver.resolve(jdk)?;
                let scope_label = scope.label().to_string();

                progress_reporter.suspend(|| {
                    reporter.step(&format!("Acquiring uninstall lock for {scope_label}"));
                });
                let acquisition =
                    controller.acquire_with_feedback(scope.clone(), lock_feedback.clone())?;
                let uninstall_lock_guard = ScopedPackageLockGuard::new(&controller, acquisition);
                let backend_label = match uninstall_lock_guard.backend() {
                    LockBackend::Advisory => "advisory",
                    LockBackend::Fallback => "fallback",
                };

                progress_reporter.suspend(|| {
                    reporter.step(&format!("Using {backend_label} backend for {scope_label}"));
                });
                info!("Acquired uninstall lock for {scope_label} using {backend_label} backend");

                let active_summary = crate::uninstall::safety::perform_safety_checks(
                    self.config,
                    self.repository,
                    jdk,
                    force,
                )?;

                if force && active_summary.has_active_use() {
                    if let Some(global) = &active_summary.global {
                        warn!(
                            "--force removing {}@{} despite active global configuration {}",
                            jdk.distribution, jdk.version, global
                        );
                        log_messages.push(format!(
                            "Force removing active global default via {} for {}@{}",
                            global, jdk.distribution, jdk.version
                        ));
                        progress_reporter.suspend(|| {
                            reporter.step(&format!(
                                "Proceeding with --force: global default set via {global}"
                            ));
                        });
                    }

                    if let Some(project) = &active_summary.project {
                        warn!(
                            "--force removing {}@{} despite active project configuration {}",
                            jdk.distribution, jdk.version, project
                        );
                        log_messages.push(format!(
                            "Force removing active project default via {} for {}@{}",
                            project, jdk.distribution, jdk.version
                        ));
                        progress_reporter.suspend(|| {
                            reporter.step(&format!(
                                "Proceeding with --force: project default set via {project}"
                            ));
                        });
                    }
                }

                match self.repository.remove_jdk(&jdk.path) {
                    Ok(()) => uninstall_lock_guard.release(),
                    Err(err) => Err(err),
                }
            })();

            overall_pb.inc(1);

            match removal_result {
                Ok(()) => {
                    removed_count += 1;
                    removed_jdks.push(jdk.clone());
                }
                Err(err) => {
                    let err_string = err.to_string();
                    log_messages.push(format!(
                        "Failed to remove {}@{}: {err_string}",
                        jdk.distribution, jdk.version
                    ));
                    warn!(
                        "Failed to remove {}@{}: {err_string}",
                        jdk.distribution, jdk.version
                    );
                    failed_jdks.push((jdk.clone(), err));
                }
            }
        }

        overall_pb.finish_and_clear();

        // Show status messages after progress bar is done using StatusReporter
        for jdk in &removed_jdks {
            reporter.success(&format!("Removed {}@{}", jdk.distribution, jdk.version));
        }

        for (jdk, _) in &failed_jdks {
            reporter.error(&format!(
                "Failed to remove {}@{}",
                jdk.distribution, jdk.version
            ));
        }

        // Now output the collected log messages after all progress indicators are finished
        for msg in log_messages {
            if msg.starts_with("Removing ") {
                info!("{msg}");
            } else if msg.starts_with("Safety check failed") || msg.starts_with("Failed to remove")
            {
                warn!("{msg}");
            }
        }

        // Report results
        let failed_with_messages: Vec<(InstalledJdk, String)> = failed_jdks
            .into_iter()
            .map(|(jdk, err)| (jdk, err.to_string()))
            .collect();

        display_batch_uninstall_summary(&removed_jdks, &failed_with_messages, total_size);

        // Return error if all removals failed
        if removed_count == 0 {
            return Err(KopiError::SystemError(
                "All JDK removals failed".to_string(),
            ));
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::KopiConfig;
    use crate::locking::{
        InstalledScopeResolver, LockController, LockTimeoutValue, ScopedPackageLockGuard,
    };
    use crate::paths::install;
    use mockall::mock;
    use tempfile::TempDir;

    // Import shared test fixtures
    use crate::test::fixtures::create_test_jdk_with_path;

    // Mock JdkRepository trait
    mock! {
        JdkRepo {
            fn list_installed_jdks(&self) -> Result<Vec<InstalledJdk>>;
            fn get_jdk_size(&self, path: &std::path::Path) -> Result<u64>;
            fn remove_jdk(&self, path: &std::path::Path) -> Result<()>;
        }
    }

    #[test]
    fn test_calculate_total_size() {
        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let repository = JdkRepository::new(&config);
        let batch_uninstaller = BatchUninstaller::new(&config, &repository, false);

        // Create some test directories
        install::ensure_installations_root(temp_dir.path()).unwrap();
        let jdk1_path = install::installation_directory(temp_dir.path(), "temurin-21.0.5+11");
        let jdk2_path = install::installation_directory(temp_dir.path(), "corretto-17.0.9");
        std::fs::create_dir_all(&jdk1_path).unwrap();
        std::fs::create_dir_all(&jdk2_path).unwrap();

        // Write some test files
        std::fs::write(jdk1_path.join("test1.txt"), vec![0u8; 1024]).unwrap();
        std::fs::write(jdk2_path.join("test2.txt"), vec![0u8; 2048]).unwrap();

        let jdks = vec![
            create_test_jdk_with_path("temurin", "21.0.5+11", jdk1_path.to_str().unwrap()),
            create_test_jdk_with_path("corretto", "17.0.9", jdk2_path.to_str().unwrap()),
        ];

        let total_size = batch_uninstaller.calculate_total_size(&jdks).unwrap();
        assert_eq!(total_size, 3072);
    }

    #[test]
    fn test_uninstall_all_invalid_spec() {
        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let repository = JdkRepository::new(&config);
        let batch_uninstaller = BatchUninstaller::new(&config, &repository, false);

        // Test invalid version spec
        let result = batch_uninstaller.uninstall_all(Some("invalid.version"), false, false);
        assert!(result.is_err());
        match result {
            Err(KopiError::InvalidVersionFormat(_)) => {
                // Good, got the expected error type
            }
            _ => panic!("Expected InvalidVersionFormat error"),
        }
    }

    #[test]
    fn batch_uninstall_handles_lock_contention() {
        let temp_dir = TempDir::new().unwrap();
        let mut config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        config
            .locking
            .set_timeout_value(LockTimeoutValue::from_secs(0));
        let repository = JdkRepository::new(&config);
        install::ensure_installations_root(temp_dir.path()).unwrap();

        let locked_slug = "temurin-21.0.5+11";
        let other_slug = "corretto-17.0.9";
        let locked_path = install::installation_directory(temp_dir.path(), locked_slug);
        let other_path = install::installation_directory(temp_dir.path(), other_slug);
        std::fs::create_dir_all(&locked_path).unwrap();
        std::fs::create_dir_all(&other_path).unwrap();

        let locked_jdk =
            create_test_jdk_with_path("temurin", "21.0.5+11", locked_path.to_str().unwrap());
        let other_jdk =
            create_test_jdk_with_path("corretto", "17.0.9", other_path.to_str().unwrap());

        let controller = LockController::with_default_inspector(
            config.kopi_home().to_path_buf(),
            &config.locking,
        );
        let resolver = InstalledScopeResolver::new(&repository);
        let scope = resolver.resolve(&locked_jdk).unwrap();
        let acquisition = controller.acquire(scope.clone()).unwrap();
        let guard = ScopedPackageLockGuard::new(&controller, acquisition);

        let batch_uninstaller = BatchUninstaller::new(&config, &repository, true);
        let result = batch_uninstaller.uninstall_batch(
            vec![locked_jdk.clone(), other_jdk.clone()],
            true,
            false,
        );
        assert!(
            result.is_ok(),
            "batch uninstall should continue despite lock contention"
        );
        assert!(
            locked_jdk.path.exists(),
            "locked JDK should remain when lock acquisition times out"
        );
        assert!(
            !other_jdk.path.exists(),
            "unlocked JDK should be removed successfully"
        );

        guard.release().unwrap();
        if locked_jdk.path.exists() {
            std::fs::remove_dir_all(&locked_jdk.path).unwrap();
        }
    }

    #[test]
    fn batch_uninstall_releases_lock_after_success() {
        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let repository = JdkRepository::new(&config);
        install::ensure_installations_root(temp_dir.path()).unwrap();

        let slug = "temurin-21.0.5+11";
        let path = install::installation_directory(temp_dir.path(), slug);
        std::fs::create_dir_all(&path).unwrap();
        let jdk = create_test_jdk_with_path("temurin", "21.0.5+11", path.to_str().unwrap());

        let resolver = InstalledScopeResolver::new(&repository);
        let scope = resolver.resolve(&jdk).unwrap();

        let batch_uninstaller = BatchUninstaller::new(&config, &repository, true);
        batch_uninstaller
            .uninstall_batch(vec![jdk.clone()], true, false)
            .expect("batch uninstall should succeed");
        assert!(
            !jdk.path.exists(),
            "JDK directory should be removed after successful uninstall"
        );

        let controller = LockController::with_default_inspector(
            config.kopi_home().to_path_buf(),
            &config.locking,
        );
        let reacquired = controller.try_acquire(scope.clone()).unwrap();
        assert!(
            reacquired.is_some(),
            "lock scope should be re-acquirable after uninstall completes"
        );
        if let Some(handle) = reacquired {
            controller.release(handle).unwrap();
        }
    }
}