algae_cli/cli/
decrypt.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	keys::KeyArgs,
9};
10
11/// Decrypt a file using a secret key or an identity.
12///
13/// Either of `--key-path` or `--key` must be provided.
14///
15/// For symmetric cryptography (using a passphrase), see `protect`/`reveal`.
16#[derive(Debug, Clone, Parser)]
17pub struct DecryptArgs {
18	/// File to be decrypted.
19	pub input: PathBuf,
20
21	/// Path or filename to write the decrypted file to.
22	///
23	/// If the input file has a `.age` extension, this can be automatically
24	/// derived (by removing the `.age`). Otherwise, this option is required.
25	#[arg(short, long)]
26	pub output: Option<PathBuf>,
27
28	#[command(flatten)]
29	#[allow(missing_docs, reason = "don't interfere with clap")]
30	pub key: KeyArgs,
31}
32
33/// CLI command for the `decrypt` operation (secret key decryption).
34pub async fn run(
35	DecryptArgs {
36		ref input,
37		output,
38		key,
39	}: DecryptArgs,
40) -> Result<()> {
41	let secret_key = key.require_secret_key().await?;
42	let output = if let Some(ref path) = output {
43		path.to_owned()
44	} else {
45		remove_age_ext(input)
46			.map_err(|_| miette!("Cannot guess output path, use --output to set one"))?
47	};
48
49	decrypt_file(input, output, secret_key).await?;
50	Ok(())
51}