Skip to content

Commit

Permalink
Merge pull request #262 from boltops-tools/graph-tfvars
Browse files Browse the repository at this point in the history
improve graph dependency processing
  • Loading branch information
tongueroo authored Sep 1, 2022
2 parents 9c6bf74 + c97ce89 commit 1ebbbd1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/terraspace/compiler/erb/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ def initialize(mod, src_path)

def build
context = Context.new(@mod)
RenderMePretty.result(@src_path, context: context)
if @mod.resolved
RenderMePretty.result(@src_path, context: context)
else
# Replace contents so only the `output` and `depends_on` are evaluated
temp_path = Rewrite.new(@src_path).rewrite
RenderMePretty.result(temp_path, context: context)
end
end
end
end
42 changes: 42 additions & 0 deletions lib/terraspace/compiler/erb/rewrite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Terraspace::Compiler::Erb
class Rewrite
def initialize(src_path)
@src_path = src_path
end

def rewrite
input = IO.read(@src_path)
output = replace(input)
tfvar_path = @src_path.sub(Terraspace.root,'')
temp_path = "/tmp/terraspace/rewrite#{tfvar_path}"
FileUtils.mkdir_p(File.dirname(temp_path))
IO.write(temp_path, output)
temp_path
end

# Replace contents so only the `output` and `depends_on` are evaluated
def replace(input)
lines = input.split("\n").map {|l| l+"\n"} # mimic IO.readlines
new_lines = lines.map do |line|
new_line(line)
end
new_lines.join('')
end

def new_line(line)
md = line.match(/.*(<% |<%=)/) || line.match(/.*<%$/)
if md
words = %w[output depends_on] # TODO: consider allowing user customizations
# IE: <%= output or <% depends_on
regexp = Regexp.new(".*<%.*#{words.join('|')}.*")
if line.match(regexp)
line # passthrough
else
line.sub('<%', '<%#') # replace with ERB opening comment
end
else
line # passthrough
end
end
end
end
2 changes: 1 addition & 1 deletion spec/terraspace/compiler/erb/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it "build" do
allow(Terraspace::Terraform::RemoteState::Marker::Output).to receive(:stack_names).and_return("b1")
result = render.build
expect(result).to eq "length = (unresolved)"
expect(result).to eq "length = (unresolved)\n"
end
end
end
30 changes: 30 additions & 0 deletions spec/terraspace/compiler/erb/rewrite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe Terraspace::Compiler::Erb::Rewrite do
let(:rewrite) { described_class.new(src_path) }

context "has output" do
let(:src_path) { fixture("rewrite/dev.tfvars") }
it "replace" do
input =<<~EOL
length = <%= output('b1.length') %>
foo = <%= foo %>
<% depends_on "b1" %>
<%
3.times do |i|
puts i
end
%>
EOL
text = rewrite.replace(input)
expect(text).to eq <<~EOL
length = <%= output('b1.length') %>
foo = <%#= foo %>
<% depends_on "b1" %>
<%#
3.times do |i|
puts i
end
%>
EOL
end
end
end

0 comments on commit 1ebbbd1

Please sign in to comment.