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
// 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 clap::{Parser, Subcommand};
use kopi::commands::cache::CacheCommand;
use kopi::commands::current::CurrentCommand;
use kopi::commands::doctor::DoctorCommand;
use kopi::commands::env::EnvCommand;
use kopi::commands::global::GlobalCommand;
use kopi::commands::install::InstallCommand;
use kopi::commands::list::ListCommand;
use kopi::commands::local::LocalCommand;
use kopi::commands::setup::SetupCommand;
use kopi::commands::shell::ShellCommand;
use kopi::commands::shim::ShimCommand;
use kopi::commands::uninstall::UninstallCommand;
use kopi::commands::which::WhichCommand;
use kopi::config::new_kopi_config;
use kopi::error::{Result, format_error_chain, get_exit_code};
use kopi::logging;
use log::warn;
#[derive(Parser)]
#[command(name = "kopi")]
#[command(author, version, about = "JDK version management tool", long_about = None)]
struct Cli {
/// Increase verbosity (-v info, -vv debug, -vvv trace)
#[arg(short, long, action = clap::ArgAction::Count, global = true)]
verbose: u8,
/// Disable progress indicators
#[arg(long, global = true)]
no_progress: bool,
/// Override lock acquisition timeout (seconds or 'infinite')
#[arg(long, value_name = "SECONDS|infinite", global = true)]
lock_timeout: Option<String>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Install a JDK version
#[command(visible_alias = "i")]
Install {
/// Version to install (e.g., "21", "17.0.9", "corretto@21")
version: String,
/// Force reinstall even if already installed
#[arg(short, long)]
force: bool,
/// Show what would be installed without actually installing
#[arg(long)]
dry_run: bool,
/// Download timeout in seconds
#[arg(long, value_name = "SECONDS")]
timeout: Option<u64>,
},
/// List installed JDK versions
#[command(visible_alias = "ls")]
List,
/// Set JDK version for current shell session
#[command(visible_alias = "use")]
Shell {
/// JDK version to use
version: String,
/// Override shell detection
#[arg(long)]
shell: Option<String>,
},
/// Show currently active JDK version
Current {
/// Show only version number
#[arg(short = 'q', long)]
quiet: bool,
/// Output in JSON format
#[arg(long)]
json: bool,
},
/// Output environment variables for shell evaluation
///
/// Sets JAVA_HOME for the current JDK. Use with eval/source in your shell.
#[command(long_about = "Output environment variables for shell evaluation
Sets JAVA_HOME for the current or specified JDK version.
Examples:
eval \"$(kopi env)\" # Bash/Zsh
kopi env | source # Fish
kopi env | Invoke-Expression # PowerShell")]
Env {
/// Specific version to use (defaults to current)
version: Option<String>,
/// Override shell detection
#[arg(long)]
shell: Option<String>,
/// Output export statements (default: true)
#[arg(long, default_value_t = true, action = clap::ArgAction::Set)]
export: bool,
},
/// Set the global default JDK version
#[command(visible_alias = "g", alias = "default")]
Global {
/// Version to set as global default
version: String,
},
/// Set the local project JDK version
#[command(visible_alias = "l", alias = "pin")]
Local {
/// Version to set for current project
version: String,
},
/// Show installation path for a JDK version
#[command(visible_alias = "w")]
Which {
/// 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,
},
/// Manage JDK metadata cache
Cache {
#[command(subcommand)]
command: CacheCommand,
},
/// Refresh JDK metadata cache (alias for cache refresh)
#[command(visible_alias = "r", hide = true)]
Refresh,
/// Search available JDK versions (alias for cache search)
#[command(visible_alias = "s", aliases = ["ls-remote", "list-remote"], hide = true)]
Search {
/// Version pattern to search (e.g., "21", "corretto", "corretto@17")
#[arg(value_name = "VERSION")]
version: Option<String>,
/// Show compact output (version numbers only)
#[arg(short, long, conflicts_with = "detailed")]
compact: bool,
/// Show detailed information including download URLs
#[arg(short, long, conflicts_with = "compact")]
detailed: bool,
/// Output results as JSON
#[arg(long, conflicts_with_all = ["compact", "detailed"])]
json: bool,
/// Show only LTS versions
#[arg(long)]
lts_only: bool,
},
/// Initial setup and configuration
Setup {
/// Force recreation of shims even if they exist
#[arg(short, long)]
force: bool,
},
/// Manage tool shims
Shim {
#[command(subcommand)]
command: ShimCommand,
},
/// Uninstall a JDK version
#[command(visible_alias = "u", alias = "remove")]
Uninstall {
/// Version to uninstall (e.g., "21", "17.0.9", "corretto@21")
version: Option<String>,
/// Skip confirmation prompts
#[arg(short, long)]
force: bool,
/// Show what would be uninstalled without actually removing
#[arg(long)]
dry_run: bool,
/// Uninstall all versions of a distribution
#[arg(long)]
all: bool,
/// Clean up failed or partial uninstall operations
#[arg(long)]
cleanup: bool,
},
/// Run diagnostics on kopi installation
Doctor {
/// Output results in JSON format
#[arg(long)]
json: bool,
/// Run only specific category of checks
#[arg(long, value_name = "CATEGORY")]
check: Option<String>,
},
}
fn setup_logger(cli: &Cli) {
logging::setup_logger(cli.verbose);
}
fn main() {
let cli = Cli::parse();
// Initialize logger based on CLI flags and environment
setup_logger(&cli);
// Load configuration once at startup
let mut config = match new_kopi_config() {
Ok(config) => config,
Err(e) => {
eprintln!("{}", format_error_chain(&e));
std::process::exit(get_exit_code(&e));
}
};
if let Err(e) = config.apply_lock_timeout_overrides(cli.lock_timeout.as_deref()) {
eprintln!("{}", format_error_chain(&e));
std::process::exit(get_exit_code(&e));
}
if let Err(err) = kopi::locking::run_startup_hygiene(config.kopi_home(), &config.locking) {
warn!("Lock hygiene sweep failed: {err}");
}
let result: Result<()> = (|| {
match cli.command {
Commands::Install {
version,
force,
dry_run,
timeout,
} => {
let command = InstallCommand::new(&config, cli.no_progress)?;
command.execute(&version, force, dry_run, timeout)
}
Commands::List => {
let command = ListCommand::new(&config)?;
command.execute()
}
Commands::Shell { version, shell } => {
let command = ShellCommand::new(&config, cli.no_progress)?;
command.execute(&version, shell.as_deref())
}
Commands::Current { quiet, json } => {
let command = CurrentCommand::new(&config)?;
command.execute(quiet, json)
}
Commands::Env {
version,
shell,
export,
} => {
let command = EnvCommand::new(&config)?;
command.execute(version.as_deref(), shell.as_deref(), export)
}
Commands::Global { version } => {
let command = GlobalCommand::new(&config, cli.no_progress)?;
command.execute(&version)
}
Commands::Local { version } => {
let command = LocalCommand::new(&config, cli.no_progress)?;
command.execute(&version)
}
Commands::Which {
version,
tool,
home,
json,
} => {
let command = WhichCommand::new(&config)?;
command.execute(version.as_deref(), &tool, home, json)
}
Commands::Cache { command } => command.execute(&config, cli.no_progress),
Commands::Refresh => {
// Delegate to cache refresh command
let cache_cmd = CacheCommand::Refresh;
cache_cmd.execute(&config, cli.no_progress)
}
Commands::Search {
version,
compact,
detailed,
json,
lts_only,
} => {
// Delegate to cache search command
let cache_cmd = CacheCommand::Search {
version: version.unwrap_or_else(|| "latest".to_string()),
compact,
detailed,
json,
lts_only,
java_version: false,
distribution_version: false,
};
cache_cmd.execute(&config, cli.no_progress)
}
Commands::Setup { force } => {
let command = SetupCommand::new(&config, cli.no_progress)?;
command.execute(force)
}
Commands::Shim { command } => command.execute(&config),
Commands::Uninstall {
version,
force,
dry_run,
all,
cleanup,
} => {
let command = UninstallCommand::new(&config, cli.no_progress)?;
command.execute(
version.as_deref(),
force,
dry_run,
all,
cleanup,
cli.no_progress,
)
}
Commands::Doctor { json, check } => {
let command = DoctorCommand::new(&config)?;
command.execute(json, cli.verbose > 0, check.as_deref())
}
}
})();
if let Err(e) = result {
eprintln!("{}", format_error_chain(&e));
std::process::exit(get_exit_code(&e));
}
}