Class: TerraspaceBundler::Exporter::Stacks::Rewrite
- Defined in:
- lib/terraspace_bundler/exporter/stacks/rewrite.rb
Instance Method Summary collapse
-
#initialize(stack) ⇒ Rewrite
constructor
A new instance of Rewrite.
- #new_line(line) ⇒ Object
- #new_lines(lines) ⇒ Object
- #replace(path) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(stack) ⇒ Rewrite
Returns a new instance of Rewrite.
3 4 5 |
# File 'lib/terraspace_bundler/exporter/stacks/rewrite.rb', line 3 def initialize(stack) @stack = stack end |
Instance Method Details
#new_line(line) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/terraspace_bundler/exporter/stacks/rewrite.rb', line 30 def new_line(line) # marker comment so files dont get updated twice when purge is false terraspace_comment = "# updated by terraspace" return line unless line.include?("../") return line if line.include?(terraspace_comment) md = line.match(/^\s*source\s*=\s*"(.*)?"/) unless md logger.error "ERROR: unable to find the module source".color(:red) logger.error line exit 1 end # Lots of cases covered by specs # # ../.. # ../../ # ../../modules/iam-user # ../../../modules/compute_instance # source_value = md[1] # IE: ""../../../modules/compute_instance" dirs = source_value.split('/') count = dirs.count('..') mod_dir = dirs[count..-1].join('/') full_dir = [@stack.mod.name, mod_dir].reject(&:empty?).join('/') %Q| source = "../../modules/#{full_dir}" #{terraspace_comment}\n| end |
#new_lines(lines) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/terraspace_bundler/exporter/stacks/rewrite.rb', line 22 def new_lines(lines) lines.map do |line| # leading spaces or no space. but cannot have any other chars # can have space betweens source and = line =~ /^\s*source\s*=/ ? new_line(line) : line end end |
#replace(path) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/terraspace_bundler/exporter/stacks/rewrite.rb', line 15 def replace(path) lines = IO.readlines(path) new_lines = new_lines(lines) text = new_lines.join('') IO.write(path, text) end |
#run ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/terraspace_bundler/exporter/stacks/rewrite.rb', line 7 def run expr = "#{@stack.dest}/*.tf" Dir.glob(expr).each do |path| next unless File.file?(path) # skip symlinks and dirs replace(path) end end |