2022-11-06 17:44:23 +00:00
|
|
|
//! The `cli-reference` subcommand, only available in debug mode.
|
|
|
|
|
|
|
|
use std::{fs::write, process::Command, str};
|
|
|
|
|
|
|
|
use {
|
|
|
|
color_eyre::Result,
|
|
|
|
hooked_config::Config,
|
|
|
|
tera::{Context, Tera},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::cli::CliReferenceArgs;
|
|
|
|
|
2022-11-07 13:42:08 +00:00
|
|
|
/// The CLI reference template.
|
2022-11-06 17:44:23 +00:00
|
|
|
const REFERENCE_TEMPLATE: &str = include_str!("../templates/cli-reference.txt");
|
|
|
|
|
|
|
|
/// The `cli-reference` subcommand.
|
|
|
|
pub fn hooked_cli_reference(
|
|
|
|
_config: Config,
|
|
|
|
args: CliReferenceArgs,
|
|
|
|
) -> Result<()> {
|
|
|
|
let out_path = if args.output.is_dir() {
|
|
|
|
args.output.join("cli-reference.txt")
|
|
|
|
} else {
|
|
|
|
args.output
|
|
|
|
};
|
|
|
|
|
|
|
|
let commands = {
|
|
|
|
let mut commands = vec![];
|
|
|
|
let commands_to_document = &["install", "run", "uninstall"];
|
|
|
|
|
|
|
|
for command_name in commands_to_document {
|
|
|
|
let output = Command::new("cargo")
|
|
|
|
.env("NO_COLOR", "1")
|
2024-01-17 17:10:25 +00:00
|
|
|
.args(["run", "-q", "--", command_name, "--help"])
|
2022-11-06 17:44:23 +00:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let usage = str::from_utf8(&output.stdout).unwrap().trim().to_string();
|
|
|
|
commands.push((command_name.to_string(), usage))
|
|
|
|
}
|
|
|
|
|
|
|
|
commands
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.insert("commands", &commands);
|
|
|
|
write(
|
2024-01-17 17:10:25 +00:00
|
|
|
out_path,
|
2022-11-06 17:44:23 +00:00
|
|
|
Tera::one_off(REFERENCE_TEMPLATE, &context, false)?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|