-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmain.rs
127 lines (107 loc) · 3.74 KB
/
main.rs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Generate README.md from doc comments.
use clap::Parser;
use std::io;
use std::io::Write;
mod helper;
fn main() {
let args = Args::parse();
let result = match &args.command {
Command::Readme(readme_args) => execute(readme_args),
};
match result {
Err(e) => {
io::stderr()
.write_fmt(format_args!("Error: {}\n", e))
.expect("An error occurred while trying to show an error message");
std::process::exit(1);
}
_ => {}
}
}
/// The command line interface for setting up a Bottlerocket TestSys cluster and running tests.
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct Args {
#[clap(subcommand)]
command: Command,
}
#[derive(Debug, Parser)]
enum Command {
/// Generate README.md from doc comments
Readme(ReadmeArgs),
}
/// Generate README.md from doc comments
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct ReadmeArgs {
/// Do not prepend badges line.
/// By default, badges defined in Cargo.toml are prepended to the output.
/// Ignored when using a template.
#[clap(long)]
no_badges: bool,
/// Do not add an extra level to headings.
/// By default, '#' headings become '##', so the first '#' can be the crate name. Use this
/// option to prevent this behavior.
#[clap(long)]
no_indent_headings: bool,
/// Do not append license line.
/// By default, the license defined in `Cargo.toml` will be prepended to the output.
/// Ignored when using a template.
#[clap(long)]
no_license: bool,
/// Ignore template file when generating README.
/// Only useful to ignore default template `README.tpl`.
#[clap(long)]
no_template: bool,
/// Do not prepend title line.
/// By default, the title ('# crate-name') is prepended to the output.
#[clap(long)]
no_title: bool,
/// File to read from.
/// If not provided, will try to use `src/lib.rs`, then `src/main.rs`. If neither file
/// could be found, will look into `Cargo.toml` for a `[lib]`, then for a single `[[bin]]`.
/// If multiple binaries are found, an error will be returned.
#[clap(long, short = 'i')]
input: Option<String>,
/// File to write to. If not provided, will output to stdout.
#[clap(long, short = 'o')]
output: Option<String>,
/// Directory to be set as project root (where `Cargo.toml` is)
/// Defaults to the current directory.
#[clap(long = "project-root", short = 'r')]
root: Option<String>,
/// Template used to render the output.
/// Default behavior is to use `README.tpl` if it exists.
#[clap(long, short = 't')]
template: Option<String>,
}
// Takes the arguments matches from clap and outputs the result, either to stdout of a file
fn execute(args: &ReadmeArgs) -> Result<(), String> {
// get project root
let project_root = helper::get_project_root(args.root.as_deref())?;
// get source file
let mut source = helper::get_source(&project_root, args.input.as_deref())?;
// get destination file
let mut dest = helper::get_dest(&project_root, args.output.as_deref())?;
// get template file
let mut template_file = if args.no_template {
None
} else {
helper::get_template_file(&project_root, args.template.as_deref())?
};
let add_title = !args.no_title;
let add_badges = !args.no_badges;
let add_license = !args.no_license;
let indent_headings = !args.no_indent_headings;
// generate output
let readme = cargo_readme::generate_readme(
&project_root,
&mut source,
template_file.as_mut(),
add_title,
add_badges,
add_license,
indent_headings,
)?;
helper::write_output(&mut dest, readme)
}