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 keys::KeyArgs,
10};
11
12#[derive(Debug, Clone, Parser)]
18pub struct EncryptArgs {
19 pub input: PathBuf,
21
22 #[arg(short, long)]
26 pub output: Option<PathBuf>,
27
28 #[arg(long = "rm")]
30 pub remove: bool,
31
32 #[command(flatten)]
33 #[allow(missing_docs, reason = "don't interfere with clap")]
34 pub key: KeyArgs,
35}
36
37pub async fn run(
39 EncryptArgs {
40 ref input,
41 output,
42 key,
43 remove,
44 }: EncryptArgs,
45) -> Result<()> {
46 let public_key = key.require_public_key().await?;
47 let output = output.unwrap_or_else(|| append_age_ext(input));
48
49 encrypt_file(input, output, public_key).await?;
50
51 if remove {
52 remove_file(input)
53 .await
54 .into_diagnostic()
55 .wrap_err("deleting input file")?;
56 }
57
58 Ok(())
59}