Skip to content

Commit

Permalink
moss/cli: Add support for --generate-manpages
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <ikey@serpentos.com>
  • Loading branch information
ikeycode committed Nov 6, 2024
1 parent 9c964b0 commit 4dc67aa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ blsforme = { git = "https://github.com/serpent-os/blsforme.git", rev = "59a8f12f
bytes = "1.6.0"
chrono = "0.4.38"
clap = { version = "4.5.8", features = ["derive", "string"] }
clap_complete = "4.5.37"
clap_mangen = "0.2.24"
criterion = { version = "0.5.1", features = ["html_reports"] }
crossterm = "0.27.0"
derive_more = "0.99.18"
Expand Down
2 changes: 2 additions & 0 deletions moss/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ vfs = { path = "../crates/vfs" }
blsforme.workspace = true
bytes.workspace = true
chrono.workspace = true
clap_mangen.workspace = true
clap_complete.workspace = true
clap.workspace = true
derive_more.workspace = true
diesel.workspace = true
Expand Down
45 changes: 45 additions & 0 deletions moss/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::{env, path::PathBuf};

use clap::{Arg, ArgAction, Command};
use clap_mangen::Man;
use moss::{installation, runtime, Installation};
use thiserror::Error;

Expand Down Expand Up @@ -59,6 +60,14 @@ fn command() -> Command {
.help("Assume yes for all questions")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("generate-manpages")
.long("generate-manpages")
.help("Generate manpages")
.action(ArgAction::Set)
.value_name("DIR")
.hide(true),
)
.arg_required_else_help(true)
.subcommand(extract::command())
.subcommand(index::command())
Expand All @@ -74,11 +83,44 @@ fn command() -> Command {
.subcommand(version::command())
}

/// Generate manpages for all commands recursively
fn generate_manpages(cmd: &Command, dir: &std::path::Path, prefix: Option<&str>) -> std::io::Result<()> {
let name = cmd.get_name();
let man = Man::new(cmd.to_owned());
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;

let filename = if let Some(prefix) = prefix {
format!("{}-{}.1", prefix, name)
} else {
format!("{}.1", name)
};

std::fs::write(dir.join(filename), buffer)?;

for subcmd in cmd.get_subcommands() {
let new_prefix = if let Some(p) = prefix {
format!("{}-{}", p, name)
} else {
name.to_string()
};
generate_manpages(subcmd, dir, Some(&new_prefix))?;
}
Ok(())
}

/// Process all CLI arguments
pub fn process() -> Result<(), Error> {
let args = replace_aliases(env::args());
let matches = command().get_matches_from(args);

if let Some(dir) = matches.get_one::<String>("generate-manpages") {
let dir = std::path::Path::new(dir);
std::fs::create_dir_all(dir)?;
generate_manpages(&command(), dir, None)?;
return Ok(());
}

// Print the version, but not if the user is using the version subcommand
if matches.get_flag("verbose") {
if let Some(command) = matches.subcommand_name() {
Expand Down Expand Up @@ -190,4 +232,7 @@ pub enum Error {

#[error("installation")]
Installation(#[from] installation::Error),

#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}

0 comments on commit 4dc67aa

Please sign in to comment.