#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]
use std::env;
use std::process::{Command, ExitCode};
#[cfg(target_os = "windows")]
use std::io;
#[cfg(target_os = "windows")]
use std::path::Path;
#[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 mut exe_path = match env::current_exe() {
Ok(p) => p,
Err(_) => return ExitCode::from(1),
};
exe_path.pop();
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(_) => ExitCode::from(1),
};
}
match spawn_windows_open(&exe_path, args) {
Ok(status) => exit_code_from_status(status),
Err(_) => ExitCode::from(1),
}
}
#[cfg(not(target_os = "windows"))]
{
#[cfg(target_os = "linux")]
let mut cmd = if args.is_empty() {
let setup = exe_path.join(SETUP_BIN);
if !setup.exists() {
return ExitCode::from(1);
}
Command::new(setup)
} else {
let mut cmd = Command::new(exe_path.join(MDO_BIN));
cmd.arg("--open");
cmd.args(args);
cmd
};
#[cfg(not(target_os = "linux"))]
let mut cmd = {
let mut cmd = Command::new(exe_path.join(MDO_BIN));
cmd.arg("--open");
cmd.args(args);
cmd
};
match cmd.status() {
Ok(status) => exit_code_from_status(status),
Err(_) => ExitCode::from(1),
}
}
}
fn exit_code_from_status(status: std::process::ExitStatus) -> ExitCode {
if status.success() {
return ExitCode::SUCCESS;
}
match status.code() {
Some(code) if code as u8 != 0 => ExitCode::from(code as u8),
_ => ExitCode::FAILURE,
}
}
#[cfg(target_os = "windows")]
fn spawn_windows_open(
exe_dir: &Path,
args: Vec<std::ffi::OsString>,
) -> io::Result<std::process::ExitStatus> {
let mut cmd = Command::new(exe_dir.join(MDO_BIN));
cmd.arg("--open");
cmd.args(args);
spawn_without_console(cmd)
}
#[cfg(target_os = "windows")]
fn spawn_windows_onboarding(exe_dir: &Path) -> io::Result<()> {
let mdo = exe_dir.join(MDO_BIN);
if !mdo.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!(
"expected {MDO_BIN} next to mdo-open.exe at {}",
mdo.display()
),
));
}
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()
}