Skip to content

Commit aa75d7d

Browse files
committed
Update examples
Supports input from stdin and output to a file.
1 parent 7199926 commit aa75d7d

File tree

4 files changed

+73
-20
lines changed

4 files changed

+73
-20
lines changed

crates/abcrypt/CHANGELOG.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file.
1414
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
1515
project adheres to https://semver.org/[Semantic Versioning].
1616

17+
== {compare-url}/abcrypt-v0.2.7\...HEAD[Unreleased]
18+
19+
=== Changed
20+
21+
* Supports input from stdin and output to a file in the examples
22+
({pull-request-url}/154[#154])
23+
1724
== {compare-url}/abcrypt-v0.2.6\...abcrypt-v0.2.7[0.2.7] - 2023-11-29
1825

1926
=== Changed

crates/abcrypt/examples/decrypt.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
#[derive(Debug, clap::Parser)]
1616
#[command(version, about)]
1717
struct Opt {
18+
/// Output the result to a file.
19+
#[arg(short, long, value_name("FILE"))]
20+
output: Option<std::path::PathBuf>,
21+
1822
/// Input file.
23+
///
24+
/// If [FILE] is not specified, data will be read from stdin.
1925
#[arg(value_name("FILE"))]
20-
input: std::path::PathBuf,
26+
input: Option<std::path::PathBuf>,
2127
}
2228

2329
#[cfg(feature = "std")]
2430
fn main() -> anyhow::Result<()> {
2531
use std::{
2632
fs,
27-
io::{self, Write},
33+
io::{self, Read, Write},
2834
};
2935

3036
use abcrypt::{Decryptor, Error};
@@ -34,8 +40,15 @@ fn main() -> anyhow::Result<()> {
3440

3541
let opt = Opt::parse();
3642

37-
let ciphertext = fs::read(&opt.input)
38-
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
43+
let ciphertext = if let Some(file) = opt.input {
44+
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
45+
} else {
46+
let mut buf = Vec::new();
47+
io::stdin()
48+
.read_to_end(&mut buf)
49+
.context("could not read data from stdin")?;
50+
Ok(buf)
51+
}?;
3952

4053
let passphrase = Password::with_theme(&ColorfulTheme::default())
4154
.with_prompt("Enter passphrase")
@@ -49,10 +62,14 @@ fn main() -> anyhow::Result<()> {
4962
.decrypt_to_vec()
5063
.context("the encrypted data is corrupted")?;
5164

52-
io::stdout()
53-
.write_all(&plaintext)
54-
.context("could not write data to stdout")?;
55-
Ok(())
65+
if let Some(file) = opt.output {
66+
fs::write(&file, plaintext)
67+
.with_context(|| format!("could not write data to {}", file.display()))
68+
} else {
69+
io::stdout()
70+
.write_all(&plaintext)
71+
.context("could not write data to stdout")
72+
}
5673
}
5774

5875
#[cfg(not(feature = "std"))]

crates/abcrypt/examples/encrypt.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#[derive(Debug, clap::Parser)]
1616
#[command(version, about)]
1717
struct Opt {
18+
/// Output the result to a file.
19+
#[arg(short, long, value_name("FILE"))]
20+
output: Option<std::path::PathBuf>,
21+
1822
/// Set the memory size in KiB.
1923
#[arg(short, long, default_value("19456"), value_name("NUM"))]
2024
memory_size: u32,
@@ -28,15 +32,17 @@ struct Opt {
2832
parallelism: u32,
2933

3034
/// Input file.
35+
///
36+
/// If [FILE] is not specified, data will be read from stdin.
3137
#[arg(value_name("FILE"))]
32-
input: std::path::PathBuf,
38+
input: Option<std::path::PathBuf>,
3339
}
3440

3541
#[cfg(feature = "std")]
3642
fn main() -> anyhow::Result<()> {
3743
use std::{
3844
fs,
39-
io::{self, Write},
45+
io::{self, Read, Write},
4046
};
4147

4248
use abcrypt::argon2::Params;
@@ -46,8 +52,15 @@ fn main() -> anyhow::Result<()> {
4652

4753
let opt = Opt::parse();
4854

49-
let plaintext = fs::read(&opt.input)
50-
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
55+
let plaintext = if let Some(file) = opt.input {
56+
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
57+
} else {
58+
let mut buf = Vec::new();
59+
io::stdin()
60+
.read_to_end(&mut buf)
61+
.context("could not read data from stdin")?;
62+
Ok(buf)
63+
}?;
5164

5265
let passphrase = Password::with_theme(&ColorfulTheme::default())
5366
.with_prompt("Enter passphrase")
@@ -57,10 +70,14 @@ fn main() -> anyhow::Result<()> {
5770
let params = Params::new(opt.memory_size, opt.iterations, opt.parallelism, None)?;
5871
let ciphertext = abcrypt::encrypt_with_params(plaintext, passphrase, params)?;
5972

60-
io::stdout()
61-
.write_all(&ciphertext)
62-
.context("could not write data to stdout")?;
63-
Ok(())
73+
if let Some(file) = opt.output {
74+
fs::write(&file, ciphertext)
75+
.with_context(|| format!("could not write data to {}", file.display()))
76+
} else {
77+
io::stdout()
78+
.write_all(&ciphertext)
79+
.context("could not write data to stdout")
80+
}
6481
}
6582

6683
#[cfg(not(feature = "std"))]

crates/abcrypt/examples/info.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
1616
#[command(version, about)]
1717
struct Opt {
1818
/// Input file.
19+
///
20+
/// If [FILE] is not specified, data will be read from stdin.
1921
#[arg(value_name("FILE"))]
20-
input: std::path::PathBuf,
22+
input: Option<std::path::PathBuf>,
2123
}
2224

2325
#[cfg(feature = "std")]
2426
fn main() -> anyhow::Result<()> {
25-
use std::fs;
27+
use std::{
28+
fs,
29+
io::{self, Read},
30+
};
2631

2732
use abcrypt::Params;
2833
use anyhow::Context;
2934
use clap::Parser;
3035

3136
let opt = Opt::parse();
3237

33-
let ciphertext = fs::read(&opt.input)
34-
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
38+
let ciphertext = if let Some(file) = opt.input {
39+
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
40+
} else {
41+
let mut buf = Vec::new();
42+
io::stdin()
43+
.read_to_end(&mut buf)
44+
.context("could not read data from stdin")?;
45+
Ok(buf)
46+
}?;
3547

3648
let params = Params::new(ciphertext).context("data is not a valid abcrypt encrypted file")?;
3749
println!(

0 commit comments

Comments
 (0)