-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdocs.rs
More file actions
67 lines (55 loc) · 1.74 KB
/
docs.rs
File metadata and controls
67 lines (55 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::{
fs::{self},
path::Path,
};
use clap::CommandFactory;
use snafu::{ResultExt, Snafu};
use stackablectl::cli::Cli;
const DOCS_BASE_PATH: &str = "docs/modules/stackablectl/partials/commands";
#[derive(Debug, Snafu)]
pub enum GenDocsError {
#[snafu(display("io error"))]
Io { source: std::io::Error },
#[snafu(display("templating error"))]
TemplateError { source: tera::Error },
}
pub fn generate() -> Result<(), GenDocsError> {
let mut cli = Cli::command();
cli.build();
let mut renderer = tera::Tera::default();
renderer
.add_raw_template(
"command_partial",
include_str!("templates/command.adoc.tpl"),
)
.context(TemplateSnafu)?;
for cmd in cli.get_subcommands().chain([&cli]) {
let usage_text = cmd.clone().render_long_help().to_string();
// Needed to remove trailing whitespaces in empty lines
let usage_text: Vec<_> = usage_text.lines().map(|l| l.trim_end()).collect();
let usage_text = usage_text.join("\n");
let page_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(Path::parent)
.unwrap()
.join(DOCS_BASE_PATH)
.join(format!(
"{name}.adoc",
name = if cmd.get_name() == cli.get_name() {
"index"
} else {
cmd.get_name()
}
));
let mut context = tera::Context::new();
context.insert("output", &usage_text);
fs::write(
page_path,
renderer
.render("command_partial", &context)
.context(TemplateSnafu)?,
)
.context(IoSnafu)?
}
Ok(())
}