forked from sdkman/sdkman-cli-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
61 lines (53 loc) · 1.65 KB
/
main.rs
File metadata and controls
61 lines (53 loc) · 1.65 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
use std::env;
use std::process::Command;
use clap::Parser;
use colored::Colorize;
use sdkman_cli_native::helpers::infer_sdkman_dir;
#[derive(Parser, Debug)]
#[command(
bin_name = "sdk config",
about = "sdk subcommand to edit the SDKMAN configuration file"
)]
struct Args;
fn main() {
let sdkman_dir = infer_sdkman_dir();
let config_path = sdkman_dir.join("etc").join("config");
let editor = env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
let mut parts = editor.split_whitespace();
let cmd = match parts.next() {
Some(c) => c,
None => {
eprintln!("{}", "No default editor configured.".red());
println!(
"{}",
"Please set the default editor with the EDITOR environment variable.".yellow()
);
std::process::exit(1);
}
};
let extra_args: Vec<&str> = parts.collect();
if which_exists(cmd) {
let mut command = Command::new(cmd);
command.args(&extra_args);
command.arg(&config_path);
let status = command.status().expect("failed to execute editor");
std::process::exit(status.code().unwrap_or(1));
} else {
eprintln!(
"{}",
format!("Editor '{}' not found. Please set EDITOR to a valid editor.", cmd).red()
);
println!(
"{}",
"Please set the default editor with the EDITOR environment variable.".yellow()
);
std::process::exit(1);
}
}
fn which_exists(cmd: &str) -> bool {
Command::new("which")
.arg(cmd)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}