From df546770b3bf9f106fca458cbedae5c4132e465b Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Wed, 7 Aug 2024 14:04:00 -0300 Subject: [PATCH] Rename output module to print. (#1530) --- .../ts-tests/initialize.sh | 2 +- .../src/commands/contract/deploy/wasm.rs | 20 +++--- .../src/commands/contract/install.rs | 10 +-- cmd/soroban-cli/src/lib.rs | 2 +- cmd/soroban-cli/src/output.rs | 63 ----------------- cmd/soroban-cli/src/print.rs | 70 +++++++++++++++++++ 6 files changed, 87 insertions(+), 80 deletions(-) delete mode 100644 cmd/soroban-cli/src/output.rs create mode 100644 cmd/soroban-cli/src/print.rs diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh index 9424bebac..d85eff115 100755 --- a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh +++ b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh @@ -30,7 +30,7 @@ function fund_all() { exe eval "./soroban keys fund root" } function deploy() { - exe eval "(./soroban contract deploy --source root --wasm $1 --ignore-checks) > $2" + exe eval "(./soroban contract deploy --quiet --source root --wasm $1 --ignore-checks) > $2" } function deploy_all() { deploy ../../../../target/wasm32-unknown-unknown/test-wasms/test_custom_types.wasm contract-id-custom-types.txt diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index ec73a2322..b047f7dc9 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -28,7 +28,7 @@ use crate::{ txn_result::{TxnEnvelopeResult, TxnResult}, NetworkRunnable, }, - output::Output, + print::Print, }; #[derive(Parser, Debug, Clone)] @@ -165,7 +165,7 @@ impl NetworkRunnable for Cmd { global_args: Option<&global::Args>, config: Option<&config::Args>, ) -> Result, Error> { - let output = Output::new(global_args.map_or(false, |a| a.quiet)); + let print = Print::new(global_args.map_or(false, |a| a.quiet)); let config = config.unwrap_or(&self.config); let wasm_hash = if let Some(wasm) = &self.wasm { let hash = if self.fee.build_only || self.fee.sim_only { @@ -197,7 +197,7 @@ impl NetworkRunnable for Cmd { } })?); - output.info(format!("Using wasm hash {wasm_hash}").as_str()); + print.info(format!("Using wasm hash {wasm_hash}").as_str()); let network = config.get_network()?; let salt: [u8; 32] = match &self.salt { @@ -230,22 +230,22 @@ impl NetworkRunnable for Cmd { )?; if self.fee.build_only { - output.check("Transaction built!"); + print.check("Transaction built!"); return Ok(TxnResult::Txn(txn)); } - output.info("Simulating deploy transaction…"); + print.info("Simulating deploy transaction…"); let txn = client.simulate_and_assemble_transaction(&txn).await?; let txn = self.fee.apply_to_assembled_txn(txn).transaction().clone(); if self.fee.sim_only { - output.check("Done!"); + print.check("Done!"); return Ok(TxnResult::Txn(txn)); } - output.globe("Submitting deploy transaction…"); - output.log_transaction(&txn, &network, true)?; + print.globe("Submitting deploy transaction…"); + print.log_transaction(&txn, &network, true)?; let get_txn_resp = client .send_transaction_polling(&config.sign_with_local_key(txn).await?) @@ -259,10 +259,10 @@ impl NetworkRunnable for Cmd { let contract_id = stellar_strkey::Contract(contract_id.0).to_string(); if let Some(url) = utils::explorer_url_for_contract(&network, &contract_id) { - output.link(url); + print.link(url); } - output.check("Deployed!"); + print.check("Deployed!"); Ok(TxnResult::Res(contract_id)) } diff --git a/cmd/soroban-cli/src/commands/contract/install.rs b/cmd/soroban-cli/src/commands/contract/install.rs index 23e4e0913..8474c9621 100644 --- a/cmd/soroban-cli/src/commands/contract/install.rs +++ b/cmd/soroban-cli/src/commands/contract/install.rs @@ -15,7 +15,7 @@ use crate::commands::txn_result::{TxnEnvelopeResult, TxnResult}; use crate::commands::{global, NetworkRunnable}; use crate::config::{self, data, network}; use crate::key; -use crate::output::Output; +use crate::print::Print; use crate::rpc::{self, Client}; use crate::{utils, wasm}; @@ -96,7 +96,7 @@ impl NetworkRunnable for Cmd { args: Option<&global::Args>, config: Option<&config::Args>, ) -> Result, Error> { - let output = Output::new(args.map_or(false, |a| a.quiet)); + let print = Print::new(args.map_or(false, |a| a.quiet)); let config = config.unwrap_or(&self.config); let contract = self.wasm.read()?; let network = config.get_network()?; @@ -163,7 +163,7 @@ impl NetworkRunnable for Cmd { // Skip reupload if this isn't V0 because V1 extension already // exists. if code.ext.ne(&ContractCodeEntryExt::V0) { - output.info("Skipping install because wasm already installed"); + print.info("Skipping install because wasm already installed"); return Ok(TxnResult::Res(hash)); } } @@ -175,7 +175,7 @@ impl NetworkRunnable for Cmd { } } - output.info("Simulating install transaction…"); + print.info("Simulating install transaction…"); let txn = client .simulate_and_assemble_transaction(&tx_without_preflight) @@ -186,7 +186,7 @@ impl NetworkRunnable for Cmd { return Ok(TxnResult::Txn(txn)); } - output.globe("Submitting install transaction…"); + print.globe("Submitting install transaction…"); let txn_resp = client .send_transaction_polling(&self.config.sign_with_local_key(txn).await?) diff --git a/cmd/soroban-cli/src/lib.rs b/cmd/soroban-cli/src/lib.rs index afb0607ca..84e1e6eb8 100644 --- a/cmd/soroban-cli/src/lib.rs +++ b/cmd/soroban-cli/src/lib.rs @@ -17,7 +17,7 @@ pub mod fee; pub mod get_spec; pub mod key; pub mod log; -pub mod output; +pub mod print; pub mod signer; pub mod toid; pub mod utils; diff --git a/cmd/soroban-cli/src/output.rs b/cmd/soroban-cli/src/output.rs deleted file mode 100644 index a86b56d65..000000000 --- a/cmd/soroban-cli/src/output.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::fmt::Display; - -use soroban_env_host::xdr::{Error as XdrError, Transaction}; - -use crate::{ - config::network::Network, - utils::{explorer_url_for_transaction, transaction_hash}, -}; - -pub struct Output { - pub quiet: bool, -} - -impl Output { - pub fn new(quiet: bool) -> Output { - Output { quiet } - } - - fn print(&self, icon: &str, message: T) { - if !self.quiet { - eprintln!("{icon} {message}"); - } - } - - pub fn check(&self, message: T) { - self.print("✅", message); - } - - pub fn info(&self, message: T) { - self.print("ℹ️", message); - } - - pub fn globe(&self, message: T) { - self.print("🌎", message); - } - - pub fn link(&self, message: T) { - self.print("🔗", message); - } - - /// # Errors - /// - /// Might return an error - pub fn log_transaction( - &self, - tx: &Transaction, - network: &Network, - show_link: bool, - ) -> Result<(), XdrError> { - let tx_hash = transaction_hash(tx, &network.network_passphrase)?; - let hash = hex::encode(tx_hash); - - self.info(format!("Transaction hash is {hash}").as_str()); - - if show_link { - if let Some(url) = explorer_url_for_transaction(network, &hash) { - self.link(url); - } - } - - Ok(()) - } -} diff --git a/cmd/soroban-cli/src/print.rs b/cmd/soroban-cli/src/print.rs new file mode 100644 index 000000000..2a163af87 --- /dev/null +++ b/cmd/soroban-cli/src/print.rs @@ -0,0 +1,70 @@ +use std::fmt::Display; + +use soroban_env_host::xdr::{Error as XdrError, Transaction}; + +use crate::{ + config::network::Network, + utils::{explorer_url_for_transaction, transaction_hash}, +}; + +pub struct Print { + pub quiet: bool, +} + +impl Print { + pub fn new(quiet: bool) -> Print { + Print { quiet } + } + + /// # Errors + /// + /// Might return an error + pub fn log_transaction( + &self, + tx: &Transaction, + network: &Network, + show_link: bool, + ) -> Result<(), XdrError> { + let tx_hash = transaction_hash(tx, &network.network_passphrase)?; + let hash = hex::encode(tx_hash); + + self.infoln(format!("Transaction hash is {hash}").as_str()); + + if show_link { + if let Some(url) = explorer_url_for_transaction(network, &hash) { + self.linkln(url); + } + } + + Ok(()) + } +} + +macro_rules! create_print_functions { + ($name:ident, $nameln:ident, $icon:expr) => { + impl Print { + #[allow(dead_code)] + pub fn $name(&self, message: T) { + if !self.quiet { + print!("{} {}", $icon, message); + } + } + + #[allow(dead_code)] + pub fn $nameln(&self, message: T) { + if !self.quiet { + println!("{} {}", $icon, message); + } + } + } + }; +} + +create_print_functions!(bucket, bucketln, "🪣"); +create_print_functions!(check, checkln, "✅"); +create_print_functions!(error, errorln, "❌"); +create_print_functions!(globe, globeln, "🌎"); +create_print_functions!(info, infoln, "ℹ️"); +create_print_functions!(link, linkln, "🔗"); +create_print_functions!(save, saveln, "💾"); +create_print_functions!(search, searchln, "🔎");