Skip to content

Commit

Permalink
Move CLI cli_output module to its own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Sep 23, 2020
1 parent ba353c2 commit 325a7e9
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 85 deletions.
26 changes: 23 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"banks-server",
"clap-utils",
"cli-config",
"cli-output",
"client",
"core",
"dos",
Expand Down
27 changes: 27 additions & 0 deletions cli-output/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
authors = ["Solana Maintainers <[email protected]>"]
edition = "2018"
name = "solana-cli-output"
description = "Blockchain, Rebuilt for Scale"
version = "1.4.0"
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"

[dependencies]
chrono = { version = "0.4.11", features = ["serde"] }
console = "0.11.3"
humantime = "2.0.1"
Inflector = "0.11.4"
indicatif = "0.15.0"
serde = "1.0.112"
serde_derive = "1.0.103"
serde_json = "1.0.56"
solana-client = { path = "../client", version = "1.4.0" }
solana-sdk = { path = "../sdk", version = "1.4.0" }
solana-stake-program = { path = "../programs/stake", version = "1.4.0" }
solana-transaction-status = { path = "../transaction-status", version = "1.4.0" }
solana-vote-program = { path = "../programs/vote", version = "1.4.0" }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
7 changes: 2 additions & 5 deletions cli/src/cli_output.rs → cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{
cli::build_balance_message,
display::{format_labeled_address, writeln_name_value},
};
use crate::display::{build_balance_message, format_labeled_address, writeln_name_value};
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
use console::{style, Emoji};
use inflector::cases::titlecase::to_title_case;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use solana_client::rpc_response::{
RpcAccountBalance, RpcKeyedAccount, RpcSupply, RpcVoteAccountInfo,
Expand Down
34 changes: 18 additions & 16 deletions cli/src/display.rs → cli-output/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::cli::SettingType;
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use solana_sdk::{
Expand All @@ -8,6 +7,24 @@ use solana_sdk::{
use solana_transaction_status::UiTransactionStatusMeta;
use std::{collections::HashMap, fmt, io};

pub fn build_balance_message(lamports: u64, use_lamports_unit: bool, show_unit: bool) -> String {
if use_lamports_unit {
let ess = if lamports == 1 { "" } else { "s" };
let unit = if show_unit {
format!(" lamport{}", ess)
} else {
"".to_string()
};
format!("{:?}{}", lamports, unit)
} else {
let sol = lamports_to_sol(lamports);
let sol_str = format!("{:.9}", sol);
let pretty_sol = sol_str.trim_end_matches('0').trim_end_matches('.');
let unit = if show_unit { " SOL" } else { "" };
format!("{}{}", pretty_sol, unit)
}
}

// Pretty print a "name value"
pub fn println_name_value(name: &str, value: &str) {
let styled_value = if value == "" {
Expand Down Expand Up @@ -40,21 +57,6 @@ pub fn format_labeled_address(pubkey: &str, address_labels: &HashMap<String, Str
}
}

pub fn println_name_value_or(name: &str, value: &str, setting_type: SettingType) {
let description = match setting_type {
SettingType::Explicit => "",
SettingType::Computed => "(computed)",
SettingType::SystemDefault => "(default)",
};

println!(
"{} {} {}",
style(name).bold(),
style(value),
style(description).italic(),
);
}

pub fn println_signers(
blockhash: &Hash,
signers: &[String],
Expand Down
3 changes: 3 additions & 0 deletions cli-output/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod cli_output;
pub mod display;
pub use cli_output::*;
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde_json = "1.0.56"
solana-account-decoder = { path = "../account-decoder", version = "1.4.0" }
solana-clap-utils = { path = "../clap-utils", version = "1.4.0" }
solana-cli-config = { path = "../cli-config", version = "1.4.0" }
solana-cli-output = { path = "../cli-output", version = "1.4.0" }
solana-client = { path = "../client", version = "1.4.0" }
solana-config-program = { path = "../programs/config", version = "1.4.0" }
solana-faucet = { path = "../faucet", version = "1.4.0" }
Expand Down
41 changes: 8 additions & 33 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use crate::{
checks::*,
cli_output::{CliAccount, CliSignature, OutputFormat},
cluster_query::*,
display::{new_spinner_progress_bar, println_name_value, println_transaction},
nonce::*,
offline::return_signers,
spend_utils::*,
stake::*,
validator_info::*,
vote::*,
checks::*, cluster_query::*, nonce::*, offline::return_signers, spend_utils::*, stake::*,
validator_info::*, vote::*,
};
use clap::{value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
use log::*;
Expand All @@ -25,6 +17,12 @@ use solana_clap_utils::{
nonce::*,
offline::*,
};
use solana_cli_output::{
display::{
build_balance_message, new_spinner_progress_bar, println_name_value, println_transaction,
},
CliAccount, CliSignature, OutputFormat,
};
use solana_client::{
blockhash_query::BlockhashQuery,
client_error::{ClientError, ClientErrorKind, Result as ClientResult},
Expand All @@ -47,7 +45,6 @@ use solana_sdk::{
instruction::InstructionError,
loader_instruction,
message::Message,
native_token::lamports_to_sol,
pubkey::{Pubkey, MAX_SEED_LEN},
signature::{Keypair, Signature, Signer, SignerError},
signers::Signers,
Expand Down Expand Up @@ -1940,28 +1937,6 @@ where
}
}

pub(crate) fn build_balance_message(
lamports: u64,
use_lamports_unit: bool,
show_unit: bool,
) -> String {
if use_lamports_unit {
let ess = if lamports == 1 { "" } else { "s" };
let unit = if show_unit {
format!(" lamport{}", ess)
} else {
"".to_string()
};
format!("{:?}{}", lamports, unit)
} else {
let sol = lamports_to_sol(lamports);
let sol_str = format!("{:.9}", sol);
let pretty_sol = sol_str.trim_end_matches('0').trim_end_matches('.');
let unit = if show_unit { " SOL" } else { "" };
format!("{}{}", pretty_sol, unit)
}
}

pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, 'v> {
App::new(name)
.about(about)
Expand Down
10 changes: 6 additions & 4 deletions cli/src/cluster_query.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::{
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
cli_output::*,
display::{
format_labeled_address, new_spinner_progress_bar, println_name_value, println_transaction,
},
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
};
use clap::{value_t, value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
use console::{style, Emoji};
use solana_clap_utils::{
commitment::commitment_arg, input_parsers::*, input_validators::*, keypair::DefaultSigner,
};
use solana_cli_output::{
display::{
format_labeled_address, new_spinner_progress_bar, println_name_value, println_transaction,
},
*,
};
use solana_client::{
pubsub_client::PubsubClient,
rpc_client::{GetConfirmedSignaturesForAddress2Config, RpcClient},
Expand Down
3 changes: 0 additions & 3 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ macro_rules! pubkey {
};
}

#[macro_use]
extern crate serde_derive;

pub mod checks;
pub mod cli;
pub mod cli_output;
pub mod cluster_query;
pub mod display;
pub mod nonce;
pub mod offline;
pub mod spend_utils;
Expand Down
25 changes: 19 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ use solana_clap_utils::{
keypair::{CliSigners, DefaultSigner, SKIP_SEED_PHRASE_VALIDATION_ARG},
DisplayError,
};
use solana_cli::{
cli::{
app, parse_command, process_command, CliCommandInfo, CliConfig, DEFAULT_RPC_TIMEOUT_SECONDS,
},
cli_output::OutputFormat,
display::{println_name_value, println_name_value_or},
use solana_cli::cli::{
app, parse_command, process_command, CliCommandInfo, CliConfig, SettingType,
DEFAULT_RPC_TIMEOUT_SECONDS,
};
use solana_cli_config::{Config, CONFIG_FILE};
use solana_cli_output::{display::println_name_value, OutputFormat};
use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use std::{collections::HashMap, error, path::PathBuf, sync::Arc, time::Duration};

pub fn println_name_value_or(name: &str, value: &str, setting_type: SettingType) {
let description = match setting_type {
SettingType::Explicit => "",
SettingType::Computed => "(computed)",
SettingType::SystemDefault => "(default)",
};

println!(
"{} {} {}",
style(name).bold(),
style(value),
style(description).italic(),
);
}

fn parse_settings(matches: &ArgMatches<'_>) -> Result<bool, Box<dyn error::Error>> {
let parse_args = match matches.subcommand() {
("config", Some(matches)) => {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
ProcessResult,
},
cli_output::CliNonceAccount,
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
};
use clap::{App, Arg, ArgMatches, SubCommand};
Expand All @@ -14,6 +13,7 @@ use solana_clap_utils::{
keypair::{DefaultSigner, SignerIndex},
nonce::*,
};
use solana_cli_output::CliNonceAccount;
use solana_client::{nonce_utils::*, rpc_client::RpcClient};
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::{
Expand Down
2 changes: 1 addition & 1 deletion cli/src/offline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cli_output::{CliSignOnlyData, OutputFormat};
use serde_json::Value;
use solana_clap_utils::keypair::presigner_from_pubkey_sigs;
use solana_cli_output::{CliSignOnlyData, OutputFormat};
use solana_sdk::{
hash::Hash,
pubkey::Pubkey,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
ProcessResult,
},
cli_output::{CliStakeHistory, CliStakeHistoryEntry, CliStakeState, CliStakeType},
nonce::check_nonce_account,
offline::return_signers,
spend_utils::{resolve_spend_tx_and_check_account_balances, SpendAmount},
Expand All @@ -19,6 +18,7 @@ use solana_clap_utils::{
offline::*,
ArgConstant,
};
use solana_cli_output::{CliStakeHistory, CliStakeHistoryEntry, CliStakeState, CliStakeType};
use solana_client::{
blockhash_query::BlockhashQuery, nonce_utils, rpc_client::RpcClient,
rpc_request::DELINQUENT_VALIDATOR_SLOT_DISTANCE,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/validator_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
cli_output::{CliValidatorInfo, CliValidatorInfoVec},
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
};
use bincode::deserialize;
Expand All @@ -15,6 +14,7 @@ use solana_clap_utils::{
input_validators::{is_pubkey, is_url},
keypair::DefaultSigner,
};
use solana_cli_output::{CliValidatorInfo, CliValidatorInfoVec};
use solana_client::rpc_client::RpcClient;
use solana_config_program::{config_instruction, get_config_data, ConfigKeys, ConfigState};
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
ProcessResult,
},
cli_output::{CliEpochVotingHistory, CliLockout, CliVoteAccount},
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
};
use clap::{value_t_or_exit, App, Arg, ArgMatches, SubCommand};
Expand All @@ -14,6 +13,7 @@ use solana_clap_utils::{
input_validators::*,
keypair::{DefaultSigner, SignerIndex},
};
use solana_cli_output::{CliEpochVotingHistory, CliLockout, CliVoteAccount};
use solana_client::rpc_client::RpcClient;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::{
Expand Down
Loading

0 comments on commit 325a7e9

Please sign in to comment.