openpgp-key-janitor/src/setup.rs
2023-05-31 18:13:49 +02:00

32 lines
1 KiB
Rust

use std::path::PathBuf;
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use crate::{paths::Paths, spec::Spec};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub(crate) struct Cli {
/// Path to the directory it should operate on. Defaults to the current working directory.
#[arg(value_name = "DIR")]
pub(crate) path: Option<PathBuf>,
}
pub(crate) fn setup() -> Result<(Paths, Spec)> {
let cli = Cli::parse();
let base_dir = cli
.path
.ok_or_else(|| anyhow!("No path specified in CLI parameters."))
.or_else(|_| std::env::current_dir())
.context(
"Couldn't get current dir from env, and no path was specified in CLI parameters either",
)?;
let paths = Paths::new(base_dir);
let spec_string =
std::fs::read_to_string(&paths.spec).context("Failed to read spec into string")?;
let spec: Spec =
serde_yaml::from_str(&spec_string).context("Failed to parse spec from string")?;
Ok((paths, spec))
}