Skip to content

Commit 7fcd42b

Browse files
authored
svix-cli: allow reading JSON bodies from stdin (#2113)
This allows `svix-cli` to read any single JSON argument from stdin if it's passed as a single dash
1 parent f52c7f4 commit 7fcd42b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

svix-cli/src/json.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::str::FromStr;
1+
use std::{io::Read, str::FromStr};
22

3-
use anyhow::{Error, Result};
3+
use anyhow::{Context, Error, Result};
44
use colored_json::{Color, ColorMode, ToColoredJson};
55
use serde::{de::DeserializeOwned, Serialize};
66

@@ -11,7 +11,16 @@ impl<T: DeserializeOwned> FromStr for JsonOf<T> {
1111
type Err = Error;
1212

1313
fn from_str(s: &str) -> Result<Self, Self::Err> {
14-
Ok(JsonOf(serde_json::from_str(s)?))
14+
if s == "-" {
15+
let mut stdin = std::io::stdin().lock();
16+
let mut input = String::new();
17+
stdin
18+
.read_to_string(&mut input)
19+
.context("Error reading stdin for '-' argument")?;
20+
Ok(JsonOf(serde_json::from_str(&input)?))
21+
} else {
22+
Ok(JsonOf(serde_json::from_str(s)?))
23+
}
1524
}
1625
}
1726

0 commit comments

Comments
 (0)