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 CLI alias for standard in #606

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions progenitor-impl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl Generator {
CliBodyArg::Optional => Some(false),
})
.map(|required| {
let help = "Path to a file that contains the full json body.";
let help = r#"Path to a file that contains the full json body (use "-" to read from standard input)."#;

quote! {
.arg(
Expand All @@ -450,7 +450,7 @@ impl Generator {
// Required if we can't turn the body into individual
// parameters.
.required(#required)
.value_parser(clap::value_parser!(std::path::PathBuf))
.value_parser(clap::value_parser!(String))
.help(#help)
)
.arg(
Expand All @@ -477,12 +477,21 @@ impl Generator {
let body_type_ident = body_type.ident();
quote! {
if let Some(value) =
matches.get_one::<std::path::PathBuf>("json-body")
matches.get_one::<String>("json-body")
{
let body_txt = std::fs::read_to_string(value).unwrap();
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap()
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, updated it to use from_reader in both branches. Before merging though I would like to see if I can get a test in here. I don't think anything currently tests if a generated CLI would actually compile.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great


let body_value =
serde_json::from_str::<#body_type_ident>(
&body_txt,
serde_json::from_slice::<#body_type_ident>(
&body_input,
)
.unwrap();
request = request.body(body_value);
Expand Down
128 changes: 98 additions & 30 deletions progenitor-impl/tests/output/buildomat-cli.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -137,8 +140,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -175,8 +181,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -221,8 +230,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -260,8 +272,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -296,8 +311,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(true)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -457,9 +475,17 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.script(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::TaskSubmit>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::TaskSubmit>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -551,9 +577,17 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.name(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::UserCreate>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::UserCreate>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -611,9 +645,17 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.token(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::WorkerBootstrap>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::WorkerBootstrap>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -665,9 +707,18 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.time(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::WorkerAppendTask>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value =
serde_json::from_slice::<types::WorkerAppendTask>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -715,9 +766,18 @@ impl<T: CliOverride> Cli<T> {
request = request.task(value.clone());
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::WorkerCompleteTask>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value =
serde_json::from_slice::<types::WorkerCompleteTask>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -749,9 +809,17 @@ impl<T: CliOverride> Cli<T> {
request = request.task(value.clone());
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::WorkerAddOutput>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::WorkerAddOutput>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down
86 changes: 66 additions & 20 deletions progenitor-impl/tests/output/keeper-cli.out
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(false)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -108,8 +111,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(true)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand All @@ -133,8 +139,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(true)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -170,8 +179,11 @@ impl Cli {
.long("json-body")
.value_name("JSON-FILE")
.required(true)
.value_parser(clap::value_parser!(std::path::PathBuf))
.help("Path to a file that contains the full json body."),
.value_parser(clap::value_parser!(String))
.help(
"Path to a file that contains the full json body (use \"-\" to read from \
standard input).",
),
)
.arg(
clap::Arg::new("json-body-template")
Expand Down Expand Up @@ -224,9 +236,17 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.key(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::EnrolBody>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::EnrolBody>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -298,9 +318,18 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.exit_status(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::ReportFinishBody>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value =
serde_json::from_slice::<types::ReportFinishBody>(&body_input).unwrap();
request = request.body(body_value);
}

Expand All @@ -324,9 +353,18 @@ impl<T: CliOverride> Cli<T> {
request = request.authorization(value.clone());
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::ReportOutputBody>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value =
serde_json::from_slice::<types::ReportOutputBody>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down Expand Up @@ -359,9 +397,17 @@ impl<T: CliOverride> Cli<T> {
request = request.body_map(|body| body.start_time(value.clone()))
}

if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
let body_txt = std::fs::read_to_string(value).unwrap();
let body_value = serde_json::from_str::<types::ReportStartBody>(&body_txt).unwrap();
if let Some(value) = matches.get_one::<String>("json-body") {
use std::io::Read;
let body_input = match value.as_str() {
"-" => {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).unwrap();
buf
}
file => std::fs::read(&file).unwrap(),
};
let body_value = serde_json::from_slice::<types::ReportStartBody>(&body_input).unwrap();
request = request.body(body_value);
}

Expand Down
Loading
Loading