algae_cli/cli/
reveal.rs

1use std::{fmt::Debug, path::PathBuf};
2
3use clap::Parser;
4use miette::{miette, Result};
5
6use crate::{
7	files::{decrypt_file, remove_age_ext},
8	passphrases::PassphraseArgs,
9};
10
11/// Decrypt a file using a passphrase.
12///
13/// Whenever possible, prefer to use `encrypt` and `decrypt` with identity files
14/// (public key cryptography).
15///
16/// This utility may also be used to convert a passphrase-protected identity
17/// file into a plaintext one.
18#[derive(Debug, Clone, Parser)]
19pub struct RevealArgs {
20	/// File to be decrypted.
21	pub input: PathBuf,
22
23	/// Path or filename to write the decrypted file to.
24	///
25	/// If the input file has a `.age` extension, this can be automatically derived (by removing the
26	/// `.age`). Otherwise, this option is required.
27	#[arg(short, long)]
28	pub output: Option<PathBuf>,
29
30	#[command(flatten)]
31	#[allow(missing_docs, reason = "don't interfere with clap")]
32	pub key: PassphraseArgs,
33}
34
35/// CLI command for the `reveal` operation (passphrase decryption).
36pub async fn run(
37	RevealArgs {
38		ref input,
39		output,
40		key,
41	}: RevealArgs,
42) -> Result<()> {
43	let key = key.require().await?;
44	let output = if let Some(ref path) = output {
45		path.to_owned()
46	} else {
47		remove_age_ext(input)
48			.map_err(|_| miette!("Cannot guess output path, use --output to set one"))?
49	};
50
51	decrypt_file(input, output, Box::new(key)).await?;
52	Ok(())
53}