class Terraspace::Seeder

Usage:

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

Public Class Methods

new(mod, options={}) click to toggle source
# File lib/terraspace/seeder.rb, line 8
def initialize(mod, options={})
  @mod, @options = mod, options
end

Public Instance Methods

actions() click to toggle source
# File lib/terraspace/seeder.rb, line 61
def actions
  Actions.new(@options)
end
dest_path() click to toggle source
# File lib/terraspace/seeder.rb, line 45
def dest_path
  Where.new(@mod, @options).dest_path
end
exist?(file) click to toggle source
# File lib/terraspace/seeder.rb, line 50
def exist?(file)
  path = "#{@mod.cache_dir}/#{file}"
  File.exist?(path)
end
load_hcl_variables() click to toggle source
# 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
parse() click to toggle source
# 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) click to toggle source
# 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
seed() click to toggle source
# 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) click to toggle source
# File lib/terraspace/seeder.rb, line 41
def write(content)
  actions.create_file(dest_path, content)
end