diff options
| author | tv <tv@krebsco.de> | 2026-11-20 00:52:58 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 02:15:02 +0100 |
| commit | 637a8603775d02ef014ec84491a2bbbb5c7c4f54 (patch) | |
| tree | a3bf577b0ba8a4c48eb2c7fa9f7390b886aa896f /src | |
| parent | 10786d2e598865cd6e763df08f692cfc0f756908 (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/party.rs | 44 |
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] |
