From 83d7b938c9b97ec0dfd77d4a24903e067891c724 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Thu, 28 Mar 2024 15:19:19 -0400 Subject: [PATCH] feat: rename to cache command --- Cargo.lock | 1 + cmd/crates/soroban-test/Cargo.toml | 1 + cmd/crates/soroban-test/src/lib.rs | 3 +- .../tests/it/integration/hello_world.rs | 9 +++- cmd/soroban-cli/src/commands/cache/clean.rs | 34 +++++++++++++++ cmd/soroban-cli/src/commands/cache/info.rs | 23 ++++++++++ .../src/commands/{data => cache}/ls.rs | 2 +- cmd/soroban-cli/src/commands/cache/mod.rs | 42 +++++++++++++++++++ cmd/soroban-cli/src/commands/cache/read.rs | 31 ++++++++++++++ cmd/soroban-cli/src/commands/config/data.rs | 9 +++- cmd/soroban-cli/src/commands/data/mod.rs | 23 ---------- cmd/soroban-cli/src/commands/mod.rs | 10 ++--- 12 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 cmd/soroban-cli/src/commands/cache/clean.rs create mode 100644 cmd/soroban-cli/src/commands/cache/info.rs rename cmd/soroban-cli/src/commands/{data => cache}/ls.rs (96%) create mode 100644 cmd/soroban-cli/src/commands/cache/mod.rs create mode 100644 cmd/soroban-cli/src/commands/cache/read.rs delete mode 100644 cmd/soroban-cli/src/commands/data/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 37def890f2..66d8232203 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3961,6 +3961,7 @@ dependencies = [ "thiserror", "tokio", "toml 0.8.10", + "ulid", "walkdir", "which", ] diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index 0886a92749..2a8ec17788 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -41,6 +41,7 @@ serde_json = "1.0.93" which = { workspace = true } tokio = "1.28.1" walkdir = "2.4.0" +ulid.workspace = true [features] it = [] diff --git a/cmd/crates/soroban-test/src/lib.rs b/cmd/crates/soroban-test/src/lib.rs index e4e24f568e..e4d7410ce4 100644 --- a/cmd/crates/soroban-test/src/lib.rs +++ b/cmd/crates/soroban-test/src/lib.rs @@ -126,7 +126,8 @@ impl TestEnv { .env("SOROBAN_ACCOUNT", TEST_ACCOUNT) .env("SOROBAN_RPC_URL", &self.rpc_url) .env("SOROBAN_NETWORK_PASSPHRASE", LOCAL_NETWORK_PASSPHRASE) - .env("XDG_CONFIG_HOME", self.temp_dir.as_os_str()) + .env("XDG_CONFIG_HOME", self.temp_dir.join("config").as_os_str()) + .env("XDG_DATA_HOME", self.temp_dir.join("data").as_os_str()) .current_dir(&self.temp_dir); cmd } diff --git a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs index 671d2edffe..77fb9644cd 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -10,9 +10,10 @@ use crate::integration::util::extend_contract; use super::util::{deploy_hello, extend, HELLO_WORLD}; +#[allow(clippy::too_many_lines)] #[tokio::test] async fn invoke() { - let sandbox = &TestEnv::new(); + let sandbox = &TestEnv::with_rpc_url("http://moss:8090/soroban/rpc"); let c = soroban_rpc::Client::new(&sandbox.rpc_url).unwrap(); let GetLatestLedgerResponse { sequence, .. } = c.get_latest_ledger().await.unwrap(); sandbox @@ -63,6 +64,12 @@ async fn invoke() { }; let id = &deploy_hello(sandbox).await; extend_contract(sandbox, id).await; + let uid = sandbox + .new_assert_cmd("cache") + .arg("ls") + .assert() + .stdout_as_str(); + ulid::Ulid::from_string(&uid).expect("invalid ulid"); // Note that all functions tested here have no state invoke_hello_world(sandbox, id); diff --git a/cmd/soroban-cli/src/commands/cache/clean.rs b/cmd/soroban-cli/src/commands/cache/clean.rs new file mode 100644 index 0000000000..ec908b151e --- /dev/null +++ b/cmd/soroban-cli/src/commands/cache/clean.rs @@ -0,0 +1,34 @@ +use std::fs; + +use super::super::config::locator; +use crate::commands::config::data; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Config(#[from] locator::Error), + #[error(transparent)] + Data(#[from] data::Error), + #[error(transparent)] + Io(#[from] std::io::Error), +} + +#[derive(Debug, clap::Parser, Clone)] +#[group(skip)] +pub struct Cmd { + /// Actions only + #[arg(long, short = 'a')] + pub actions: bool, +} + +impl Cmd { + pub fn run(&self) -> Result<(), Error> { + let dir = if self.actions { + data::actions_dir()? + } else { + data::project_dir()?.data_dir().to_path_buf() + }; + fs::remove_dir_all(dir)?; + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/cache/info.rs b/cmd/soroban-cli/src/commands/cache/info.rs new file mode 100644 index 0000000000..723ae890e0 --- /dev/null +++ b/cmd/soroban-cli/src/commands/cache/info.rs @@ -0,0 +1,23 @@ +use super::super::config::locator; +use crate::commands::config::data; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Config(#[from] locator::Error), + #[error(transparent)] + Data(#[from] data::Error), +} + +#[derive(Debug, clap::Parser, Clone)] +#[group(skip)] +pub struct Cmd {} + +impl Cmd { + pub fn run(&self) -> Result<(), Error> { + let binding = data::project_dir()?; + let dir = binding.data_dir(); + println!("{}", dir.to_string_lossy()); + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/data/ls.rs b/cmd/soroban-cli/src/commands/cache/ls.rs similarity index 96% rename from cmd/soroban-cli/src/commands/data/ls.rs rename to cmd/soroban-cli/src/commands/cache/ls.rs index a45311cb29..05d99bb7e6 100644 --- a/cmd/soroban-cli/src/commands/data/ls.rs +++ b/cmd/soroban-cli/src/commands/cache/ls.rs @@ -29,7 +29,7 @@ impl Cmd { } pub fn ls(&self) -> Result, Error> { - Ok(data::list_actions()? + Ok(data::list_ulids()? .iter() .map(ToString::to_string) .collect()) diff --git a/cmd/soroban-cli/src/commands/cache/mod.rs b/cmd/soroban-cli/src/commands/cache/mod.rs new file mode 100644 index 0000000000..db629db100 --- /dev/null +++ b/cmd/soroban-cli/src/commands/cache/mod.rs @@ -0,0 +1,42 @@ +use clap::Parser; + +pub mod clean; +pub mod info; +pub mod ls; +pub mod read; + +#[derive(Debug, Parser)] +pub enum Cmd { + /// List cached actions (transactions, simulations) + Ls(ls::Cmd), + /// Show location of cache + Info(info::Cmd), + /// Delete all cached actions + Clean(clean::Cmd), + /// Read cached action + Read(read::Cmd), +} + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Info(#[from] info::Error), + #[error(transparent)] + Ls(#[from] ls::Error), + #[error(transparent)] + Clean(#[from] clean::Error), + #[error(transparent)] + Read(#[from] read::Error), +} + +impl Cmd { + pub fn run(&self) -> Result<(), Error> { + match self { + Cmd::Ls(cmd) => cmd.run()?, + Cmd::Info(cmd) => cmd.run()?, + Cmd::Clean(cmd) => cmd.run()?, + Cmd::Read(cmd) => cmd.run()?, + }; + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/cache/read.rs b/cmd/soroban-cli/src/commands/cache/read.rs new file mode 100644 index 0000000000..73b959a99a --- /dev/null +++ b/cmd/soroban-cli/src/commands/cache/read.rs @@ -0,0 +1,31 @@ +use std::fs; + +use super::super::config::locator; +use crate::commands::config::data; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Config(#[from] locator::Error), + #[error(transparent)] + Data(#[from] data::Error), + #[error("failed to find cache entry {0}")] + NotFound(String), +} + +#[derive(Debug, clap::Parser, Clone)] +#[group(skip)] +pub struct Cmd { + /// ULID of the cache entry + #[arg(long, visible_alias = "id")] + ulid: String, +} + +impl Cmd { + pub fn run(&self) -> Result<(), Error> { + let dir = data::actions_dir()?; + fs::read_to_string(dir.join(&self.ulid).with_extension(".json")) + .map_err(|_| Error::NotFound(self.ulid.clone()))?; + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/config/data.rs b/cmd/soroban-cli/src/commands/config/data.rs index 2ca1c49d05..93bab46dd3 100644 --- a/cmd/soroban-cli/src/commands/config/data.rs +++ b/cmd/soroban-cli/src/commands/config/data.rs @@ -112,7 +112,14 @@ impl std::fmt::Display for DatedAction { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let (id, a, uri) = (&self.0, &self.1, &self.2); let datetime = to_datatime(id).format("%b %d %H:%M"); - write!(f, "{datetime} {uri} {}", a.type_str(),) + let status = match a { + Action::Simulation(sim) => sim + .error + .as_ref() + .map_or_else(|| "SUCCESS".to_string(), |_| "ERROR".to_string()), + Action::Transaction(txn) => txn.status.to_string(), + }; + write!(f, "{id} {} {status} {datetime} {uri} ", a.type_str(),) } } diff --git a/cmd/soroban-cli/src/commands/data/mod.rs b/cmd/soroban-cli/src/commands/data/mod.rs deleted file mode 100644 index f9d07d4c86..0000000000 --- a/cmd/soroban-cli/src/commands/data/mod.rs +++ /dev/null @@ -1,23 +0,0 @@ -use clap::Parser; -pub mod ls; - -#[derive(Debug, Parser)] -pub enum Cmd { - /// List identities - Ls(ls::Cmd), -} - -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error(transparent)] - Ls(#[from] ls::Error), -} - -impl Cmd { - pub fn run(&self) -> Result<(), Error> { - match self { - Cmd::Ls(cmd) => cmd.run()?, - }; - Ok(()) - } -} diff --git a/cmd/soroban-cli/src/commands/mod.rs b/cmd/soroban-cli/src/commands/mod.rs index 1f5b002b26..3da8e39719 100644 --- a/cmd/soroban-cli/src/commands/mod.rs +++ b/cmd/soroban-cli/src/commands/mod.rs @@ -3,10 +3,10 @@ use std::str::FromStr; use async_trait::async_trait; use clap::{command, error::ErrorKind, CommandFactory, FromArgMatches, Parser}; +pub mod cache; pub mod completion; pub mod config; pub mod contract; -pub mod data; pub mod events; pub mod global; pub mod keys; @@ -101,7 +101,7 @@ impl Root { Cmd::Version(version) => version.run(), Cmd::Keys(id) => id.run().await?, Cmd::Config(c) => c.run().await?, - Cmd::Data(data) => data.run()?, + Cmd::Cache(data) => data.run()?, }; Ok(()) } @@ -139,9 +139,9 @@ pub enum Cmd { Network(network::Cmd), /// Print version information Version(version::Cmd), - /// Access cached data + /// Cache for tranasctions and contract specs #[command(subcommand)] - Data(data::Cmd), + Cache(cache::Cmd), } #[derive(thiserror::Error, Debug)] @@ -164,7 +164,7 @@ pub enum Error { #[error(transparent)] Network(#[from] network::Error), #[error(transparent)] - Data(#[from] data::Error), + Cache(#[from] cache::Error), } #[async_trait]