Class: Terraspace::Terraform::RemoteState::Marker::PrettyTracer

Inherits:
Object
  • Object
show all
Defined in:
lib/terraspace/terraform/remote_state/marker/pretty_tracer.rb

Instance Method Summary collapse

Constructor Details

#initialize(caller_line) ⇒ PrettyTracer

Returns a new instance of PrettyTracer.



3
4
5
# File 'lib/terraspace/terraform/remote_state/marker/pretty_tracer.rb', line 3

def initialize(caller_line)
  @caller_line = caller_line
end

Instance Method Details

#pretty_trace(path, error_line_number) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/terraspace/terraform/remote_state/marker/pretty_tracer.rb', line 14

def pretty_trace(path, error_line_number)
  io = StringIO.new
  context = 5 # lines of context
  top, bottom = [error_line_number-context-1, 0].max, error_line_number+context-1

  io.puts "Here's the line in #{Terraspace::Util.pretty_path(path)} with the error:\n\n"

  lines = IO.read(path).split("\n")
  context = 5 # lines of context
  top, bottom = [error_line_number-context-1, 0].max, error_line_number+context-1
  spacing = lines.size.to_s.size
  lines[top..bottom].each_with_index do |line_content, index|
    line_number = top+index+1
    if line_number == error_line_number
      io.printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
    else
      io.printf("%#{spacing}d %s\n", line_number, line_content)
    end
  end

  io.string
end

#source_codeObject

/full/path/to/app/stacks/a1/tfvars/dev.tfvars:4:in ‘__tilt_5560’



8
9
10
11
12
# File 'lib/terraspace/terraform/remote_state/marker/pretty_tracer.rb', line 8

def source_code
  line = @caller_line.sub(/:in `.*/,'')
  path, error_line_number = line.split(':')
  pretty_trace(path, error_line_number.to_i)
end