Skip to content

Commit

Permalink
[restatectl] Use clap_stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
igalshilman committed Oct 10, 2024
1 parent 6445040 commit fe92a6d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/restatectl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
20 changes: 5 additions & 15 deletions tools/restatectl/src/commands/metadata/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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<String>,

/// The local path to the JSON document to store
#[arg(short, long)]
path: Option<String>,
content: FileOrStdin,

/// Expected version for conditional update
#[arg(short = 'e', long)]
Expand All @@ -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))?;
Expand Down

0 comments on commit fe92a6d

Please sign in to comment.