From fe92a6dd324e3ecdb4a29bebfdde18b416064316 Mon Sep 17 00:00:00 2001 From: igalshilman Date: Thu, 10 Oct 2024 17:36:40 +0200 Subject: [PATCH] [restatectl] Use clap_stdin --- Cargo.lock | 10 ++++++++++ tools/restatectl/Cargo.toml | 1 + tools/restatectl/src/commands/metadata/put.rs | 20 +++++-------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d67080b6f..f3e5bac3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1346,6 +1346,15 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-stdin" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "471df7896633bfc1e7d3da5b598422891e4cb8931210168ec63ea586e285803f" +dependencies = [ + "thiserror", +] + [[package]] name = "clap-verbosity-flag" version = "2.2.0" @@ -6426,6 +6435,7 @@ dependencies = [ "bytestring", "chrono", "clap", + "clap-stdin", "clap-verbosity-flag", "cling", "crossterm 0.27.0", diff --git a/tools/restatectl/Cargo.toml b/tools/restatectl/Cargo.toml index 53fce761a..2bd4f12b6 100644 --- a/tools/restatectl/Cargo.toml +++ b/tools/restatectl/Cargo.toml @@ -25,6 +25,7 @@ bytestring = { workspace = true } chrono = { workspace = true } clap = { version = "4.1", features = ["derive", "env", "wrap_help", "color"] } clap-verbosity-flag = { version = "2.0.1" } +clap-stdin = "0.5.1" cling = { version = "0.1.0", default-features = false, features = ["derive"] } crossterm = { version = "0.27.0" } ctrlc = { version = "3.4" } diff --git a/tools/restatectl/src/commands/metadata/put.rs b/tools/restatectl/src/commands/metadata/put.rs index eaeb43a4c..fb7617158 100644 --- a/tools/restatectl/src/commands/metadata/put.rs +++ b/tools/restatectl/src/commands/metadata/put.rs @@ -10,11 +10,12 @@ use crate::commands::metadata::patch::{patch_value, PatchValueOpts}; use crate::commands::metadata::MetadataCommonOpts; +use anyhow::anyhow; use clap::Parser; +use clap_stdin::FileOrStdin; use cling::{Collect, Run}; use serde_json::Value; use std::collections::HashMap; -use std::fs; #[derive(Run, Parser, Collect, Clone, Debug)] #[clap()] @@ -27,13 +28,9 @@ pub struct PutValueOpts { #[arg(short, long)] key: String, - /// The JSON document to store + /// The JSON document to store, can be read from stdin or a file path #[arg(short, long)] - doc: Option, - - /// The local path to the JSON document to store - #[arg(short, long)] - path: Option, + content: FileOrStdin, /// Expected version for conditional update #[arg(short = 'e', long)] @@ -47,14 +44,7 @@ pub struct PutValueOpts { async fn put_value(opts: &PutValueOpts) -> anyhow::Result<()> { let opts = opts.clone(); - let doc_body = if let Some(doc) = opts.doc { - doc - } else if let Some(path) = opts.path { - fs::read_to_string(path) - .map_err(|e| anyhow::anyhow!("Unable to read the document: {}", e))? - } else { - anyhow::bail!("Please specify either doc or path"); - }; + let doc_body = opts.content.contents().map_err(|e| anyhow!(e))?; let mut doc: Value = serde_json::from_str(&doc_body) .map_err(|e| anyhow::anyhow!("Parsing JSON value: {}", e))?;