kopi 0.0.2

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
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
# Which Command Implementation Plan

## Overview
This document outlines the implementation plan for the `kopi which` command, which shows the installation path for JDK versions. The command provides a simple way to locate Java executables, other JDK tools, or the JDK home directory, supporting both current and specific JDK version queries with flexible output formats.

## Command Syntax
- `kopi which [<version>] [options]` - Show path to java executable or JDK home
  - `<version>` - Optional JDK version specification (e.g., `21`, `temurin@21.0.5+11`)
  - `--tool <name>` - Show path for specific JDK tool (default: java)
  - `--home` - Show JDK home directory instead of executable path
  - `--json` - Output in JSON format
- Alias: `w`

## Phase 1: Core Implementation

### Input Resources
- `/docs/tasks/which/design.md` - Complete which command design
- `/src/version/resolver.rs` - Existing version resolution logic
- `/src/storage/jdk_repository.rs` - JDK installation management
- `/src/error/` - Error types and exit codes
- `/src/commands/mod.rs` - Command structure

### Deliverables
1. **Which Command Module** (`/src/commands/which.rs`)
   - Command handler implementation with clap derive:
     ```rust
     use clap::Args;
     use serde::Serialize;
     use crate::error::{KopiError, KopiResult};
     use crate::storage::JdkRepository;
     use crate::version::resolver::resolve_version;
     use crate::models::{JdkSpec, parse_jdk_spec};
     use std::path::PathBuf;

     #[derive(Args, Debug)]
     pub struct WhichCommand {
         /// JDK version specification (optional)
         version: Option<String>,

         /// Show path for specific JDK tool
         #[arg(long, default_value = "java")]
         tool: String,

         /// Show JDK home directory instead of executable path
         #[arg(long)]
         home: bool,

         /// Output in JSON format
         #[arg(long)]
         json: bool,
     }

     #[derive(Serialize)]
     struct WhichOutput {
         distribution: String,
         version: String,
         tool: String,
         tool_path: String,
         jdk_home: String,
         source: String,
     }
     ```

   - Version resolution logic:
     ```rust
     impl WhichCommand {
         pub fn execute(self) -> KopiResult<()> {
             let repo = JdkRepository::load()?;
             
             // Resolve JDK spec
             let (jdk_spec, source) = if let Some(version) = self.version {
                 // Parse specified version
                 let spec = parse_jdk_spec(&version)?;
                 (spec, "specified".to_string())
             } else {
                 // Use current version resolution
                 let (version_req, source) = resolve_version_with_source()?;
                 let version_req = version_req.ok_or(KopiError::NoLocalVersion {
                     searched_paths: vec![], // populated by resolver
                 })?;
                 let spec = JdkSpec::from_version_request(version_req)?;
                 (spec, format!("{:?}", source))
             };

             // Find installed JDK
             let installation = repo.find_installed_jdk(&jdk_spec)?
                 .ok_or_else(|| KopiError::JdkNotInstalled {
                     jdk_spec: jdk_spec.clone(),
                     version: jdk_spec.version().to_string(),
                     auto_install_enabled: false,
                 })?;

             // Determine output path
             let output_path = if self.home {
                 installation.path().to_path_buf()
             } else {
                 self.get_tool_path(&installation, &self.tool)?
             };

             // Output result
             if self.json {
                 self.output_json(&jdk_spec, &installation, &output_path, &source)?;
             } else {
                 println!("{}", output_path.display());
             }

             Ok(())
         }
     }
     ```

   - Tool path resolution:
     ```rust
     fn get_tool_path(&self, installation: &JdkInstallation, tool: &str) -> KopiResult<PathBuf> {
         let tool_name = if cfg!(windows) {
             format!("{}.exe", tool)
         } else {
             tool.to_string()
         };

         let tool_path = installation.path().join("bin").join(&tool_name);
         
         if !tool_path.exists() {
             return Err(KopiError::ToolNotFound {
                 tool: tool.to_string(),
                 jdk_path: installation.path().to_path_buf(),
             });
         }

         Ok(tool_path)
     }
     ```

   - Pattern matching for ambiguous versions:
     ```rust
     fn find_matching_jdk(&self, repo: &JdkRepository, spec: &JdkSpec) -> KopiResult<JdkInstallation> {
         let matches = repo.find_matching_jdks(spec)?;
         
         match matches.len() {
             0 => Err(KopiError::JdkNotInstalled {
                 jdk_spec: spec.clone(),
                 version: spec.version().to_string(),
                 auto_install_enabled: false,
             }),
             1 => Ok(matches.into_iter().next().unwrap()),
             _ => {
                 // Multiple matches - need disambiguation
                 let versions: Vec<String> = matches.iter()
                     .map(|jdk| format!("{}@{}", jdk.distribution(), jdk.version()))
                     .collect();
                 Err(KopiError::ValidationError(
                     format!("Multiple JDKs match version '{}'\n\nFound:\n  {}\n\nPlease specify the full version or distribution",
                         spec.version(),
                         versions.join("\n  ")
                     )
                 ))
             }
         }
     }
     ```

