Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --maskfile-introspect flag to print out the maskfile command structure as json #85

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions mask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT"

[dependencies]
colored = "2" # https://github.com/mackwic/colored
serde_json = "1.0" # https://github.com/serde-rs/json
mask-parser = { path = "../mask-parser" }

[dependencies.clap] # https://github.com/clap-rs/clap
Expand Down
33 changes: 29 additions & 4 deletions mask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fn main() {
.setting(AppSettings::SubcommandRequired)
.setting(AppSettings::ColoredHelp)
.version(crate_version!())
.arg(custom_maskfile_path_arg());
.arg(custom_maskfile_path_arg())
.arg(introspect_arg());

let (maskfile, maskfile_path) = find_maskfile();
if maskfile.is_err() {
Expand All @@ -24,6 +25,13 @@ fn main() {
}

let root = mask_parser::parse(maskfile.unwrap());

if is_introspecting() {
let json = root.to_json().expect("to_json failed");
println!("{}", serde_json::to_string_pretty(&json).unwrap());
return;
}

let matches = build_subcommands(cli_app, &root.commands).get_matches();
let chosen_cmd =
find_command(&matches, &root.commands).expect("SubcommandRequired failed to work");
Expand Down Expand Up @@ -71,17 +79,34 @@ fn find_maskfile() -> (Result<String, String>, String) {
(maskfile, maskfile_path.to_str().unwrap().to_string())
}

fn is_introspecting() -> bool {
let args: Vec<String> = env::args().collect();
for a in args {
if a == "--maskfile-introspect" {
return true;
}
}
false
}

/// Load a maskfile from another directory
fn custom_maskfile_path_arg<'a, 'b>() -> Arg<'a, 'b> {
// This is needed to prevent clap from complaining about the custom flag check
// within find_maskfile(). It should be removed once clap 3.x is released.
// See https://github.com/clap-rs/clap/issues/748
let custom_maskfile_path = Arg::with_name("maskfile")
Arg::with_name("maskfile")
.help("Path to a different maskfile you want to use")
.long("maskfile")
.takes_value(true)
.multiple(false);
.multiple(false)
}

custom_maskfile_path
/// Print out the maskfile structure in json
fn introspect_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("maskfile-introspect")
.help("Print out the maskfile command structure in json")
.long("maskfile-introspect")
.multiple(false)
}

fn build_subcommands<'a, 'b>(
Expand Down
59 changes: 59 additions & 0 deletions mask/tests/introspect_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
mod common;
use assert_cmd::prelude::*;
use predicates::str::contains;
use serde_json::json;

#[test]
fn outputs_the_maskfile_structure_as_json() {
let (_temp, maskfile_path) = common::maskfile(
r#"
# Document Title

## somecommand
> The command description

~~~bash
echo something
~~~
"#,
);

let verbose_flag = json!({
"name": "verbose",
"description": "Sets the level of verbosity",
"short": "v",
"long": "verbose",
"multiple": false,
"takes_value": false,
"required": false,
"validate_as_number": false,
});

let expected_json = json!({
"title": "Document Title",
"description": "",
"commands": [
{
"level": 2,
"name": "somecommand",
"description": "The command description",
"script": {
"executor": "bash",
"source": "echo something\n",
},
"subcommands": [],
"required_args": [],
"named_flags": [verbose_flag],
}
]
});

common::run_mask(&maskfile_path)
.arg("--maskfile-introspect")
.assert()
.code(0)
.stdout(contains(
serde_json::to_string_pretty(&expected_json).unwrap(),
))
.success();
}