From 7096902052806c6863ca32668e213c2a5a909851 Mon Sep 17 00:00:00 2001 From: networkException Date: Thu, 12 Sep 2024 02:52:25 +0200 Subject: [PATCH] Main: Rewrite arguments parsing using clap --- src/main.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b8b554..d047213 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }