Class: TerraspaceBundler::Exporter::Stacks::Stack

Inherits:
Base
  • Object
show all
Defined in:
lib/terraspace_bundler/exporter/stacks/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, options = {}) ⇒ Stack

Returns a new instance of Stack.



4
5
6
7
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 4

def initialize(mod, options={})
  @mod, @options = mod, options
  @src = @options[:src] || @options[:example] || @options[:name]
end

Instance Attribute Details

#modObject (readonly)

Returns the value of attribute mod.



3
4
5
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 3

def mod
  @mod
end

Instance Method Details

#copyObject



14
15
16
17
18
19
20
21
22
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 14

def copy
  return unless @options

  FileUtils.rm_rf(dest) if purge?
  return if File.exist?(dest)

  FileUtils.mkdir_p(File.dirname(dest))
  FileUtils.cp_r(src, dest)
end

#destObject



59
60
61
62
63
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 59

def dest
  dest = @options[:dest] || TB.config.stack_options[:dest]
  name = @options[:name] || @mod.name # falls back to mod name by default
  "#{dest}/#{name}"
end

#examplesObject



55
56
57
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 55

def examples
  @options[:examples] || TB.config.stack_options[:examples]
end

#examples_folderObject

public method used by StackConcern#all_stacks



51
52
53
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 51

def examples_folder
  [mod_path, examples].join('/')
end

#exportObject



9
10
11
12
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 9

def export
  copy
  rewrite
end

#pretty_path(path) ⇒ Object



46
47
48
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 46

def pretty_path(path)
  path.sub("#{Dir.pwd}/",'')
end

#purge?Boolean

purge precedence:

1. Terrafile mod level stack option
2. Terrafile-level stack_options

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 70

def purge?
  # config.stack_options is set from Terrafile-level stack_options to TB.config.stack
  # relevant source code: dsl/syntax.rb: def stack_options
  config = TB.config.stack_options[:purge]
  config = config.nil? ? false : config
  @options[:purge].nil? ? config : @options[:purge]
end

#rewriteObject



24
25
26
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 24

def rewrite
  Rewrite.new(self).run
end

#srcObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 28

def src
  src = @src
  without_examples = [mod_path, src].compact.join('/')
  with_examples = [examples_folder, src].compact.join('/')
  paths = [with_examples, without_examples]
  found = paths.find do |path|
    File.exist?(path)
  end

  unless found
    searched = paths.map { |p| pretty_path(p) }.map { |p| "    #{p}" }.join("\n")
    logger.error "ERROR: Example not found. stack src: #{src}. Searched:".color(:red)
    logger.error searched
    exit 1
  end
  found
end