diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a4287..14229a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Add `--version` option to show the version * Better logging for template rendering status, errors, and config (verbose mode) * Development: adjust require's to be able to run CLI from any folder +* Enable defining vars at the env block level, rather than only on template blocks #### 0.11.0 diff --git a/README.md b/README.md index 148cccf..3b31c0f 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,15 @@ test: secrets: path: config/templates/secrets.yml dest: config/secrets.yml + # vars can be defined on a per-template basis + vars: + test_specific_key: and_the_value production: + # vars can be defined at the environment level, which are available to these templates + vars: + hello: world + templates: # You can concatenate multiple files together my_config: diff --git a/lib/consult.rb b/lib/consult.rb index d4df181..7c46176 100644 --- a/lib/consult.rb +++ b/lib/consult.rb @@ -41,7 +41,7 @@ def load(config_dir: nil, force_render: false, verbose: nil) @all_config ||= {} @config = @all_config[:shared].to_h.deep_merge @all_config[env&.to_sym].to_h - @templates = @config[:templates]&.map { |name, config| Template.new(name, config.merge(verbose: verbose)) } || [] + @templates = @config[:templates]&.map { |name, config| Template.new(name, config.merge({verbose: verbose, env_vars: @config[:vars]})) } || [] @force_render = force_render diff --git a/lib/consult/template.rb b/lib/consult/template.rb index 90ecdfb..c0c38c3 100644 --- a/lib/consult/template.rb +++ b/lib/consult/template.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'template_functions' +require_relative '../support/hash_extensions' module Consult class Template @@ -40,7 +41,7 @@ def paths end def vars - @config[:vars] + @config[:env_vars].to_h.deep_merge @config[:vars].to_h end def dest diff --git a/spec/lib/template_spec.rb b/spec/lib/template_spec.rb index 3fd0d41..f71207c 100644 --- a/spec/lib/template_spec.rb +++ b/spec/lib/template_spec.rb @@ -95,6 +95,34 @@ expect(template.key('infrastructure/db1/dns')).to eq 'db1.local.net' end + describe '#vars' do + let(:env_vars) do + { + env_vars: { + 'test_env_override' => 'some value from env vars', + }, + } + end + + let(:env_vars_and_template_vars) do + env_vars.merge({ + vars: { + 'test_var_override' => 'some value from template vars', + }, + }) + end + + it 'can read vars from environment block' do + config.merge! env_vars + expect(template.vars['test_env_override']).to eq 'some value from env vars' + end + + it 'can read vars from vars block' do + config.merge! env_vars_and_template_vars + expect(template.vars['test_var_override']).to eq 'some value from template vars' + end + end + it '#with' do expect { |b| template.with(0, &b) }.to yield_control end diff --git a/spec/support/config/consult.yml b/spec/support/config/consult.yml index b7cd067..79e19fa 100644 --- a/spec/support/config/consult.yml +++ b/spec/support/config/consult.yml @@ -83,6 +83,9 @@ shared: aziz: 'Light!' test: + vars: + test_env_override: some value + templates: secrets: path: templates/secrets.yml.erb diff --git a/spec/support/templates/database.yml.erb b/spec/support/templates/database.yml.erb index 8ecbdb1..ded542e 100644 --- a/spec/support/templates/database.yml.erb +++ b/spec/support/templates/database.yml.erb @@ -1,4 +1,6 @@ # rendered at <%= timestamp %> +# additional var: <%= vars.dig 'test_env_override' %> + common: &common appname: Rails adapter: postgres