Main: Rewrite arguments parsing using clap
This commit is contained in:
parent
d248ae29e5
commit
7096902052
1 changed files with 20 additions and 6 deletions
26
src/main.rs
26
src/main.rs
|
@ -1,21 +1,35 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
use url_escape;
|
use url_escape;
|
||||||
|
|
||||||
use handlebars::{Context, Handlebars, Helper, HelperResult, JsonRender, Output, RenderContext, RenderErrorReason};
|
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<()> {
|
fn main() -> Result<()> {
|
||||||
|
let Arguments {
|
||||||
|
contents_path,
|
||||||
|
output_path,
|
||||||
|
} = Arguments::parse();
|
||||||
|
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
|
|
||||||
let path_to_contents = env::args().nth(1).expect("The first argument to be present.");
|
let contents = fs::read_to_string(&contents_path)
|
||||||
let output_path = env::args().nth(2).expect("The second argument to be present.");
|
.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 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 secret_inputs = env::vars().filter(|(key, _)| key.starts_with("SECRET_"));
|
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))?;
|
let output = handlebars.render_template(contents.as_str(), &Value::Object(data_map))?;
|
||||||
|
|
||||||
fs::write(&output_path, output)
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue