lib.rs (1598B)
1 use std::{env, ffi::OsString, process::ExitCode}; 2 3 use flags::{Kanit, KanitCmd, ServiceCmd}; 4 5 #[cfg(feature = "blame")] 6 mod blame; 7 mod flags; 8 mod service; 9 mod teardown; 10 11 fn handle_cli_inner(args: Vec<OsString>) -> ExitCode { 12 let res = match Kanit::from_vec(args) { 13 Ok(app) => match app.subcommand { 14 KanitCmd::Poweroff(opts) => teardown::teardown("poweroff", opts.force), 15 KanitCmd::Reboot(opts) => teardown::teardown("reboot", opts.force), 16 KanitCmd::Halt(opts) => teardown::teardown("halt", opts.force), 17 KanitCmd::Kexec(opts) => teardown::teardown("kexec", opts.force), 18 #[cfg(feature = "blame")] 19 KanitCmd::Blame(opts) => blame::blame(opts), 20 #[cfg(not(feature = "blame"))] 21 KanitCmd::Blame(_) => { 22 eprintln!("kanit compiled without blame"); 23 return ExitCode::FAILURE; 24 } 25 KanitCmd::Service(svc) => match svc.subcommand { 26 ServiceCmd::Enable(opts) => service::enable(opts), 27 ServiceCmd::Disable(opts) => service::disable(opts), 28 ServiceCmd::List(opts) => service::list(opts), 29 }, 30 }, 31 Err(e) => { 32 eprintln!("{e}"); 33 return ExitCode::FAILURE; 34 } 35 }; 36 37 if let Err(e) = res { 38 eprintln!("{e}"); 39 return ExitCode::FAILURE; 40 } 41 42 ExitCode::SUCCESS 43 } 44 45 pub fn handle_cli(pop_first: bool) -> ExitCode { 46 let mut args = env::args_os(); 47 if pop_first { 48 args.next(); 49 } 50 51 handle_cli_inner(args.collect()) 52 }