39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
use doot_core::{encryption::AgeEncryption, Config};
|
|
use std::path::PathBuf;
|
|
|
|
pub fn run(file: PathBuf, recipient: Option<String>, verbose: bool) -> anyhow::Result<()> {
|
|
let config_dir = Config::default_config_dir();
|
|
let recipient_key = if let Some(r) = recipient {
|
|
r
|
|
} else if let Ok(key) = std::env::var("DOOT_AGE_RECIPIENT") {
|
|
key
|
|
} else {
|
|
let key_file = config_dir.join("recipient.txt");
|
|
if key_file.exists() {
|
|
std::fs::read_to_string(&key_file)?.trim().to_string()
|
|
} else {
|
|
anyhow::bail!(
|
|
"no recipient specified. use --recipient, DOOT_AGE_RECIPIENT env var, or {}",
|
|
key_file.display()
|
|
);
|
|
}
|
|
};
|
|
|
|
if verbose {
|
|
println!("encrypting {} with recipient {}", file.display(), &recipient_key[..20]);
|
|
}
|
|
|
|
let mut encryption = AgeEncryption::new();
|
|
encryption.add_recipient(&recipient_key)?;
|
|
|
|
let output = file.with_extension(
|
|
file.extension()
|
|
.map(|e| format!("{}.age", e.to_string_lossy()))
|
|
.unwrap_or_else(|| "age".to_string()),
|
|
);
|
|
|
|
encryption.encrypt_file(&file, &output)?;
|
|
|
|
println!("encrypted {} -> {}", file.display(), output.display());
|
|
Ok(())
|
|
}
|