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
# File 'lib/terraspace_bundler/exporter/stacks/stack.rb', line 4

def initialize(mod, options={})
  @mod, @options = mod, options
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



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

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



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

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



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

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

#examples_folderObject

public method used by StackConcern#all_stacks



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

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

#exportObject



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

def export
  copy
  rewrite
end

#pretty_path(path) ⇒ Object



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

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)


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

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



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

def rewrite
  Rewrite.new(self).run
end

#srcObject



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

def src
  src = @options[: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