Skip to content

Commit 6d93db2

Browse files
committed
allow passing message from stdin to encrypt and decrypt
1 parent a1943d7 commit 6d93db2

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/commands/decrypt.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1+
use std::io::Read;
2+
13
use super::*;
24
use crate::utils::{config::Config, rpgp::decrypt_full};
35
use anyhow::{Context, Result};
46

57
/// Decrypt a string using GPG
68
#[derive(Parser)]
79
pub struct Args {
8-
message: String,
10+
message: Option<String>,
911
}
1012

1113
pub async fn command(args: Args) -> Result<()> {
1214
let config = Config::get().context("Failed to get config")?;
1315

14-
let decrypted = decrypt_full(args.message, &config)?;
16+
let message = match args.message {
17+
Some(m) => m,
18+
None => {
19+
let mut buffer = String::new();
20+
std::io::stdin()
21+
.read_to_string(&mut buffer)
22+
.context("Failed to read from stdin")?;
23+
buffer
24+
}
25+
};
26+
27+
if message.is_empty() {
28+
return Err(anyhow::anyhow!(
29+
"No message provided.\nUsage: envx decrypt [message] or echo [message] | envx decrypt"
30+
));
31+
}
32+
33+
let decrypted = decrypt_full(message, &config)?;
1534

1635
println!("{}", decrypted);
1736

src/commands/encrypt.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::io::Read;
2+
13
use anyhow::Context;
24

35
use crate::utils::{
@@ -11,10 +13,11 @@ use super::*;
1113
#[derive(Parser)]
1214
pub struct Args {
1315
/// recipient's public key fingerprint
16+
#[clap(long, short)]
1417
recipient: String,
1518

1619
/// string to encrypt
17-
message: String,
20+
message: Option<String>,
1821
}
1922

2023
pub async fn command(args: Args) -> Result<()> {
@@ -28,7 +31,22 @@ pub async fn command(args: Args) -> Result<()> {
2831
let primary_public_key = std::fs::read_to_string(primary_key_location)
2932
.context("Failed to read primary key")?;
3033

31-
let encrypted = encrypt(&args.message, primary_public_key.as_str())?;
34+
let message = match args.message {
35+
Some(message) => message,
36+
None => {
37+
let mut message = String::new();
38+
std::io::stdin()
39+
.read_to_string(&mut message)
40+
.context("Failed to read message")?;
41+
message
42+
}
43+
};
44+
45+
if message.is_empty() {
46+
anyhow::bail!("Message is empty");
47+
}
48+
49+
let encrypted = encrypt(&message, primary_public_key.as_str())?;
3250

3351
println!("{}", encrypted);
3452

src/commands/keyring/clear.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ pub struct Args {
1212
/// Partial fingerprint of the key to set
1313
#[clap(short, long)]
1414
key: Option<String>,
15+
16+
/// Clear all saved keys
17+
#[clap(short, long)]
18+
all: bool,
1519
}
1620

1721
pub async fn command(args: Args) -> Result<()> {
1822
let config = Config::get()?;
1923

24+
if args.all {
25+
config.keys.iter().for_each(|key| {
26+
clear_password(&key.fingerprint).unwrap();
27+
});
28+
return Ok(());
29+
}
30+
2031
let fingerprint = match args.key {
2132
Some(key) => config.get_key(&key)?.fingerprint,
2233
None => {

src/commands/keyring/view.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ pub struct Args {
1515
#[clap(short, long)]
1616
key: Option<String>,
1717

18-
/// Force, don't prompt for confirmation
18+
/// Don't prompt for confirmation
1919
#[clap(short, long)]
20-
force: bool,
20+
yes: bool,
2121
}
2222

2323
pub async fn command(args: Args) -> Result<()> {
@@ -33,7 +33,7 @@ pub async fn command(args: Args) -> Result<()> {
3333

3434
let password = get_password(&fingerprint)?;
3535

36-
if args.force {
36+
if args.yes {
3737
println!("{}", password);
3838
return Ok(());
3939
}

0 commit comments

Comments
 (0)