#![doc = include_str!("../README.md")]
#![warn(clippy::large_stack_arrays)]
#![warn(clippy::arithmetic_side_effects)]
#![warn(clippy::expect_used)]
#![warn(clippy::unwrap_used)]
#![warn(clippy::indexing_slicing)]
#![warn(clippy::panic)]
#![warn(clippy::todo)]
#![warn(clippy::unimplemented)]
#![warn(clippy::unreachable)]
#![warn(clippy::missing_panics_doc)]
#![warn(clippy::allow_attributes_without_reason)]
#![warn(clippy::cognitive_complexity)]
mod cli;
use crate::cli::{CliArgs, Environment};
use std::io::{self, Error, Read, Write};
use std::ops::Deref;
use std::{env, fs, process};
fn abort(error: Error) -> ! {
const USAGE: &str = include_str!("../USAGE.txt");
eprintln!("{error}");
eprintln!("-----");
eprintln!("");
eprintln!("{USAGE}");
process::exit(1);
}
fn application() -> Result<(), Error> {
let CliArgs { flag_userauth, flag_seal: flag_create, name } = CliArgs::from_env()?;
let Environment { cwd, userauth } = Environment::from_env();
if let Some(cwd) = cwd {
env::set_current_dir(cwd)?;
}
let user_auth = match (&flag_userauth, &userauth) {
(Some(flag_userauth), _) => Some(flag_userauth.as_bytes()),
(_, Some(userauth)) => Some(userauth.as_bytes()),
_ => None,
};
match flag_create {
Some(flag_create) => {
let mut key = Vec::new();
io::stdin().read_to_end(&mut key)?;
let sealed_key = keycache::seal(&key, flag_create, user_auth)?;
fs::write(name.deref(), sealed_key)?;
Ok(())
}
None => {
let sealed_key = fs::read(name.deref())?;
let key = keycache::open(&sealed_key, user_auth)?;
io::stdout().write_all(&key)?;
Ok(())
}
}
}
pub fn main() {
let _ = application().map_err(abort);
}