Skip to content

Commit

Permalink
feat(CLI): Add two new output types for contract inspect (stellar#829)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and kalepail committed Aug 10, 2023
1 parent 8efc4e1 commit af20217
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
28 changes: 25 additions & 3 deletions cmd/soroban-cli/src/commands/contract/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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)]
#[group(skip)]
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,
}
Expand All @@ -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(())
}
}
10 changes: 10 additions & 0 deletions cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ impl From<Durability> 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,
}
12 changes: 11 additions & 1 deletion cmd/soroban-cli/src/utils/contract_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -97,6 +97,16 @@ impl ContractSpec {
spec,
})
}

pub fn spec_as_json_array(&self) -> Result<String, Error> {
let spec = self
.spec
.iter()
.map(|e| Ok(format!("\"{}\"", e.to_xdr_base64()?)))
.collect::<Result<Vec<_>, Error>>()?
.join(",\n");
Ok(format!("[{spec}]"))
}
}

impl Display for ContractSpec {
Expand Down
12 changes: 12 additions & 0 deletions docs/soroban-cli-full-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ Inspect a WASM file listing contract functions, meta, etc
###### **Options:**

* `--wasm <WASM>` — Path to wasm binary
* `--output <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 <CONFIG_DIR>`

Expand Down

0 comments on commit af20217

Please sign in to comment.