Skip to content

Commit

Permalink
Add YAML support to CLI (#3)
Browse files Browse the repository at this point in the history
* Add yaml support

* Update README
  • Loading branch information
guangie88 authored Apr 25, 2019
1 parent 4498697 commit 8cafedc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ structopt = "0.2.15"
serde = "1.0.90"
toml = "0.5.0"
serde_json = "1.0.39"
serde_yaml = "0.8.8"

[[bin]]
name = "tera"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Tera CLI for one-off template interpolation from context file / env vars.

The following context formats are supported:

- TOML context file
- JSON context file
- JSON context file (`--json .` defaults to `.tera.json`)
- TOML context file (`--toml .` defaults to `.tera.toml`)
- YAML context file (`--yaml .` defaults to `.tera.yml`)
- Environment variables

## Simple Examples
Expand Down
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde_json;
use serde_yaml;
use std::{
collections::HashMap,
env,
Expand Down Expand Up @@ -55,6 +56,11 @@ struct ContextFormat {
#[structopt(name = "json", long, group = "format", parse(from_os_str))]
json: Option<PathBuf>,

/// YAML file path to read context values.
/// "." to indicate reading from default ".tera.yml"
#[structopt(name = "yaml", long, group = "format", parse(from_os_str))]
yaml: Option<PathBuf>,

/// Use env vars as the context instead
#[structopt(name = "env", long, group = "format")]
env: bool,
Expand Down Expand Up @@ -117,6 +123,14 @@ fn read_context(conf: &Args) -> CliResult<Context> {
let mut context = Context::new();
context.insert(&conf.root_key, &value);
Ok(context)
} else if let Some(ref path) = conf.context.yaml {
// YAML
let path = get_config_path(path, ".yml");
let value: serde_yaml::Value =
serde_yaml::from_str(&fs::read_to_string(&path)?)?;
let mut context = Context::new();
context.insert(&conf.root_key, &value);
Ok(context)
} else if conf.context.env {
let env_vars = env::vars().collect::<HashMap<String, String>>();
let mut context = Context::new();
Expand Down

0 comments on commit 8cafedc

Please sign in to comment.