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 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
28 changes: 28 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"progenitor-client",
"progenitor-impl",
"progenitor-macro",
"test-progenitor-compilation",
]

resolver = "2"
Expand Down
10 changes: 10 additions & 0 deletions progenitor-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ typify = { git = "https://github.com/oxidecomputer/typify" }
unicode-ident = "1.0.12"

[dev-dependencies]
base64 = "0.21"
clap = { version = "4.4", features = ["derive", "string", "env"] }
chrono = { version = "0.4", features = ["serde"] }
dropshot = { git = "https://github.com/oxidecomputer/dropshot", default-features = false }
expectorate = "1.1"
futures = "0.3.27"
http = "0.2.9"
hyper = "0.14.27"
progenitor-client = { path = "../progenitor-client" }
rand = "0.8"
regress = "0.7.1"
reqwest = { version = "0.11.22", features = ["json", "stream"] }
rustfmt-wrapper = "0.2.1"
serde_yaml = "0.9"
serde_json = "1.0.108"
test-progenitor-compilation = { path = "../test-progenitor-compilation" }
uuid = { version = "1.6", features = ["serde", "v4"] }
17 changes: 8 additions & 9 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,14 +477,13 @@ 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();
let body_value =
serde_json::from_str::<#body_type_ident>(
&body_txt,
)
.unwrap();
let body_value: #body_type_ident = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};

request = request.body(body_value);
}
}
Expand Down
90 changes: 60 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,11 @@ 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") {
let body_value: types::TaskSubmit = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down Expand Up @@ -551,9 +571,11 @@ 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") {
let body_value: types::UserCreate = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down Expand Up @@ -611,9 +633,11 @@ 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") {
let body_value: types::WorkerBootstrap = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down Expand Up @@ -665,9 +689,11 @@ 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") {
let body_value: types::WorkerAppendTask = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down Expand Up @@ -715,9 +741,11 @@ 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") {
let body_value: types::WorkerCompleteTask = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down Expand Up @@ -749,9 +777,11 @@ 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") {
let body_value: types::WorkerAddOutput = match value.as_str() {
"-" => serde_json::from_reader(std::io::stdin()).unwrap(),
file => serde_json::from_reader(std::fs::File::open(&file).unwrap()).unwrap(),
};
request = request.body(body_value);
}

Expand Down
Loading
Loading