Skip to content

Commit

Permalink
fix typo of 'active' to 'alive'... duh. (solve mocking 'svc.'). add '…
Browse files Browse the repository at this point in the history
…--quiet' flag to suppress log messages. Update README with commands to output files to homedir to avoid having to keep unstaging the results/ dir...

Signed-off-by: qubitrenagade <qubitrenegade@gmail.com>
  • Loading branch information
qubitrenegade committed Feb 3, 2019
1 parent 5e6aaf7 commit 72d9ce5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
3 changes: 3 additions & 0 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ pub fn get() -> App<'static, 'static> {
(@arg MOCK_DATA: -m --("mock-data") +takes_value "Path to json file with mock data for template, defaults to none")
(@arg PRINT: -p --("print") "Prints config to STDOUT")
(@arg RENDER_DIR: -r --("render-dir") +takes_value "Path to render templates to, defaults to ./results/")
// --no-render doesn't work...
(@arg NO_WRITE_FILE: -n --("no-render") --("no-render-dir") "Don't write anything to disk, ignores --render-dir")
(@arg QUIET: -q --("no-verbose") --quiet
"Don't print any helper messages. When used with `--print` will only print config file")
)
)
(@subcommand ring =>
Expand Down
51 changes: 36 additions & 15 deletions components/hab/src/command/plan/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ pub fn start(
print: bool,
no_render_dir: bool,
render_dir: String,
quiet: bool,
) -> Result<()> {
// Strip the file name out of our passed template
let file_name = match Path::new(&template_path).file_name() {
Some(name) => name.to_str().clone().unwrap(),
None => panic!(format!("Something went wrong getting filename of {}", &template_path)),
};

ui.begin(format!("Rendering: {} into: {}/ as: {}", template_path, render_dir, file_name))?;
ui.br()?;
if !(quiet) {
ui.begin(format!("Rendering: {} into: {}/ as: {}", template_path, render_dir, file_name))?;
ui.br()?;
}

// read our template from file
let template = read_to_string(&template_path)
Expand All @@ -49,8 +52,11 @@ pub fn start(
// create a "data" json struct
let mut data: Json = serde_json::from_str("{}").unwrap();

// import default.toml values, convert to JSON
ui.begin(format!("Importing default.toml: {}", &default_toml_path))?;
if !(quiet) {
// import default.toml values, convert to JSON
ui.begin(format!("Importing default.toml: {}", &default_toml_path))?;
}

// we should always have a default.toml, would be nice to "autodiscover" based on package name,
// for now assume we're working in the plan dir if --default-toml not passed
let default_toml = read_to_string(&default_toml_path)
Expand All @@ -62,8 +68,10 @@ pub fn start(
// ui.begin(format!("Importing user.toml: {}", &user_toml_path));
let user_toml = match user_toml_path {
Some(path) => {
// print helper message, maybe only print if '--verbose'? how?
ui.begin(format!("Importing user.toml: {}", path.to_string()))?;
if !(quiet) {
// print helper message, maybe only print if '--verbose'? how?
ui.begin(format!("Importing user.toml: {}", path.to_string()))?;
}
read_to_string(path.to_string())
.expect(&format!("Something went wrong reading: {}", path.to_string()))
},
Expand All @@ -75,8 +83,10 @@ pub fn start(
// read mock data if provided
let mock_data = match mock_data_path {
Some(path) => {
// print helper message, maybe only print if '--verbose'? how?
ui.begin(format!("Importing override file: {}", path.to_string()))?;
if !(quiet) {
// print helper message, maybe only print if '--verbose'? how?
ui.begin(format!("Importing override file: {}", path.to_string()))?;
}
read_to_string(path.to_string())
.expect(&format!("Something went wrong reading: {}", path.to_string()))
},
Expand All @@ -97,18 +107,27 @@ pub fn start(
let rendered_template = renderer.render(&template_path, &data).ok().unwrap();

if print {
ui.br()?;
ui.warn(format!("###======== Rendered template: {}", &template_path))?;
if !(quiet) {
ui.br()?;
ui.warn(format!("###======== Rendered template: {}", &template_path))?;
}

println!("{}", rendered_template);
ui.warn(format!("========### End rendered template: {}", &template_path))?;

if !(quiet) {
ui.warn(format!("========### End rendered template: {}", &template_path))?;
}
}

// if not no render dir (aka "unless no_render_dir == true")
if !(no_render_dir) {
// Render our template file
create_with_template(ui, &format!("{}/{}", render_dir, file_name), &rendered_template)?;
create_with_template(ui, &format!("{}/{}", render_dir, file_name), &rendered_template, quiet)?;
}

ui.br()?;
if !(quiet) {
ui.br()?;
}
// not really sure this is correct...
Ok(())
}
Expand Down Expand Up @@ -142,9 +161,11 @@ fn merge(a: &mut Json, b: Json) {

// This is almost a dupe of the method in plan/init, except we don't care if the file exists and go
// ahead and overwite it. I feel like maybe a different name would be good?
fn create_with_template(ui: &mut UI, location: &str, template: &str) -> Result<()> {
fn create_with_template(ui: &mut UI, location: &str, template: &str, quiet: bool) -> Result<()> {
let path = Path::new(&location);
ui.status(Status::Creating, format!("file: {}", location))?;
if !(quiet) {
ui.status(Status::Creating, format!("file: {}", location))?;
}
// If the directory doesn't exist we need to make it.
if let Some(directory) = path.parent() {
create_dir_all(directory)?;
Expand Down
2 changes: 2 additions & 0 deletions components/hab/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ fn sub_plan_render(ui: &mut UI, m: &ArgMatches<'_>) -> Result<()> {

let print = m.is_present("PRINT");
let no_render_dir = m.is_present("NO_WRITE_FILE");
let quiet = m.is_present("QUIET");

let render_dir = match m.value_of("RENDER_DIR") {
Some(name) => name.to_string(),
Expand All @@ -692,6 +693,7 @@ fn sub_plan_render(ui: &mut UI, m: &ArgMatches<'_>) -> Result<()> {
print,
no_render_dir,
render_dir,
quiet,
)
}

Expand Down
15 changes: 10 additions & 5 deletions test/fixtures/render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ cargo run -- plan render ../../test/fixtures/render/consul/config/consul_config.
--default-toml ../../test/fixtures/render/consul/default.toml \
--user-toml ../../test/fixtures/render/consul/user.toml \
--mock-data ../../test/fixtures/render/consul/override.json \
--render-dir result/config \
--render-dir ~/result/config \
--print
```

```
cargo run -- plan render ../../test/fixtures/render/consul/config/consul_config.json \
--default-toml ../../test/fixtures/render/consul/default.toml \
--render-dir result/config \
--render-dir ~/result/config \
--print
```

Expand All @@ -34,7 +34,7 @@ cargo run -- plan render ../../test/fixtures/render/consul/hooks/run \
--default-toml ../../test/fixtures/render/consul/default.toml \
--user-toml ../../test/fixtures/render/consul/user.toml \
--mock-data ../../test/fixtures/render/consul/override.json \
--render-dir result/hooks \
--render-dir ~/result/hooks \
--print
```

Expand Down Expand Up @@ -83,5 +83,10 @@ exec consul agent ${CONSUL_OPTS}

# TODO:

* Figure out how to load `svc` data for `eachAlive` helper
* figure out how to load `pkg.` data. e.g. for `{{pkg.svc_config_path}}`
* [x] ! ~~Figure out how to load `svc` data for `eachAlive` helper~~
* [x] ! Figured out how to mock that data... helps to spell things right... ("alive" not "active") duh...
* [ ] ? figure out how to load `pkg.` data. e.g. for `{{pkg.svc_config_path}}`
* [x] ! figured out how to mock `pkg.` data, which I think "override.json" should override.
* [ ] ? figure out how to have multiple `--mock-data` params.. e.g.: `--mock-data test/day00.json`, `--mock-data test/day01-with-failed-member.json`
* ~~[ ]~~ ~~I want to make it `ersatz_data` instead of `mock_data`.~~ that's dumb.

10 changes: 7 additions & 3 deletions test/fixtures/render/consul/override.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cfg": {
"website": false,
"server": {
"datacenter": "IN_OVERRIDE_JSON"
},
Expand All @@ -14,9 +15,12 @@
},
"svc": {
"members": [
{ "active": true, "sys": { "ip": "1.1.1.1" }},
{ "properties": { "active": true}, "sys": { "ip": "2.2.2.2" }},
{ "sys": { "ip": "3.3.3.3" }}
{ "alive": true, "sys": { "ip": "1.1.1.1" }},
{ "alive": true, "sys": { "ip": "2.2.2.2" }},
{ "alive": true, "sys": { "ip": "3.3.3.3" }}
]
},
"pkg": {
"svc_config_path": "/home/foo/bar"
}
}

0 comments on commit 72d9ce5

Please sign in to comment.