Skip to content

Commit

Permalink
Enable defining vars at the env block level (#43)
Browse files Browse the repository at this point in the history
- vars can still be defined/overridden on the template level (add this to the readme to clarify)
  • Loading branch information
aharpervc authored Oct 3, 2023
1 parent cbd5460 commit b4190b2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/consult.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/consult/template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'template_functions'
require_relative '../support/hash_extensions'

module Consult
class Template
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/support/config/consult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ shared:
aziz: 'Light!'

test:
vars:
test_env_override: some value

templates:
secrets:
path: templates/secrets.yml.erb
Expand Down
2 changes: 2 additions & 0 deletions spec/support/templates/database.yml.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# rendered at <%= timestamp %>
# additional var: <%= vars.dig 'test_env_override' %>

common: &common
appname: Rails
adapter: postgres
Expand Down

0 comments on commit b4190b2

Please sign in to comment.