From de80a128d938f2d246573a797416829bb78a37fb Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:31:45 -0300 Subject: [PATCH] Print secret message to tty If stdout is not a tty (i.e it is a pipe to another process or it writes to another file) a normal message will be printed. --- Cargo.lock | 1 + crates/keygen/Cargo.toml | 1 + crates/keygen/src/keygen.rs | 21 ++++++++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3aabda0f7ae..6acb989edbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2944,6 +2944,7 @@ name = "fuel-core-keygen" version = "0.20.4" dependencies = [ "anyhow", + "atty", "clap 4.4.6", "fuel-core-types", "libp2p-identity 0.2.5", diff --git a/crates/keygen/Cargo.toml b/crates/keygen/Cargo.toml index d81b7d21055..53569f0123e 100644 --- a/crates/keygen/Cargo.toml +++ b/crates/keygen/Cargo.toml @@ -12,6 +12,7 @@ description = "Create to create command line utilities for fuel-core key managem [dependencies] anyhow = { workspace = true } +atty = "0.2.14" clap = { workspace = true, features = ["derive", "env"] } fuel-core-types = { workspace = true, features = ["serde", "random"] } libp2p-identity = { version = "0.2.4", features = ["secp256k1", "peerid"] } diff --git a/crates/keygen/src/keygen.rs b/crates/keygen/src/keygen.rs index 89536a93562..ba817f7040a 100644 --- a/crates/keygen/src/keygen.rs +++ b/crates/keygen/src/keygen.rs @@ -2,6 +2,7 @@ use crate::{ BLOCK_PRODUCTION, P2P, }; +use atty::Stream; use clap::ValueEnum; use fuel_core_types::{ fuel_crypto::{ @@ -21,12 +22,15 @@ use libp2p_identity::{ use serde_json::json; use std::{ io::{ + stdin, + stdout, Read, Write, }, ops::Deref, str::FromStr, }; +use termion::screen::IntoAlternateScreen; /// Generate a random new secret & public key in the format expected by fuel-core #[derive(Debug, clap::Args)] @@ -132,19 +136,22 @@ impl ParseSecret { fn wait_for_keypress() { let mut single_key = [0u8]; - std::io::stdin().read_exact(&mut single_key).unwrap(); + stdin().read_exact(&mut single_key).unwrap(); } fn display_string_discreetly( discreet_string: &str, continue_message: &str, ) -> anyhow::Result<()> { - use termion::screen::IntoAlternateScreen; - let mut screen = std::io::stdout().into_alternate_screen()?; - writeln!(screen, "{discreet_string}")?; - screen.flush()?; - println!("{continue_message}"); - wait_for_keypress(); + if atty::is(Stream::Stdout) { + let mut screen = stdout().into_alternate_screen()?; + writeln!(screen, "{discreet_string}")?; + screen.flush()?; + println!("{continue_message}"); + wait_for_keypress(); + } else { + println!("{discreet_string}"); + } Ok(()) }