2. **Version Resolution Enhancement** (`/src/version/resolver.rs`)
   - Add `resolve_version_with_source()` if not already present:
     ```rust
     pub fn resolve_version_with_source() -> KopiResult<(Option<VersionRequest>, VersionSource)> {
         // Check KOPI_JAVA_VERSION environment variable
         if let Ok(version) = std::env::var("KOPI_JAVA_VERSION") {
             return Ok((Some(VersionRequest::parse(&version)?), VersionSource::Environment));
         }

         // Check project files
         if let Some((version, path)) = find_project_version_file()? {
             return Ok((Some(version), VersionSource::ProjectFile(path)));
         }

         // Check global configuration
         if let Some(version) = read_global_version()? {
             return Ok((Some(version), VersionSource::GlobalDefault));
         }

         Ok((None, VersionSource::None))
     }
     ```

3. **Error Type Enhancement** (`/src/error/mod.rs`)
   - Add `ToolNotFound` error variant if not present:
     ```rust
     #[derive(Error, Debug)]
     pub enum KopiError {
         // ... existing variants
         
         #[error("Tool '{tool}' not found in JDK installation at {jdk_path}")]
         ToolNotFound {
             tool: String,
             jdk_path: PathBuf,
         },
     }
     ```

4. **CLI Integration** (update `/src/main.rs`)
   - Add `Which` command to Commands enum:
     ```rust
     #[derive(Subcommand)]
     enum Commands {
         // ... existing commands
         
         /// Show installation path for a JDK version
         Which(which::WhichCommand),
         
         /// Show installation path for a JDK version (alias)
         #[command(visible_alias = "w")]
         W(which::WhichCommand),
     }
     ```

   - Add command routing:
     ```rust
     match cli.command {
         // ... existing matches
         Commands::Which(cmd) => cmd.execute()?,
         Commands::W(cmd) => cmd.execute()?,
     }
     ```

5. **Module Registration** (update `/src/commands/mod.rs`)
   ```rust
   pub mod which;
   ```

### Success Criteria
- Command correctly resolves current JDK when no version specified
- Specific version lookup works with pattern matching
- Tool path resolution works for various JDK tools
- `--home` option returns JDK directory instead of executable
- JSON output properly formatted
- Error messages clear and actionable
- Exit codes match error types from `src/error/exit_codes.rs`

## Phase 2: Testing and Polish

### Input Resources
- Phase 1 deliverables
- `/tests/common/` - Test utilities
- Existing test patterns from other commands

