Skip to content

Commit

Permalink
Improve display for cli client new-address command
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Sep 10, 2023
1 parent 41228a7 commit e2a37f0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
62 changes: 50 additions & 12 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ use sui_types::{
transaction::Transaction,
};

use tabled::settings::Style as TableStyle;
use tabled::builder::Builder as TableBuilder;
use tabled::settings::{
object::Cell as TableCell, Border as TableBorder, Modify as TableModify, Panel as TablePanel,
Style as TableStyle,
};
use tracing::info;

macro_rules! serialize_or_execute {
Expand Down Expand Up @@ -1051,7 +1055,12 @@ impl SuiClientCommands {
derivation_path,
word_length,
)?;
SuiClientCommandResult::NewAddress((address, phrase, scheme))

SuiClientCommandResult::NewAddress(NewAddressOutput {
address,
key_scheme: scheme,
recovery_phrase: phrase,
})
}
SuiClientCommands::Gas { address } => {
let address = address.unwrap_or(context.active_address()?);
Expand Down Expand Up @@ -1385,6 +1394,36 @@ impl Display for SuiClientCommandResult {
table.with(style);
write!(f, "{}", table)?
}
SuiClientCommandResult::NewAddress(new_address) => {
let mut builder = TableBuilder::default();

builder.push_record(vec!["address", new_address.address.to_string().as_str()]);
builder.push_record(vec![
"keyScheme",
new_address.key_scheme.to_string().as_str(),
]);
builder.push_record(vec![
"recoveryPhrase",
new_address.recovery_phrase.to_string().as_str(),
]);

let mut table = builder.build();
table.with(TableStyle::rounded());
table.with(TablePanel::header(
"Created new keypair and saved it to keystore.",
));

table.with(
TableModify::new(TableCell::new(0, 0))
.with(TableBorder::default().corner_bottom_right('┬')),
);
table.with(
TableModify::new(TableCell::new(0, 0))
.with(TableBorder::default().corner_top_right('─')),
);

write!(f, "{}", table)?
}
SuiClientCommandResult::Upgrade(response)
| SuiClientCommandResult::Publish(response) => {
write!(writer, "{}", write_transaction_response(response)?)?;
Expand Down Expand Up @@ -1486,15 +1525,6 @@ impl Display for SuiClientCommandResult {
SuiClientCommandResult::SyncClientState => {
writeln!(writer, "Client state sync complete.")?;
}
// Do not use writer for new address output, which may get sent to logs.
#[allow(clippy::print_in_format_impl)]
SuiClientCommandResult::NewAddress((address, recovery_phrase, scheme)) => {
println!(
"Created new keypair for address with scheme {:?}: [{address}]",
scheme
);
println!("Secret Recovery Phrase : [{recovery_phrase}]");
}
SuiClientCommandResult::Gas(gases) => {
// TODO: generalize formatting of CLI
writeln!(writer, " {0: ^66} | {1: ^11}", "Object ID", "Gas Value")?;
Expand Down Expand Up @@ -1734,6 +1764,14 @@ pub struct DynamicFieldOutput {
pub data: Vec<DynamicFieldInfo>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewAddressOutput {
pub address: SuiAddress,
pub key_scheme: SignatureScheme,
pub recovery_phrase: String,
}

#[derive(Serialize)]
#[serde(untagged)]
pub enum SuiClientCommandResult {
Expand All @@ -1747,7 +1785,7 @@ pub enum SuiClientCommandResult {
ExecuteSignedTx(SuiTransactionBlockResponse),
Gas(Vec<GasCoin>),
MergeCoin(SuiTransactionBlockResponse),
NewAddress((SuiAddress, String, SignatureScheme)),
NewAddress(NewAddressOutput),
NewEnv(SuiEnv),
Object(SuiObjectResponse),
Objects(Vec<SuiObjectResponse>),
Expand Down
9 changes: 5 additions & 4 deletions crates/sui/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ async fn test_regression_6546() -> Result<(), anyhow::Error> {
let SuiClientCommandResult::Objects(coins) = SuiClientCommands::Objects {
address: Some(address),
}
.execute(context)
.await? else{
.execute(context)
.await?
else {
panic!()
};
let config_path = test_cluster.swarm.dir().join(SUI_CLIENT_CONFIG);
Expand Down Expand Up @@ -1302,8 +1303,8 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {
}
.execute(context)
.await?;
let new_addr = if let SuiClientCommandResult::NewAddress((a, _, _)) = os {
a
let new_addr = if let SuiClientCommandResult::NewAddress(x) = os {
x.address
} else {
panic!("Command failed")
};
Expand Down

0 comments on commit e2a37f0

Please sign in to comment.