Skip to content

Commit c8a4d11

Browse files
committed
test: add option to take secret from stdin
1 parent 22d1415 commit c8a4d11

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/arguments/add.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use std::io::{self, BufRead};
2+
13
use clap::{Args, value_parser};
2-
use color_eyre::eyre::{ErrReport, Result};
4+
use color_eyre::eyre::{self, ErrReport, Result};
35

46
use zeroize::Zeroize;
57

@@ -60,6 +62,10 @@ pub struct AddArgs {
6062
required_if_eq("otp_type", "MOTP")
6163
)]
6264
pub pin: Option<String>,
65+
66+
/// Pass the secret through the standard input
67+
#[arg(long = "secret-stdin", default_value_t = false)]
68+
take_secret_from_stdin: bool,
6369
}
6470

6571
impl SubcommandExecutor for AddArgs {
@@ -79,7 +85,15 @@ impl SubcommandExecutor for AddArgs {
7985
}
8086

8187
fn get_from_args(matches: AddArgs) -> color_eyre::Result<OTPElement> {
82-
let secret = rpassword::prompt_password("Insert the secret: ").map_err(ErrReport::from)?;
88+
let secret = if matches.take_secret_from_stdin {
89+
if let Some(password) = io::stdin().lock().lines().next() {
90+
password.map_err(ErrReport::from)
91+
} else {
92+
Err(eyre::eyre!("Error during reading from stdin"))
93+
}
94+
} else {
95+
rpassword::prompt_password("Insert the secret: ").map_err(ErrReport::from)
96+
}?;
8397
map_args_to_code(secret, matches)
8498
}
8599

0 commit comments

Comments
 (0)