From 9b4aa230941338902fe93c2e881fece3ba384678 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Tue, 12 Mar 2024 15:54:02 -0400 Subject: [PATCH] feat: implement display for Dated Action to see formated list of actions --- cmd/soroban-cli/src/commands/config/data.rs | 33 +++++++++++++++++++-- cmd/soroban-cli/src/commands/data/ls.rs | 32 +++++--------------- cmd/soroban-cli/src/commands/data/mod.rs | 22 ++++++++++++++ cmd/soroban-cli/src/commands/mod.rs | 6 ++++ 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/cmd/soroban-cli/src/commands/config/data.rs b/cmd/soroban-cli/src/commands/config/data.rs index b8d86eee2f..2ca1c49d05 100644 --- a/cmd/soroban-cli/src/commands/config/data.rs +++ b/cmd/soroban-cli/src/commands/config/data.rs @@ -91,20 +91,37 @@ pub fn list_ulids() -> Result, Error> { list.sort(); Ok(list .iter() - .map(|s| ulid::Ulid::from_str(s)) + .map(|s| ulid::Ulid::from_str(s.trim_end_matches(".json"))) .collect::, _>>()?) } -pub fn list_actions() -> Result, Error> { +pub fn list_actions() -> Result, Error> { list_ulids()? .into_iter() + .rev() .map(|id| { let (action, uri) = read(&id)?; - Ok((id, action, uri)) + Ok(DatedAction(id, action, uri)) }) .collect::, Error>>() } +pub struct DatedAction(ulid::Ulid, Action, Uri); + +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(),) + } +} + +impl DatedAction {} + +fn to_datatime(id: &ulid::Ulid) -> chrono::DateTime { + chrono::DateTime::from_timestamp_millis(id.timestamp_ms().try_into().unwrap()).unwrap() +} + #[derive(Serialize, Deserialize)] struct Data { action: Action, @@ -117,6 +134,16 @@ pub enum Action { Transaction(GetTransactionResponseRaw), } +impl Action { + pub fn type_str(&self) -> String { + match self { + Action::Simulation(_) => "Sim", + Action::Transaction(_) => "Txn", + } + .to_string() + } +} + impl From for Action { fn from(res: SimulateTransactionResponse) -> Self { Self::Simulation(res) diff --git a/cmd/soroban-cli/src/commands/data/ls.rs b/cmd/soroban-cli/src/commands/data/ls.rs index da2a3960a5..c798c557b7 100644 --- a/cmd/soroban-cli/src/commands/data/ls.rs +++ b/cmd/soroban-cli/src/commands/data/ls.rs @@ -1,7 +1,6 @@ use clap::command; -use crate::commands::config::data::{self, Action}; - +use crate::commands::config::data; use super::super::config::locator; #[derive(thiserror::Error, Debug)] @@ -30,31 +29,16 @@ impl Cmd { } pub fn ls(&self) -> Result, Error> { - data::list_actions()? + Ok(data::list_actions()? .iter() - .map(|(id, action, uri)| { - Ok(format!( - "{} {} {uri}\n", - to_datatime(id), - action_type(action) - )) - }) - .collect() + .map(ToString::to_string) + .collect()) } pub fn ls_l(&self) -> Result, Error> { - todo!() - } -} - -fn to_datatime(id: &ulid::Ulid) -> chrono::DateTime { - chrono::DateTime::from_timestamp_millis(id.timestamp_ms().try_into().unwrap()).unwrap() -} - -fn action_type(a: &Action) -> String { - match a { - Action::Simulation(_) => "Simulation", - Action::Transaction(_) => "Transaction", + Ok(data::list_actions()? + .iter() + .map(ToString::to_string) + .collect()) } - .to_string() } diff --git a/cmd/soroban-cli/src/commands/data/mod.rs b/cmd/soroban-cli/src/commands/data/mod.rs index 1ea63c501c..f9d07d4c86 100644 --- a/cmd/soroban-cli/src/commands/data/mod.rs +++ b/cmd/soroban-cli/src/commands/data/mod.rs @@ -1 +1,23 @@ +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 4b14b45bfe..94e9ae62ab 100644 --- a/cmd/soroban-cli/src/commands/mod.rs +++ b/cmd/soroban-cli/src/commands/mod.rs @@ -101,6 +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()?, }; Ok(()) } @@ -138,6 +139,9 @@ pub enum Cmd { Network(network::Cmd), /// Print version information Version(version::Cmd), + /// Access cached data + #[command(subcommand)] + Data(data::Cmd), } #[derive(thiserror::Error, Debug)] @@ -159,6 +163,8 @@ pub enum Error { Plugin(#[from] plugin::Error), #[error(transparent)] Network(#[from] network::Error), + #[error(transparent)] + Data(#[from] data::Error), } #[async_trait]