1use std::{fmt::Debug, path::PathBuf};
2
3use clap::Parser;
4use miette::{IntoDiagnostic, Result, WrapErr};
5use tokio::fs::remove_file;
6
7use crate::{
8 files::{append_age_ext, encrypt_file},
9 passphrases::PassphraseArgs,
10};
11
12#[derive(Debug, Clone, Parser)]
20pub struct ProtectArgs {
21 pub input: PathBuf,
23
24 #[arg(short, long)]
28 pub output: Option<PathBuf>,
29
30 #[arg(long = "rm")]
32 pub remove: bool,
33
34 #[command(flatten)]
35 #[allow(missing_docs, reason = "don't interfere with clap")]
36 pub key: PassphraseArgs,
37}
38
39pub async fn run(
41 ProtectArgs {
42 ref input,
43 output,
44 key,
45 remove,
46 }: ProtectArgs,
47) -> Result<()> {
48 let key = key.require_with_confirmation().await?;
49 let output = output.unwrap_or_else(|| append_age_ext(input));
50
51 encrypt_file(input, output, Box::new(key)).await?;
52
53 if remove {
54 remove_file(input)
55 .await
56 .into_diagnostic()
57 .wrap_err("deleting input file")?;
58 }
59
60 Ok(())
61}