Skip to content

Commit f52a9a0

Browse files
committed
Accept --json-body - to read body from stdin.
1 parent 7c28b25 commit f52a9a0

6 files changed

Lines changed: 944 additions & 218 deletions

File tree

progenitor-impl/src/cli.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl Generator {
473473
CliBodyArg::Optional => Some(false),
474474
})
475475
.map(|required| {
476-
let help = "Path to a file that contains the full json body.";
476+
let help = "Path to a file that contains the full json body, or '-' to read from stdin.";
477477

478478
quote! {
479479
.arg(
@@ -511,7 +511,13 @@ impl Generator {
511511
if let Some(value) =
512512
matches.get_one::<std::path::PathBuf>("json-body")
513513
{
514-
let body_txt = std::fs::read_to_string(value).with_context(|| format!("failed to read {}", value.display()))?;
514+
let body_txt = if value.as_os_str() == "-" {
515+
let mut buf = String::new();
516+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf).context("failed to read from stdin")?;
517+
buf
518+
} else {
519+
std::fs::read_to_string(value).with_context(|| format!("failed to read {}", value.display()))?
520+
};
515521
let body_value =
516522
serde_json::from_str::<#body_type_ident>(
517523
&body_txt,

progenitor-impl/tests/output/src/buildomat_cli.rs

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ impl<T: CliConfig> Cli<T> {
8383
.value_name("JSON-FILE")
8484
.required(false)
8585
.value_parser(::clap::value_parser!(std::path::PathBuf))
86-
.help("Path to a file that contains the full json body."),
86+
.help(
87+
"Path to a file that contains the full json body, or '-' to read from \
88+
stdin.",
89+
),
8790
)
8891
.arg(
8992
::clap::Arg::new("json-body-template")
@@ -148,7 +151,10 @@ impl<T: CliConfig> Cli<T> {
148151
.value_name("JSON-FILE")
149152
.required(false)
150153
.value_parser(::clap::value_parser!(std::path::PathBuf))
151-
.help("Path to a file that contains the full json body."),
154+
.help(
155+
"Path to a file that contains the full json body, or '-' to read from \
156+
stdin.",
157+
),
152158
)
153159
.arg(
154160
::clap::Arg::new("json-body-template")
@@ -186,7 +192,10 @@ impl<T: CliConfig> Cli<T> {
186192
.value_name("JSON-FILE")
187193
.required(false)
188194
.value_parser(::clap::value_parser!(std::path::PathBuf))
189-
.help("Path to a file that contains the full json body."),
195+
.help(
196+
"Path to a file that contains the full json body, or '-' to read from \
197+
stdin.",
198+
),
190199
)
191200
.arg(
192201
::clap::Arg::new("json-body-template")
@@ -234,7 +243,10 @@ impl<T: CliConfig> Cli<T> {
234243
.value_name("JSON-FILE")
235244
.required(false)
236245
.value_parser(::clap::value_parser!(std::path::PathBuf))
237-
.help("Path to a file that contains the full json body."),
246+
.help(
247+
"Path to a file that contains the full json body, or '-' to read from \
248+
stdin.",
249+
),
238250
)
239251
.arg(
240252
::clap::Arg::new("json-body-template")
@@ -273,7 +285,10 @@ impl<T: CliConfig> Cli<T> {
273285
.value_name("JSON-FILE")
274286
.required(false)
275287
.value_parser(::clap::value_parser!(std::path::PathBuf))
276-
.help("Path to a file that contains the full json body."),
288+
.help(
289+
"Path to a file that contains the full json body, or '-' to read from \
290+
stdin.",
291+
),
277292
)
278293
.arg(
279294
::clap::Arg::new("json-body-template")
@@ -309,7 +324,10 @@ impl<T: CliConfig> Cli<T> {
309324
.value_name("JSON-FILE")
310325
.required(true)
311326
.value_parser(::clap::value_parser!(std::path::PathBuf))
312-
.help("Path to a file that contains the full json body."),
327+
.help(
328+
"Path to a file that contains the full json body, or '-' to read from \
329+
stdin.",
330+
),
313331
)
314332
.arg(
315333
::clap::Arg::new("json-body-template")
@@ -466,8 +484,15 @@ impl<T: CliConfig> Cli<T> {
466484
}
467485

468486
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
469-
let body_txt = std::fs::read_to_string(value)
470-
.with_context(|| format!("failed to read {}", value.display()))?;
487+
let body_txt = if value.as_os_str() == "-" {
488+
let mut buf = String::new();
489+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
490+
.context("failed to read from stdin")?;
491+
buf
492+
} else {
493+
std::fs::read_to_string(value)
494+
.with_context(|| format!("failed to read {}", value.display()))?
495+
};
471496
let body_value = serde_json::from_str::<types::TaskSubmit>(&body_txt)
472497
.with_context(|| format!("failed to parse {}", value.display()))?;
473498
request = request.body(body_value);
@@ -572,8 +597,15 @@ impl<T: CliConfig> Cli<T> {
572597
}
573598

574599
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
575-
let body_txt = std::fs::read_to_string(value)
576-
.with_context(|| format!("failed to read {}", value.display()))?;
600+
let body_txt = if value.as_os_str() == "-" {
601+
let mut buf = String::new();
602+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
603+
.context("failed to read from stdin")?;
604+
buf
605+
} else {
606+
std::fs::read_to_string(value)
607+
.with_context(|| format!("failed to read {}", value.display()))?
608+
};
577609
let body_value = serde_json::from_str::<types::UserCreate>(&body_txt)
578610
.with_context(|| format!("failed to parse {}", value.display()))?;
579611
request = request.body(body_value);
@@ -642,8 +674,15 @@ impl<T: CliConfig> Cli<T> {
642674
}
643675

644676
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
645-
let body_txt = std::fs::read_to_string(value)
646-
.with_context(|| format!("failed to read {}", value.display()))?;
677+
let body_txt = if value.as_os_str() == "-" {
678+
let mut buf = String::new();
679+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
680+
.context("failed to read from stdin")?;
681+
buf
682+
} else {
683+
std::fs::read_to_string(value)
684+
.with_context(|| format!("failed to read {}", value.display()))?
685+
};
647686
let body_value = serde_json::from_str::<types::WorkerBootstrap>(&body_txt)
648687
.with_context(|| format!("failed to parse {}", value.display()))?;
649688
request = request.body(body_value);
@@ -702,8 +741,15 @@ impl<T: CliConfig> Cli<T> {
702741
}
703742

704743
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
705-
let body_txt = std::fs::read_to_string(value)
706-
.with_context(|| format!("failed to read {}", value.display()))?;
744+
let body_txt = if value.as_os_str() == "-" {
745+
let mut buf = String::new();
746+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
747+
.context("failed to read from stdin")?;
748+
buf
749+
} else {
750+
std::fs::read_to_string(value)
751+
.with_context(|| format!("failed to read {}", value.display()))?
752+
};
707753
let body_value = serde_json::from_str::<types::WorkerAppendTask>(&body_txt)
708754
.with_context(|| format!("failed to parse {}", value.display()))?;
709755
request = request.body(body_value);
@@ -762,8 +808,15 @@ impl<T: CliConfig> Cli<T> {
762808
}
763809

764810
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
765-
let body_txt = std::fs::read_to_string(value)
766-
.with_context(|| format!("failed to read {}", value.display()))?;
811+
let body_txt = if value.as_os_str() == "-" {
812+
let mut buf = String::new();
813+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
814+
.context("failed to read from stdin")?;
815+
buf
816+
} else {
817+
std::fs::read_to_string(value)
818+
.with_context(|| format!("failed to read {}", value.display()))?
819+
};
767820
let body_value = serde_json::from_str::<types::WorkerCompleteTask>(&body_txt)
768821
.with_context(|| format!("failed to parse {}", value.display()))?;
769822
request = request.body(body_value);
@@ -802,8 +855,15 @@ impl<T: CliConfig> Cli<T> {
802855
}
803856

804857
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
805-
let body_txt = std::fs::read_to_string(value)
806-
.with_context(|| format!("failed to read {}", value.display()))?;
858+
let body_txt = if value.as_os_str() == "-" {
859+
let mut buf = String::new();
860+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
861+
.context("failed to read from stdin")?;
862+
buf
863+
} else {
864+
std::fs::read_to_string(value)
865+
.with_context(|| format!("failed to read {}", value.display()))?
866+
};
807867
let body_value = serde_json::from_str::<types::WorkerAddOutput>(&body_txt)
808868
.with_context(|| format!("failed to parse {}", value.display()))?;
809869
request = request.body(body_value);

progenitor-impl/tests/output/src/cli_gen_cli.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ impl<T: CliConfig> Cli<T> {
3030
.value_name("JSON-FILE")
3131
.required(true)
3232
.value_parser(::clap::value_parser!(std::path::PathBuf))
33-
.help("Path to a file that contains the full json body."),
33+
.help(
34+
"Path to a file that contains the full json body, or '-' to read from \
35+
stdin.",
36+
),
3437
)
3538
.arg(
3639
::clap::Arg::new("json-body-template")
@@ -57,8 +60,15 @@ impl<T: CliConfig> Cli<T> {
5760
}
5861

5962
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
60-
let body_txt = std::fs::read_to_string(value)
61-
.with_context(|| format!("failed to read {}", value.display()))?;
63+
let body_txt = if value.as_os_str() == "-" {
64+
let mut buf = String::new();
65+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
66+
.context("failed to read from stdin")?;
67+
buf
68+
} else {
69+
std::fs::read_to_string(value)
70+
.with_context(|| format!("failed to read {}", value.display()))?
71+
};
6272
let body_value = serde_json::from_str::<types::UnoBody>(&body_txt)
6373
.with_context(|| format!("failed to parse {}", value.display()))?;
6474
request = request.body(body_value);

progenitor-impl/tests/output/src/keeper_cli.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ impl<T: CliConfig> Cli<T> {
4848
.value_name("JSON-FILE")
4949
.required(false)
5050
.value_parser(::clap::value_parser!(std::path::PathBuf))
51-
.help("Path to a file that contains the full json body."),
51+
.help(
52+
"Path to a file that contains the full json body, or '-' to read from \
53+
stdin.",
54+
),
5255
)
5356
.arg(
5457
::clap::Arg::new("json-body-template")
@@ -113,7 +116,10 @@ impl<T: CliConfig> Cli<T> {
113116
.value_name("JSON-FILE")
114117
.required(true)
115118
.value_parser(::clap::value_parser!(std::path::PathBuf))
116-
.help("Path to a file that contains the full json body."),
119+
.help(
120+
"Path to a file that contains the full json body, or '-' to read from \
121+
stdin.",
122+
),
117123
)
118124
.arg(
119125
::clap::Arg::new("json-body-template")
@@ -138,7 +144,10 @@ impl<T: CliConfig> Cli<T> {
138144
.value_name("JSON-FILE")
139145
.required(true)
140146
.value_parser(::clap::value_parser!(std::path::PathBuf))
141-
.help("Path to a file that contains the full json body."),
147+
.help(
148+
"Path to a file that contains the full json body, or '-' to read from \
149+
stdin.",
150+
),
142151
)
143152
.arg(
144153
::clap::Arg::new("json-body-template")
@@ -177,7 +186,10 @@ impl<T: CliConfig> Cli<T> {
177186
.value_name("JSON-FILE")
178187
.required(true)
179188
.value_parser(::clap::value_parser!(std::path::PathBuf))
180-
.help("Path to a file that contains the full json body."),
189+
.help(
190+
"Path to a file that contains the full json body, or '-' to read from \
191+
stdin.",
192+
),
181193
)
182194
.arg(
183195
::clap::Arg::new("json-body-template")
@@ -217,8 +229,15 @@ impl<T: CliConfig> Cli<T> {
217229
}
218230

219231
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
220-
let body_txt = std::fs::read_to_string(value)
221-
.with_context(|| format!("failed to read {}", value.display()))?;
232+
let body_txt = if value.as_os_str() == "-" {
233+
let mut buf = String::new();
234+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
235+
.context("failed to read from stdin")?;
236+
buf
237+
} else {
238+
std::fs::read_to_string(value)
239+
.with_context(|| format!("failed to read {}", value.display()))?
240+
};
222241
let body_value = serde_json::from_str::<types::EnrolBody>(&body_txt)
223242
.with_context(|| format!("failed to parse {}", value.display()))?;
224243
request = request.body(body_value);
@@ -299,8 +318,15 @@ impl<T: CliConfig> Cli<T> {
299318
}
300319

301320
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
302-
let body_txt = std::fs::read_to_string(value)
303-
.with_context(|| format!("failed to read {}", value.display()))?;
321+
let body_txt = if value.as_os_str() == "-" {
322+
let mut buf = String::new();
323+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
324+
.context("failed to read from stdin")?;
325+
buf
326+
} else {
327+
std::fs::read_to_string(value)
328+
.with_context(|| format!("failed to read {}", value.display()))?
329+
};
304330
let body_value = serde_json::from_str::<types::ReportFinishBody>(&body_txt)
305331
.with_context(|| format!("failed to parse {}", value.display()))?;
306332
request = request.body(body_value);
@@ -327,8 +353,15 @@ impl<T: CliConfig> Cli<T> {
327353
}
328354

329355
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
330-
let body_txt = std::fs::read_to_string(value)
331-
.with_context(|| format!("failed to read {}", value.display()))?;
356+
let body_txt = if value.as_os_str() == "-" {
357+
let mut buf = String::new();
358+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
359+
.context("failed to read from stdin")?;
360+
buf
361+
} else {
362+
std::fs::read_to_string(value)
363+
.with_context(|| format!("failed to read {}", value.display()))?
364+
};
332365
let body_value = serde_json::from_str::<types::ReportOutputBody>(&body_txt)
333366
.with_context(|| format!("failed to parse {}", value.display()))?;
334367
request = request.body(body_value);
@@ -365,8 +398,15 @@ impl<T: CliConfig> Cli<T> {
365398
}
366399

367400
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
368-
let body_txt = std::fs::read_to_string(value)
369-
.with_context(|| format!("failed to read {}", value.display()))?;
401+
let body_txt = if value.as_os_str() == "-" {
402+
let mut buf = String::new();
403+
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
404+
.context("failed to read from stdin")?;
405+
buf
406+
} else {
407+
std::fs::read_to_string(value)
408+
.with_context(|| format!("failed to read {}", value.display()))?
409+
};
370410
let body_value = serde_json::from_str::<types::ReportStartBody>(&body_txt)
371411
.with_context(|| format!("failed to parse {}", value.display()))?;
372412
request = request.body(body_value);

0 commit comments

Comments
 (0)