File tree Expand file tree Collapse file tree 4 files changed +55
-7
lines changed
Expand file tree Collapse file tree 4 files changed +55
-7
lines changed Original file line number Diff line number Diff line change 1+ use std:: io:: Read ;
2+
13use super :: * ;
24use crate :: utils:: { config:: Config , rpgp:: decrypt_full} ;
35use anyhow:: { Context , Result } ;
46
57/// Decrypt a string using GPG
68#[ derive( Parser ) ]
79pub struct Args {
8- message : String ,
10+ message : Option < String > ,
911}
1012
1113pub 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.\n Usage: envx decrypt [message] or echo [message] | envx decrypt"
30+ ) ) ;
31+ }
32+
33+ let decrypted = decrypt_full ( message, & config) ?;
1534
1635 println ! ( "{}" , decrypted) ;
1736
Original file line number Diff line number Diff line change 1+ use std:: io:: Read ;
2+
13use anyhow:: Context ;
24
35use crate :: utils:: {
@@ -11,10 +13,11 @@ use super::*;
1113#[ derive( Parser ) ]
1214pub 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
2023pub 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
Original file line number Diff line number Diff 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
1721pub 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 => {
Original file line number Diff line number Diff 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
2323pub 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 }
You can’t perform that action at this time.
0 commit comments