aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/party.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/party.rs')
-rw-r--r--src/federation/party.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs
index e030048..8f5143b 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -133,7 +133,14 @@ fn command() -> Command {
Command::new("write")
.about("Writes a value (JSON) at a path")
.arg(id("path"))
- .arg(id("value")),
+ .arg(id("value"))
+ .arg(
+ Arg::new("raw")
+ .short('r')
+ .long("raw")
+ .action(ArgAction::SetTrue)
+ .help("The value a string as it is, not JSON"),
+ ),
)
.subcommand(
Command::new("keys")
@@ -914,10 +921,9 @@ pub(crate) async fn main() -> Result<()> {
match (name, sub) {
("write", sub) => {
let path_to = sub.get_one::<String>("path").expect("required");
- let value: JsonValue =
- serde_json::from_str(sub.get_one::<String>("value").expect("required"))
- .context("the value isn't JSON")?;
- let value = client::json(&value).map_err(|why| anyhow!(why))?;
+ let given = sub.get_one::<String>("value").expect("required");
+ let value =
+ client::json(&written(given, sub.get_flag("raw"))?).map_err(|why| anyhow!(why))?;
sign(
&node,
&signer,
@@ -1191,6 +1197,14 @@ fn keys_given(given: &[String]) -> Result<Vec<String>> {
}
/// `given`, where each is a node ID.
+/// The value `write` was given: JSON, or with `-r` (`raw`) a string as it is.
+fn written(given: &str, raw: bool) -> Result<JsonValue> {
+ if raw {
+ return Ok(given.into());
+ }
+ serde_json::from_str(given).context("the value isn't JSON; -r writes it as a string")
+}
+
fn node_ids(given: Vec<String>) -> Result<Vec<String>> {
match given.iter().find(|id| !keys::node_id(id)) {
Some(none) => bail!("{none:?} isn't a node ID"),
@@ -2547,6 +2561,26 @@ mod tests {
super::command().debug_assert();
}
+ /// `write` takes JSON, or with `-r` a string as it is, JSON or not.
+ #[test]
+ fn a_value_as_json_or_as_it_is() {
+ use serde_json::json;
+ assert_eq!(
+ super::written(r#"{"a": 1}"#, false).unwrap(),
+ json!({"a": 1})
+ );
+ assert_eq!(super::written(r#""hello""#, false).unwrap(), json!("hello"));
+ assert!(super::written("hello", false).is_err());
+ assert_eq!(
+ super::written("hello, world", true).unwrap(),
+ json!("hello, world")
+ );
+ assert_eq!(
+ super::written(r#"{"a": 1}"#, true).unwrap(),
+ json!(r#"{"a": 1}"#)
+ );
+ }
+
/// `propose` and `fork` take a file, or a subcommand saying what the version is for,
/// with their options after it.
#[test]