#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]
use std::env;
use std::process::{Command, ExitCode};
#[cfg(target_os = "windows")]
use std::io;
use mdo_cli::handler;
#[cfg(target_os = "windows")]
const MDO_BIN: &str = "mdo.exe";
#[cfg(not(target_os = "windows"))]
const MDO_BIN: &str = "mdo";
#[cfg(target_os = "linux")]
const SETUP_BIN: &str = "mdo-setup";
fn main() -> ExitCode {
let exe_path = match env::current_exe() {
Ok(p) => p,
Err(e) => {
eprintln!("mdo-open: cannot locate its own executable: {e}");
return ExitCode::from(1);
}
};
let args = env::args_os().skip(1).collect::<Vec<_>>();
#[cfg(target_os = "windows")]
{
if args.is_empty() {
return match spawn_windows_onboarding(&exe_path) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("mdo-open: {e}");
ExitCode::from(1)
}
};
}
match spawn_windows_open(&exe_path, args) {
Ok(status) => exit_code_from_status(status),
Err(e) => {
eprintln!("mdo-open: {e}");
ExitCode::from(1)
}
}
}
#[cfg(not(target_os = "windows"))]
{
#[cfg(target_os = "linux")]
if args.is_empty() {
let setup = match handler::sibling_binary(&exe_path, SETUP_BIN) {
Ok(path) => path,
Err(e) => {
eprintln!("mdo-open: {e}");
return ExitCode::from(1);
}
};
let mut cmd = Command::new(setup);
match cmd.status() {
Ok(status) => return exit_code_from_status(status),
Err(e) => {
eprintln!("mdo-open: {e}");
return ExitCode::from(1);
}
}
}
let mdo_bin = match handler::sibling_binary(&exe_path, MDO_BIN) {
Ok(path) => path,
Err(e) => {
eprintln!("mdo-open: {e}");
return ExitCode::from(1);
}
};
let mut cmd = Command::new(mdo_bin);
cmd.args(handler::open_argv(&args));
match cmd.status() {
Ok(status) => exit_code_from_status(status),
Err(e) => {
eprintln!("mdo-open: {e}");
ExitCode::from(1)
}
}
}
}
fn exit_code_from_status(status: std::process::ExitStatus) -> ExitCode {
exit_code_from_parts(status.success(), status.code())
}
fn exit_code_from_parts(success: bool, code: Option<i32>) -> ExitCode {
if success {
return ExitCode::SUCCESS;
}
match code {
Some(code) if code as u8 != 0 => ExitCode::from(code as u8),
_ => ExitCode::FAILURE,
}
}
#[cfg(target_os = "windows")]
fn spawn_windows_open(
anchor_exe: &std::path::Path,
args: Vec<std::ffi::OsString>,
) -> io::Result<std::process::ExitStatus> {
let mdo_bin = handler::sibling_binary(anchor_exe, MDO_BIN)?;
let mut cmd = Command::new(mdo_bin);
cmd.args(handler::open_argv(&args));
spawn_without_console(cmd)
}
#[cfg(target_os = "windows")]
fn spawn_windows_onboarding(anchor_exe: &std::path::Path) -> io::Result<()> {
let mdo = handler::sibling_binary(anchor_exe, MDO_BIN)?;
mdo_cli::windows_setup::spawn_terminal_setup(&mdo)
}
#[cfg(target_os = "windows")]
fn spawn_without_console(mut cmd: Command) -> io::Result<std::process::ExitStatus> {
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
cmd.creation_flags(CREATE_NO_WINDOW);
let mut child = cmd.spawn()?;
child.wait()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_code_success_is_success() {
assert_eq!(exit_code_from_parts(true, Some(0)), ExitCode::SUCCESS);
assert_eq!(exit_code_from_parts(true, None), ExitCode::SUCCESS);
}
#[test]
fn small_exit_codes_pass_through() {
assert_eq!(exit_code_from_parts(false, Some(1)), ExitCode::from(1));
assert_eq!(exit_code_from_parts(false, Some(7)), ExitCode::from(7));
assert_eq!(exit_code_from_parts(false, Some(255)), ExitCode::from(255));
}
#[test]
fn failure_with_zero_low_byte_must_not_read_as_success() {
assert_eq!(exit_code_from_parts(false, Some(0x100)), ExitCode::FAILURE);
assert_eq!(exit_code_from_parts(false, Some(0x200)), ExitCode::FAILURE);
assert_eq!(exit_code_from_parts(false, Some(0xFF00)), ExitCode::FAILURE);
}
#[test]
fn unknown_termination_falls_back_to_failure() {
assert_eq!(exit_code_from_parts(false, None), ExitCode::FAILURE);
}
#[cfg(target_os = "windows")]
#[test]
fn real_windows_exit_code_256_means_failure() {
let status = Command::new("cmd.exe")
.args(["/C", "exit", "256"])
.status()
.expect("cmd.exe should be spawnable on Windows");
assert!(!status.success());
assert_eq!(status.code(), Some(256));
assert_eq!(exit_code_from_status(status), ExitCode::FAILURE);
}
}