debuapi: Optionally read from stdin for Body
use crate::error::ImlManagerCliError;
use console::Term;
use structopt::{clap::arg_enum, StructOpt};
+use tokio::io::{stdin, AsyncReadExt};
arg_enum! {
#[derive(PartialEq, Debug)]
@@ -25,7 +26,7 @@ pub struct ApiCommand {
path: String,
- /// JSON formatted body to send
+ /// JSON formatted body to send or "-" to read from stdin
#[structopt(required_ifs(&[("method", "patch"),("method", "post"),("method", "put")]))]
body: Option<String>,
}
@@ -44,8 +45,14 @@ pub async fn api_cli(command: ApiCommand) -> Result<(), ImlManagerCliError> {
ApiMethod::Put => client.put(uri),
};
- let body: Option<serde_json::Value> =
- command.body.map(|s| serde_json::from_str(&s)).transpose()?;
+ let body: Option<serde_json::Value> = if command.body == Some("-".to_string()) {
+ let mut buf: Vec<u8> = Vec::new();
+ stdin().read_to_end(&mut buf).await?;
+ let s = String::from_utf8_lossy(&buf);
+ Some(serde_json::from_str(&s)?)
+ } else {
+ command.body.map(|s| serde_json::from_str(&s)).transpose()?
+ };
let req = if let Some(data) = body {
req.json(&data)
