Skip to content

Commit

Permalink
feat: rename to cache command
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Apr 2, 2024
1 parent 30d505d commit 1478548
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 32 deletions.
1 change: 1 addition & 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 cmd/crates/soroban-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
3 changes: 2 additions & 1 deletion cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
34 changes: 34 additions & 0 deletions cmd/soroban-cli/src/commands/cache/clean.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
23 changes: 23 additions & 0 deletions cmd/soroban-cli/src/commands/cache/info.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Cmd {
}

pub fn ls(&self) -> Result<Vec<String>, Error> {
Ok(data::list_actions()?
Ok(data::list_ulids()?
.iter()
.map(ToString::to_string)
.collect())
Expand Down
42 changes: 42 additions & 0 deletions cmd/soroban-cli/src/commands/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
31 changes: 31 additions & 0 deletions cmd/soroban-cli/src/commands/cache/read.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
9 changes: 8 additions & 1 deletion cmd/soroban-cli/src/commands/config/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),)
}
}

Expand Down
23 changes: 0 additions & 23 deletions cmd/soroban-cli/src/commands/data/mod.rs

This file was deleted.

10 changes: 5 additions & 5 deletions cmd/soroban-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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)]
Expand All @@ -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]
Expand Down

0 comments on commit 1478548

Please sign in to comment.