Skip to content

Commit

Permalink
WIP add suport to hcl
Browse files Browse the repository at this point in the history
  • Loading branch information
lumasepa committed Sep 6, 2019
1 parent 6e73cb5 commit 52b3b7d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
25 changes: 20 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(or_patterns)]

use molysite::hcl::parse_hcl;
use serde_json;
use serde_yaml;
use std::ffi::OsStr;
Expand All @@ -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
Expand All @@ -32,18 +34,20 @@ renderman [opts]
)
}

pub fn parse_args() -> (String, Option<String>, Context) {
pub fn parse_args() -> (String, Option<String>, bool, Context) {
let mut args = env::args().collect::<Vec<String>>();
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()
Expand Down Expand Up @@ -93,7 +97,7 @@ pub fn parse_args() -> (String, Option<String>, 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) {
Expand All @@ -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");
Expand Down

0 comments on commit 52b3b7d

Please sign in to comment.