### Deliverables
1. **Unit Tests** (`/src/commands/which.rs` test module)
   ```rust
   #[cfg(test)]
   mod tests {
       use super::*;
       use tempfile::TempDir;
       use crate::test::fixtures::create_test_jdk;

       #[test]
       fn test_which_current_version() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           // Set up environment
           std::env::set_var("KOPI_JAVA_VERSION", "temurin@21");
           
           let cmd = WhichCommand {
               version: None,
               tool: "java".to_string(),
               home: false,
               json: false,
           };
           
           // Should find current version
           assert!(cmd.execute().is_ok());
       }

       #[test]
       fn test_which_specific_version() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           // Install test JDK
           create_test_jdk(&repo, "temurin", "21.0.5+11");
           
           let cmd = WhichCommand {
               version: Some("temurin@21".to_string()),
               tool: "java".to_string(),
               home: false,
               json: false,
           };
           
           assert!(cmd.execute().is_ok());
       }

       #[test]
       fn test_which_tool_not_found() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           create_test_jdk(&repo, "temurin", "21.0.5+11");
           
           let cmd = WhichCommand {
               version: Some("temurin@21".to_string()),
               tool: "nonexistent-tool".to_string(),
               home: false,
               json: false,
           };
           
           match cmd.execute() {
               Err(KopiError::ToolNotFound { tool, .. }) => {
                   assert_eq!(tool, "nonexistent-tool");
               }
               _ => panic!("Expected ToolNotFound error"),
           }
       }

       #[test]
       fn test_which_home_option() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           let jdk = create_test_jdk(&repo, "temurin", "21.0.5+11");
           
           let cmd = WhichCommand {
               version: Some("temurin@21".to_string()),
               tool: "java".to_string(),
               home: true,
               json: false,
           };
           
           // Should return JDK home, not executable path
           let output = capture_stdout(|| cmd.execute());
           assert_eq!(output.trim(), jdk.path().display().to_string());
       }

       #[test]
       fn test_which_json_output() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           create_test_jdk(&repo, "temurin", "21.0.5+11");
           
           let cmd = WhichCommand {
               version: Some("temurin@21".to_string()),
               tool: "javac".to_string(),
               home: false,
               json: true,
           };
           
           let output = capture_stdout(|| cmd.execute());
           let json: serde_json::Value = serde_json::from_str(&output).unwrap();
           
           assert_eq!(json["tool"], "javac");
           assert_eq!(json["distribution"], "temurin");
           assert_eq!(json["version"], "21.0.5+11");
       }

       #[test]
       fn test_ambiguous_version() {
           let temp_dir = TempDir::new().unwrap();
           let repo = create_test_repository(&temp_dir);
           
           // Install multiple JDKs with same major version
           create_test_jdk(&repo, "temurin", "21.0.5+11");
           create_test_jdk(&repo, "corretto", "21.0.7.6.1");
           
           let cmd = WhichCommand {
               version: Some("21".to_string()),
               tool: "java".to_string(),
               home: false,
               json: false,
           };
           
           match cmd.execute() {
               Err(KopiError::ValidationError(msg)) => {
                   assert!(msg.contains("Multiple JDKs match"));
                   assert!(msg.contains("temurin@21"));
                   assert!(msg.contains("corretto@21"));
               }
               _ => panic!("Expected ValidationError for ambiguous version"),
           }
       }
   }
   ```

2. **Integration Tests** (`/tests/which.rs`)
   ```rust
   #[path = "common/mod.rs"]
   mod common;

   use common::{TestHomeGuard, run_kopi_command};
   use predicates::prelude::*;

   #[test]
   fn test_which_command_basic() {
       let _guard = TestHomeGuard::new();
       
       // Install a JDK first
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       // Test basic which
       run_kopi_command(&["which", "temurin@21"])
           .assert()
           .success()
           .stdout(predicate::str::contains("/bin/java"));
   }

   #[test]
   fn test_which_current_project() {
       let _guard = TestHomeGuard::new();
       
       // Install and set local version
       run_kopi_command(&["install", "temurin@17"])
           .assert()
           .success();
       
       run_kopi_command(&["local", "temurin@17"])
           .assert()
           .success();
       
       // Which without version should find project version
       run_kopi_command(&["which"])
           .assert()
           .success()
           .stdout(predicate::str::contains("temurin-17"));
   }

   #[test]
   fn test_which_tools() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       // Test various tools
       for tool in &["java", "javac", "jar", "jshell"] {
           run_kopi_command(&["which", "--tool", tool, "temurin@21"])
               .assert()
               .success()
               .stdout(predicate::str::contains(tool));
       }
   }

   #[test]
   fn test_which_home_option() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       // Home option should not include /bin/java
       run_kopi_command(&["which", "--home", "temurin@21"])
           .assert()
           .success()
           .stdout(predicate::str::contains("temurin-21").and(
               predicate::str::contains("/bin/java").not()
           ));
   }

   #[test]
   fn test_which_json_format() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       let output = run_kopi_command(&["which", "--json", "temurin@21"])
           .assert()
           .success()
           .get_output()
           .stdout
           .clone();
       
       let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
       assert_eq!(json["distribution"], "temurin");
       assert_eq!(json["tool"], "java");
       assert!(json["tool_path"].as_str().unwrap().contains("java"));
   }

   #[test]
   fn test_which_not_installed() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["which", "temurin@22"])
           .assert()
           .failure()
           .code(4) // JdkNotInstalled
           .stderr(predicate::str::contains("not installed"));
   }

   #[test]
   fn test_which_tool_not_found() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       run_kopi_command(&["which", "--tool", "nonexistent", "temurin@21"])
           .assert()
           .failure()
           .code(5) // ToolNotFound
           .stderr(predicate::str::contains("Tool 'nonexistent' not found"));
   }
   ```

