mod cli;
use clap::Parser;
use lernie::cmd::{Cli, Fx, Outcome, prelude};
use std::io;
use std::path::Path;
use std::process::ExitCode;
fn main() -> ExitCode {
let cli = Cli::parse();
for prelude in cli.command.preludes() {
prelude();
}
let driver_target = match std::env::current_exe() {
Ok(p) => p,
Err(e) => {
eprintln!("lernie: resolve current executable: {e}");
return ExitCode::FAILURE;
}
};
let editor: fn(&Path) -> io::Result<()> = cli::edit_in_editor;
let result = {
let mut stdin = io::stdin().lock();
let mut stdout = io::stdout().lock();
let mut stderr = io::stderr().lock();
let mut fx = Fx {
driver_target,
adapter_target: None,
editor: &editor,
tool_stdin: &mut stdin,
tool_stdout: &mut stdout,
tool_stderr: &mut stderr,
stop: prelude::stop_flag(),
};
cli.command.run(&mut fx)
};
match result {
Ok(outcome) => perform(outcome),
Err(e) => {
eprintln!("{e}");
ExitCode::FAILURE
}
}
}
fn perform(outcome: Outcome) -> ExitCode {
match outcome {
Outcome::Line(line) => {
println!("{line}");
ExitCode::SUCCESS
}
Outcome::Quiet => ExitCode::SUCCESS,
Outcome::Exec(mut cmd) => {
use std::os::unix::process::CommandExt;
eprintln!("lernie advance: exec successor: {}", cmd.exec());
ExitCode::FAILURE
}
Outcome::Code(code) => ExitCode::from(code),
}
}