From 52b3b7df7fae5fcfc20d86c47a07474a55f0eced Mon Sep 17 00:00:00 2001 From: Sergio Medina Date: Fri, 6 Sep 2019 15:03:16 +0100 Subject: [PATCH] WIP add suport to hcl --- Cargo.toml | 1 + README.md | 17 +++++++++++++++++ src/main.rs | 25 ++++++++++++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f6409c..5e32282 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ serde_json = "1.0.40" serde_yaml = "0.8.9" glob = "0.3.0" base64 = "0.10.1" +molysite = "" [[bin]] name = "j2_render" diff --git a/README.md b/README.md index 3ed65fb..2521ab3 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ # j2_render + +j2_render is CLI tool to render [jinja 2]() templates, it can load the context from different sources. + +j2_render is writen in rust and uses [tera]() as jinja 2 engine, +may be some differences between implementations, so look at tera's documentation for reference + +## Installation + +```bash +export J2_RENDER_VERSION=v0.0.1 +sudo curl -L "https://github.com/lumasepa/j2_render/releases/download/${J2_RENDER_VERSION}/j2_render_$(uname | tr '[:upper:]' '[:lower:]')_amd64" -o /usr/local/bin/j2_render +sudo chmod +x /usr/local/bin/j2_render +``` + +## Usage + +#### Usage Examples diff --git a/src/main.rs b/src/main.rs index e8369e3..db0c1ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(or_patterns)] +use molysite::hcl::parse_hcl; use serde_json; use serde_yaml; use std::ffi::OsStr; @@ -21,7 +22,8 @@ mod testers; pub fn help() { println!( " -renderman [opts] +j2_render [opts] + --stdin -i [json,yaml,hcl,tfvars,template] read from stdin context or template --out [file_path] output file for rendered template, default stdout --env -e load env vars in ctx @@ -32,18 +34,20 @@ renderman [opts] ) } -pub fn parse_args() -> (String, Option, Context) { +pub fn parse_args() -> (String, Option, bool, Context) { let mut args = env::args().collect::>(); args.reverse(); let mut template = String::new(); let mut context = Context::new(); let mut out = None; + let mut print_ctx = false; args.pop(); // binary name while let Some(arg) = args.pop() { match arg.as_str() { + "--print-ctx" | "-p" => print_ctx = true, "--out" | "-o" => { let filepath = args .pop() @@ -93,7 +97,7 @@ pub fn parse_args() -> (String, Option, Context) { _ => panic!("Error argument {} not recognized", arg), } } - return (template, out, context); + return (template, out, print_ctx, context); } pub fn populate_ctx(context: &mut Context, format: String, data: String) { @@ -120,14 +124,25 @@ pub fn populate_ctx(context: &mut Context, format: String, data: String) { context.insert(k, v); } } - "hcl" | "tfvars" | "tf" => panic!("Format not already supported"), + "hcl" | "tfvars" | "tf" => { + let parsed_hcl = parse_hcl(&data).expect("Error parsing hcl/tf/tfvars"); + let object = value + .as_object() + .expect("Error expected object in root of hcl/tf/tfvars file"); + for (k, v) in object.iter() { + context.insert(k, v); + } + } _ => panic!("Format {} not recognized", format), } } pub fn main() -> std::result::Result<(), String> { - let (template, out, context) = parse_args(); + let (template, out, print_ctx, context) = parse_args(); + if print_ctx { + println!() + } let mut tera = Tera::default(); tera.add_raw_template("template", &template) .expect("Error loading template in engine");