Skip to content

Commit

Permalink
Merge pull request #4 from quiye/on
Browse files Browse the repository at this point in the history
add on option
  • Loading branch information
quiye authored Feb 26, 2019
2 parents 1213dc9 + 6ba0efb commit 463785b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "varust"
version = "0.1.1"
version = "0.1.2"
authors = ["Yuya Ishida <[email protected]>"]
edition = "2018"

Expand Down
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
# varust
# Varust

A simple CLI tool to extract environment variables from yaml file.

## install
## Installing

```sh
cargo install --git https://github.com/quiye/varust.git
```

## usage
## Usage

[sample.yaml](sample.yaml) is a simple structured yaml file.

```sh
$ cat sample.yaml
# shared variables
shared:
environment:
REPLICATION: 3
CONSISTENCY: ONE
# variables for production
prod:
environment:
USER: prod
URL: https://prod-env.com
PORT: 443
REPLICATION: 5
CONSISTENCY: ALL
# variables for develop
dev:
environment:
USER: dev
URL: http://dev-env.com
PORT: 80
CONSISTENCY: TWO
```

From above file, we can extract productional environment variables by varust.

```sh
$ varust prod.environment sample.yaml
USER=prod
REPLICATION=5
CONSISTENCY=ALL
URL=https://prod-env.com
PORT=443
```
Expand All @@ -39,7 +48,23 @@ On the other hand, we can extract variables for develop environment.

```sh
$ varust dev.environment sample.yaml
URL=http://dev-env.com
PORT=80
USER=dev
URL=http://dev-env.com
CONSISTENCY=TWO
```

### Options

#### `-o (--on)`

By using this option, we can expand `dev.environment` on a shared setting `shared.environment`.

Look below !

```sh
$ varust dev.environment --on shared.environment sample.yaml
PORT=80
REPLICATION=3
CONSISTENCY=TWO
URL=http://dev-env.com
```
12 changes: 10 additions & 2 deletions sample.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# shared variables
shared:
environment:
REPLICATION: 3
CONSISTENCY: ONE
# variables for production
prod:
environment:
USER: prod
URL: https://prod-env.com
PORT: 443
REPLICATION: 5
CONSISTENCY: ALL
# variables for develop
dev:
environment:
USER: dev
URL: http://dev-env.com
PORT: 80
CONSISTENCY: TWO
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ use yaml_rust::{Yaml, YamlLoader};

#[derive(StructOpt)]
struct Cli {
root: String,
node: String,
#[structopt(parse(from_os_str))]
path: std::path::PathBuf,
#[structopt(short = "o", long = "on")]
base_node: Option<String>,
}

fn main() {
let args = Cli::from_args();
let content = std::fs::read_to_string(&args.path).expect("could not read file");
let docs = YamlLoader::load_from_str(&content).unwrap();
let doc = &docs[0];
let map = search_nodes(doc, &args.root);
let map = search_nodes(doc, &args.node);
let mut base_map = match args.base_node {
Some(node) => search_nodes(doc, &node),
None => HashMap::new(),
};

for (k, v) in map {
base_map.insert(k, v);
}
for (k, v) in base_map {
println!("{}={}", k, v);
}
}
Expand All @@ -29,7 +39,6 @@ fn search_nodes<'a>(yaml: &'a yaml_rust::Yaml, path: &str) -> HashMap<&'a str, S
) -> HashMap<&'a str, String> {
if pathv.len() == 1 {
let &node_name = pathv.first().unwrap();
// let Yaml::Hash(h) = yaml[node_name];
match &yaml[node_name] {
Yaml::Hash(h) => {
let mut map: HashMap<&str, String> = HashMap::new();
Expand Down

0 comments on commit 463785b

Please sign in to comment.