diff --git a/src/cli.rs b/src/cli.rs index de73306..1b56810 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,8 @@ use crate::cmd::{bootstrap, echo, head, hostname, r#false, r#true, sleep}; use clap::Command; -pub fn run() { - let matches = Command::new("shitbox") +pub fn cli() -> Command { + Command::new("shitbox") .about("The Harbor Freight multitool of embedded Linux") .version(env!("CARGO_PKG_VERSION")) .arg_required_else_help(true) @@ -15,7 +15,10 @@ pub fn run() { sleep::cli(), r#true::cli(), ]) - .get_matches(); +} + +pub fn run() { + let matches = cli().get_matches(); match matches.subcommand() { Some(("echo", _matches)) => echo::run(), Some(("false", _matches)) => r#false::run(), diff --git a/src/cmd/bootstrap/mod.rs b/src/cmd/bootstrap/mod.rs index 14d5514..c1b435c 100644 --- a/src/cmd/bootstrap/mod.rs +++ b/src/cmd/bootstrap/mod.rs @@ -1,4 +1,18 @@ +use crate::cmd; use clap::{value_parser, Arg, ArgMatches, Command}; +use clap_mangen::Man; +use std::{fs, io, path::PathBuf, process}; + +const programs: [&str; 8] = [ + "shitbox", + "bootstrap", + "echo", + "false", + "head", + "hostname", + "true", + "sleep", +]; #[must_use] pub fn cli() -> Command { @@ -67,4 +81,48 @@ pub fn cli() -> Command { ]) } -pub fn run(matches: &ArgMatches) {} +fn manpage(prefix: &str, cmd: &str) -> Result<(), io::Error> { + let (fname, cmd) = match cmd { + "shitbox" => ("shitbox.1", crate::cli::cli()), + "bootstrap" => ("shitbox-bootstrap.1", cli()), + "echo" => ("echo.1", cmd::echo::cli()), + "false" => ("false.1", cmd::r#false::cli()), + "head" => ("head.1", cmd::head::cli()), + "hostname" => ("hostname.1", cmd::hostname::cli()), + "true" => ("true.1", cmd::r#true::cli()), + "sleep" => ("sleep.1", cmd::sleep::cli()), + _ => unimplemented!(), + }; + let outdir: PathBuf = [prefix, "usr", "share", "man", "man1"].iter().collect(); + if !outdir.exists() { + fs::create_dir_all(&outdir)?; + } + let mut outfile = outdir; + outfile.push(fname); + let man = Man::new(cmd); + let mut buffer: Vec = vec![]; + man.render(&mut buffer)?; + fs::write(&outfile, buffer)?; + println!(" {}", outfile.display()); + Ok(()) +} + +fn manpages(prefix: &str) -> Result<(), io::Error> { + println!("Generating Unix man pages:"); + programs.iter().try_for_each(|cmd| manpage(prefix, cmd))?; + Ok(()) +} + +pub fn run(matches: &ArgMatches) { + if let Some(prefix) = matches.get_one::("prefix") { + match matches.subcommand() { + Some(("manpages", _matches)) => { + if let Err(e) = manpages(&prefix) { + eprintln!("{e}"); + process::exit(1); + } + } + _ => {} + } + } +}