3. **Platform-Specific Tests** (`/tests/which_platform.rs`)
   ```rust
   #[cfg(windows)]
   #[test]
   fn test_which_windows_exe() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       // Windows should include .exe
       run_kopi_command(&["which", "temurin@21"])
           .assert()
           .success()
           .stdout(predicate::str::contains("java.exe"));
   }

   #[cfg(unix)]
   #[test]
   fn test_which_unix_no_exe() {
       let _guard = TestHomeGuard::new();
       
       run_kopi_command(&["install", "temurin@21"])
           .assert()
           .success();
       
       // Unix should not include .exe
       run_kopi_command(&["which", "temurin@21"])
           .assert()
           .success()
           .stdout(predicate::str::contains("/java").and(
               predicate::str::contains(".exe").not()
           ));
   }
   ```

4. **Benchmark Tests** (`/benches/which_bench.rs`)
   ```rust
   use criterion::{black_box, criterion_group, criterion_main, Criterion};
   use kopi::commands::which::WhichCommand;

   fn bench_which_current(c: &mut Criterion) {
       c.bench_function("which_current", |b| {
           b.iter(|| {
               let cmd = WhichCommand {
                   version: None,
                   tool: black_box("java".to_string()),
                   home: false,
                   json: false,
               };
               let _ = cmd.execute();
           });
       });
   }

   fn bench_which_specific(c: &mut Criterion) {
       c.bench_function("which_specific", |b| {
           b.iter(|| {
               let cmd = WhichCommand {
                   version: Some(black_box("temurin@21".to_string())),
                   tool: black_box("java".to_string()),
                   home: false,
                   json: false,
               };
               let _ = cmd.execute();
           });
       });
   }

   criterion_group!(benches, bench_which_current, bench_which_specific);
   criterion_main!(benches);
   ```

### Success Criteria
- All unit tests pass with good coverage
- Integration tests verify end-to-end functionality
- Platform-specific behavior tested on Windows and Unix
- Performance benchmarks show < 20ms typical execution time
- Error scenarios properly tested
- JSON output validates against expected schema

## Implementation Guidelines

### Development Process
1. Start with `/clear` command to reset context
2. Load this plan.md and design.md
3. Implement core functionality first
4. Add tests incrementally
5. Run quality checks after each module:
   - `cargo fmt`
   - `cargo clippy`
   - `cargo check`
   - `cargo test --lib --quiet`
   - `cargo test --test which` (integration tests)

### Code Quality Standards
- Use existing error types and patterns
- Follow Rust idioms and project conventions
- Document public APIs
- Handle all error cases explicitly
- Minimize allocations in hot paths

### Testing Strategy
- Unit tests use mocks for JdkRepository
- Integration tests use real filesystem
- Test both success and error paths
- Verify exit codes match specifications
- Test cross-platform behavior

## Design Principles

### Simplicity
- Command does one thing well: show paths
- Minimal output by default (just the path)
- No additional information unless requested

### Flexibility
- Support any JDK tool via `--tool`
- Provide JDK home with `--home`
- JSON output for scripting

### Consistency
- Reuse existing version resolution
- Match error handling patterns
- Follow project conventions

### Performance
- Fast execution (< 20ms target)
- Minimal file I/O
- Efficient path construction

## Success Metrics
- Command executes in < 20ms for typical use
- Exit codes correctly indicate error types
- Works reliably across platforms
- Integrates seamlessly with existing commands
- Clear, actionable error messages

## Dependencies Required
No new dependencies needed. Uses existing:
- **clap**: Command-line parsing
- **serde/serde_json**: JSON output
- **Standard library**: Path manipulation

## Next Steps
Begin with Phase 1, implementing the core WhichCommand module and integrating it with the CLI. Focus on getting basic functionality working before adding all features.