use std::io::Read;
use clap::Parser;
use magic_crypt::{MagicCryptTrait, new_magic_crypt};
#[derive(Parser, Debug)]
#[command(
version,
about = "Secrets CLI",
long_about = "CLI tool to encode/decode secrets files"
)]
struct Args {
#[arg(short, long)]
filepath: String,
#[arg(short, long)]
text: Option<String>,
#[arg(short, long, default_value = "secret")]
password: String,
#[arg(short, long, default_value_t = false, action = clap::ArgAction::SetTrue)]
copy: bool,
#[arg(short, long, default_value_t = false, action = clap::ArgAction::SetTrue)]
overwrite: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let mcrypt = new_magic_crypt!(args.password, 256);
if let Some(text) = args.text {
let decrypted_data = mcrypt
.decrypt_base64_to_bytes(text)
.expect("Failed to decrypt base64 data");
if std::path::Path::new(&args.filepath).exists() {
if args.overwrite {
println!("Overwriting existing {}", args.filepath);
} else {
let backup_path = format!("{}.bak", args.filepath);
std::fs::rename(&args.filepath, &backup_path)
.expect(format!("Failed to rename existing file to {}", backup_path).as_str());
println!("Created backup {}", backup_path);
}
}
std::fs::write(&args.filepath, &decrypted_data)
.expect(format!("Failed to write decrypted data to {}", args.filepath).as_str());
println!("Decoded to {}", args.filepath);
}
else {
let mut file = std::fs::File::open(&args.filepath)
.expect(format!("Failed to open {}", args.filepath).as_str());
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.expect(format!("Failed to read {}", args.filepath).as_str());
let encoded_json = mcrypt.encrypt_bytes_to_base64(&buffer);
println!("{}", encoded_json);
if args.copy {
let mut clipboard = arboard::Clipboard::new().expect("Failed to open clipboard");
clipboard
.set_text(encoded_json.clone())
.expect("Failed to copy to clipboard");
println!("Copied to clipboard");
}
}
Ok(())
}