use std::io::IsTerminal;
use std::time::{SystemTime, UNIX_EPOCH};
use engine::Timestamp;
use os::HostIdentity;
use vce::{Machine, OsKind, Session};
struct Options {
os: OsKind,
shell: Option<vce::ShellChoice>,
user: Option<String>,
password: Option<String>,
boot_time: Timestamp,
}
fn main() {
let arguments: Vec<String> = std::env::args().skip(1).collect();
if arguments
.iter()
.any(|argument| argument == "--help" || argument == "-h")
{
println!("{}", usage());
return;
}
if arguments.iter().any(|argument| argument == "--version") {
println!("vce {}", env!("CARGO_PKG_VERSION"));
return;
}
let options = match parse_args(arguments) {
Ok(options) => options,
Err(message) => {
eprintln!("{message}");
std::process::exit(2);
}
};
let identity = match &options.user {
Some(user) => {
let host = HostIdentity::from_host();
HostIdentity::new(user.clone(), host.hostname())
}
None => HostIdentity::from_host(),
};
let machine = match Machine::builder(options.os)
.username(identity.username())
.hostname(identity.hostname())
.boot_time(options.boot_time)
.build()
{
Ok(machine) => machine,
Err(error) => {
eprintln!("boot failed: {error}");
std::process::exit(1);
}
};
let interactive = std::io::stdin().is_terminal();
let session = if interactive && options.password.is_none() {
machine.interactive_session(options.shell)
} else {
let password = options
.password
.as_deref()
.unwrap_or_else(|| machine.default_user());
machine.login(machine.default_user(), password, options.shell)
};
let session = match session {
Ok(session) => session,
Err(error) => {
eprintln!("login failed: {error}");
std::process::exit(1);
}
};
run_session(session);
}
fn run_session<'os>(session: Session<'os>) -> ! {
let status = if std::io::stdin().is_terminal() {
session.run_interactive()
} else {
let mut stdin = std::io::stdin();
session.run_line(&mut stdin)
};
std::process::exit(status);
}
fn parse_args(args: Vec<String>) -> Result<Options, String> {
let mut os_choice = OsKind::Linux;
let mut shell = None;
let mut user = None;
let mut password = None;
let mut index = 0;
while index < args.len() {
match args[index].as_str() {
"--os" => {
index += 1;
let value = args
.get(index)
.ok_or_else(|| "missing value for --os".to_owned())?;
os_choice = match value.as_str() {
"linux" => OsKind::Linux,
"macos" => OsKind::MacOS,
"windows" => OsKind::Windows,
other => {
return Err(format!("unknown OS '{other}' (linux, macos, windows)"));
}
};
}
"--shell" => {
index += 1;
let value = args
.get(index)
.ok_or_else(|| "missing value for --shell".to_owned())?;
shell = Some(match value.as_str() {
"bash" => vce::ShellChoice::Bash,
"zsh" => vce::ShellChoice::Zsh,
"powershell" | "pwsh" => vce::ShellChoice::PowerShell,
"cmd" => vce::ShellChoice::Cmd,
other => {
return Err(format!(
"unknown shell '{other}' (bash, zsh, powershell, cmd; the default follows the OS's default shell)"
));
}
});
}
"--user" => {
index += 1;
user = Some(
args.get(index)
.ok_or_else(|| "missing value for --user".to_owned())?
.clone(),
);
}
"--password" => {
index += 1;
password = Some(
args.get(index)
.ok_or_else(|| "missing value for --password".to_owned())?
.clone(),
);
}
other => {
return Err(format!("unknown argument '{other}'\n{}", usage()));
}
}
index += 1;
}
let boot_time = Timestamp::from_nanos(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
.try_into()
.unwrap_or(u64::MAX),
);
Ok(Options {
os: os_choice,
shell,
user,
password,
boot_time,
})
}
fn usage() -> String {
"usage: vce [--os linux|macos|windows] [--shell bash|zsh|powershell|cmd] [--user NAME] [--password WORD] [--help] [--version]".to_owned()
}