From 91025bfcde77f1b6fe5493909b967db00fb961ba Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:48:27 -0300 Subject: [PATCH] Print secret message to tty, normal message otherwise Port 586ff3e547feddb782dde3d8dde7e45842df92ba from https://github.com/FuelLabs/fuel-core/pull/1426 Context: https://github.com/FuelLabs/fuel-core/pull/1426#discussion_r1364696151 --- Cargo.lock | 1 + forc-plugins/forc-crypto/Cargo.toml | 1 + forc-plugins/forc-crypto/src/keygen/mod.rs | 21 +++++++++++++-------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85d24b15522..5b53859e9d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1893,6 +1893,7 @@ version = "0.46.0" dependencies = [ "anyhow", "async-trait", + "atty", "clap 3.2.25", "forc-tracing", "fuel-core-types", diff --git a/forc-plugins/forc-crypto/Cargo.toml b/forc-plugins/forc-crypto/Cargo.toml index 47c8d2dbff5..3f45cbbb178 100644 --- a/forc-plugins/forc-crypto/Cargo.toml +++ b/forc-plugins/forc-crypto/Cargo.toml @@ -12,6 +12,7 @@ repository.workspace = true [dependencies] anyhow = "1.0.75" async-trait = "0.1.58" +atty = "0.2.14" clap = { version = "3", features = ["derive", "env"] } forc-tracing = { version = "0.46.0", path = "../../forc-tracing" } fuel-core-types = { version = "0.20.5" } diff --git a/forc-plugins/forc-crypto/src/keygen/mod.rs b/forc-plugins/forc-crypto/src/keygen/mod.rs index c234f51b690..3bdf1f7215a 100644 --- a/forc-plugins/forc-crypto/src/keygen/mod.rs +++ b/forc-plugins/forc-crypto/src/keygen/mod.rs @@ -1,5 +1,7 @@ +use atty::Stream; use clap::ValueEnum; -use std::io::{Read, Write}; +use std::io::{stdin, stdout, Read, Write}; +use termion::screen::IntoAlternateScreen; pub mod new_key; pub mod parse_secret; @@ -16,18 +18,21 @@ pub enum KeyType { 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(); } pub(crate) 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 { + print!("{discreet_string}"); + } Ok(()) }