Main: Rewrite arguments parsing using clap

This commit is contained in:
networkException 2024-09-12 02:52:25 +02:00
parent d248ae29e5
commit 7096902052
Signed by: networkException
GPG key ID: E3877443AE684391

View file

@ -1,21 +1,35 @@
use std::env;
use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use serde_json::{Map, Value};
use url_escape;
use handlebars::{Context, Handlebars, Helper, HelperResult, JsonRender, Output, RenderContext, RenderErrorReason};
#[derive(clap::Parser)]
#[command(author, version, about, long_about = None)]
struct Arguments {
/// The path to the contents file
contents_path: PathBuf,
/// The path to write the built secret file to
output_path: PathBuf,
}
fn main() -> Result<()> {
let Arguments {
contents_path,
output_path,
} = Arguments::parse();
let mut handlebars = Handlebars::new();
let path_to_contents = env::args().nth(1).expect("The first argument to be present.");
let output_path = env::args().nth(2).expect("The second argument to be present.");
let contents = fs::read_to_string(&path_to_contents)
.expect(format!("To be able to read the contents file at the path '{path_to_contents}' passed as the first argument.").as_str());
let contents = fs::read_to_string(&contents_path)
.expect(format!("To be able to read the contents file at the path '{}' passed as the first argument.", contents_path.to_string_lossy()).as_str());
let secret_inputs = env::vars().filter(|(key, _)| key.starts_with("SECRET_"));
@ -75,7 +89,7 @@ fn main() -> Result<()> {
let output = handlebars.render_template(contents.as_str(), &Value::Object(data_map))?;
fs::write(&output_path, output)
.expect(format!("To be able to write to {output_path}").as_str());
.expect(format!("To be able to write to '{}'", output_path.to_string_lossy()).as_str());
Ok(())
}