Class: Terraspace::Seeder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util
Defined in:
lib/terraspace/seeder/actions.rb,
lib/terraspace/seeder.rb,
lib/terraspace/seeder/where.rb,
lib/terraspace/seeder/content.rb

Overview

Usage:

@actions = Actions.new
@actions.create_file("content", "/path/to/file.txt")

Defined Under Namespace

Classes: Actions, Content, Where

Instance Method Summary collapse

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Constructor Details

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

Returns a new instance of Seeder.



8
9
10
# File 'lib/terraspace/seeder.rb', line 8

def initialize(mod, options={})
  @mod, @options = mod, options
end

Instance Method Details

#actionsObject



61
62
63
# File 'lib/terraspace/seeder.rb', line 61

def actions
  Actions.new(@options)
end

#dest_pathObject



45
46
47
# File 'lib/terraspace/seeder.rb', line 45

def dest_path
  Where.new(@mod, @options).dest_path
end

#exist?(file) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/terraspace/seeder.rb', line 50

def exist?(file)
  path = "#{@mod.cache_dir}/#{file}"
  File.exist?(path)
end

#load_hcl_variablesObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/terraspace/seeder.rb', line 30

def load_hcl_variables
  HclParser.load(read("variables.tf"))
rescue Racc::ParseError => e
  logger.error "ERROR: Unable to parse the #{Util.pretty_path(@mod.cache_dir)}/variables.tf file".color(:red)
  logger.error "and generate the starter tfvars file. This is probably due to a complex variable type."
  logger.error "#{e.class}: #{e.message}"
  puts
  logger.error "You will have to create the tfvars file manually at: #{Util.pretty_path(dest_path)}"
  exit 1
end

#parseObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/terraspace/seeder.rb', line 18

def parse
  if exist?("variables.tf")
    load_hcl_variables
  elsif exist?("variables.tf.json")
    JSON.load(read("variables.tf.json"))
  else
    logger.warn "WARN: no variables.tf or variables.tf.json found in: #{@mod.cache_dir}"
    ENV['TS_TEST'] ? raise : exit
  end
end

#read(file) ⇒ Object



55
56
57
58
59
# File 'lib/terraspace/seeder.rb', line 55

def read(file)
  path = "#{@mod.cache_dir}/#{file}"
  logger.info "Reading: #{Util.pretty_path(path)}"
  IO.read(path)
end

#seedObject



12
13
14
15
16
# File 'lib/terraspace/seeder.rb', line 12

def seed
  parsed = parse # make @parsed available for rest of processing
  content = Content.new(parsed).build
  write(content)
end

#write(content) ⇒ Object



41
42
43
# File 'lib/terraspace/seeder.rb', line 41

def write(content)
  actions.create_file(dest_path, content)
end