From 637a8603775d02ef014ec84491a2bbbb5c7c4f54 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 20 Nov 2026 00:52:58 +0000 Subject: continuum-party write -r: a string as it is A value not JSON is taken as a string with -r, as `get -r` prints one; without it, the error says so. --- src/federation/party.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'src/federation') 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::("path").expect("required"); - let value: JsonValue = - serde_json::from_str(sub.get_one::("value").expect("required")) - .context("the value isn't JSON")?; - let value = client::json(&value).map_err(|why| anyhow!(why))?; + let given = sub.get_one::("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> { } /// `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 { + 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) -> Result> { 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] -- cgit v1.3.1