Add man page generation to bootstrap applet

This commit is contained in:
Nathan Fisher 2022-12-20 19:23:41 -05:00
parent a2e80dc867
commit aa1d311745
2 changed files with 65 additions and 4 deletions

View File

@ -1,8 +1,8 @@
use crate::cmd::{bootstrap, echo, head, hostname, r#false, r#true, sleep}; use crate::cmd::{bootstrap, echo, head, hostname, r#false, r#true, sleep};
use clap::Command; use clap::Command;
pub fn run() { pub fn cli() -> Command {
let matches = Command::new("shitbox") Command::new("shitbox")
.about("The Harbor Freight multitool of embedded Linux") .about("The Harbor Freight multitool of embedded Linux")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.arg_required_else_help(true) .arg_required_else_help(true)
@ -15,7 +15,10 @@ pub fn run() {
sleep::cli(), sleep::cli(),
r#true::cli(), r#true::cli(),
]) ])
.get_matches(); }
pub fn run() {
let matches = cli().get_matches();
match matches.subcommand() { match matches.subcommand() {
Some(("echo", _matches)) => echo::run(), Some(("echo", _matches)) => echo::run(),
Some(("false", _matches)) => r#false::run(), Some(("false", _matches)) => r#false::run(),

View File

@ -1,4 +1,18 @@
use crate::cmd;
use clap::{value_parser, Arg, ArgMatches, Command}; 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] #[must_use]
pub fn cli() -> Command { 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<u8> = 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::<String>("prefix") {
match matches.subcommand() {
Some(("manpages", _matches)) => {
if let Err(e) = manpages(&prefix) {
eprintln!("{e}");
process::exit(1);
}
}
_ => {}
}
}
}