From af202174c92af0389d979c1724c55262af6bc827 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Fri, 4 Aug 2023 15:11:09 -0400 Subject: [PATCH] feat(CLI): Add two new output types for `contract inspect` (#829) * feat(CLI): add output type to contract inspect Now it can also output base64 xdr of the spec and an array of entries each encoded as base64 xdr. * fix: move array to spec definition --------- Co-authored-by: Tsachi Herman <24438559+tsachiherman@users.noreply.github.com> --- .../src/commands/contract/inspect.rs | 28 +++++++++++++++++-- cmd/soroban-cli/src/commands/contract/mod.rs | 10 +++++++ cmd/soroban-cli/src/utils/contract_spec.rs | 12 +++++++- docs/soroban-cli-full-docs.md | 12 ++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/inspect.rs b/cmd/soroban-cli/src/commands/contract/inspect.rs index 1e978ea59..355c18ca8 100644 --- a/cmd/soroban-cli/src/commands/contract/inspect.rs +++ b/cmd/soroban-cli/src/commands/contract/inspect.rs @@ -1,6 +1,9 @@ use clap::{command, Parser}; -use std::fmt::Debug; +use soroban_env_host::xdr; +use std::{fmt::Debug, path::PathBuf}; +use tracing::debug; +use super::SpecOutput; use crate::{commands::config::locator, wasm}; #[derive(Parser, Debug, Clone)] @@ -8,6 +11,10 @@ use crate::{commands::config::locator, wasm}; pub struct Cmd { #[command(flatten)] wasm: wasm::Args, + /// Output just XDR in base64 + #[arg(long, default_value = "docs")] + output: SpecOutput, + #[clap(flatten)] locator: locator::Args, } @@ -16,12 +23,27 @@ pub struct Cmd { pub enum Error { #[error(transparent)] Wasm(#[from] wasm::Error), + #[error("missing spec for {0:?}")] + MissingSpec(PathBuf), + #[error(transparent)] + Xdr(#[from] xdr::Error), + #[error(transparent)] + Spec(#[from] crate::utils::contract_spec::Error), } impl Cmd { pub fn run(&self) -> Result<(), Error> { - println!("File: {}", self.wasm.wasm.to_string_lossy()); - println!("{}", self.wasm.parse()?); + let wasm = self.wasm.parse()?; + debug!("File: {}", self.wasm.wasm.to_string_lossy()); + let output = match self.output { + SpecOutput::XdrBase64 => wasm + .spec_base64 + .clone() + .ok_or_else(|| Error::MissingSpec(self.wasm.wasm.clone()))?, + SpecOutput::XdrBase64Array => wasm.spec_as_json_array()?, + SpecOutput::Docs => wasm.to_string(), + }; + println!("{output}"); Ok(()) } } diff --git a/cmd/soroban-cli/src/commands/contract/mod.rs b/cmd/soroban-cli/src/commands/contract/mod.rs index 26ad09510..7da171cd7 100644 --- a/cmd/soroban-cli/src/commands/contract/mod.rs +++ b/cmd/soroban-cli/src/commands/contract/mod.rs @@ -128,3 +128,13 @@ impl From for soroban_env_host::xdr::ContractDataDurability { } } } + +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)] +pub enum SpecOutput { + /// XDR of array of contract spec entries + XdrBase64, + /// Array of xdr of contract spec entries + XdrBase64Array, + /// Pretty print of contract spec entries + Docs, +} diff --git a/cmd/soroban-cli/src/utils/contract_spec.rs b/cmd/soroban-cli/src/utils/contract_spec.rs index b19197df0..55d9f1055 100644 --- a/cmd/soroban-cli/src/utils/contract_spec.rs +++ b/cmd/soroban-cli/src/utils/contract_spec.rs @@ -6,7 +6,7 @@ use std::{ use soroban_env_host::xdr::{ self, DepthLimitedRead, ReadXdr, ScEnvMetaEntry, ScMetaEntry, ScMetaV0, ScSpecEntry, ScSpecFunctionV0, ScSpecUdtEnumV0, ScSpecUdtErrorEnumV0, ScSpecUdtStructV0, ScSpecUdtUnionV0, - StringM, + StringM, WriteXdr, }; pub struct ContractSpec { @@ -97,6 +97,16 @@ impl ContractSpec { spec, }) } + + pub fn spec_as_json_array(&self) -> Result { + let spec = self + .spec + .iter() + .map(|e| Ok(format!("\"{}\"", e.to_xdr_base64()?))) + .collect::, Error>>()? + .join(",\n"); + Ok(format!("[{spec}]")) + } } impl Display for ContractSpec { diff --git a/docs/soroban-cli-full-docs.md b/docs/soroban-cli-full-docs.md index 406b47f7e..4cd51f64a 100644 --- a/docs/soroban-cli-full-docs.md +++ b/docs/soroban-cli-full-docs.md @@ -291,6 +291,18 @@ Inspect a WASM file listing contract functions, meta, etc ###### **Options:** * `--wasm ` — Path to wasm binary +* `--output ` — Output just XDR in base64 + + Default value: `docs` + + Possible values: + - `xdr-base64`: + XDR of array of contract spec entries + - `xdr-base64-array`: + Array of xdr of contract spec entries + - `docs`: + Pretty print of contract spec entries + * `--global` — Use global config * `--config-dir `