Class: Lono::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Importer

Returns a new instance of Importer.



7
8
9
10
11
# File 'lib/lono/importer.rb', line 7

def initialize(source, options)
  @source = source
  @options = options
  @format = normalize_format(@options[:format])
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/lono/importer.rb', line 6

def options
  @options
end

Instance Method Details

#add_template_definitionObject

Add template definition to config/templates/base/stacks.rb.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lono/importer.rb', line 40

def add_template_definition
  path = "#{Lono.root}/config/templates/base/stacks.rb"
  lines = File.exist?(path) ? IO.readlines(path) : []
  new_template_definition = %Q|template "#{template_name}"|
  unless lines.detect { |l| l.include?(new_template_definition) }
    lines << ["\n", new_template_definition]
    result = lines.join('')
    IO.write(path, result)
  end
  path
end

#create_paramsObject

Creates starter params/base/.txt file



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lono/importer.rb', line 53

def create_params
  template = if @format == 'yml'
              YAML.load_file(template_path)
            else
              JSON.load(IO.read(template_path))
            end

  result = []
  required_parameters.each do |name, attributes|
    result << "#{name}="
  end
  optional_parameters.each do |name, attributes|
    key = "#{name}=".ljust(20, ' ')
    result << "##{key} # optional"
  end
  content = result.join("\n") + "\n"

  folder = File.dirname(params_path)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  IO.write(params_path, content) unless File.exist?(params_path)
end

#download_templateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lono/importer.rb', line 25

def download_template
  template =  open(@source).read

  result = if @format == 'yml'
              YAML.dump(YAML.load(template))
            else
              JSON.pretty_generate(JSON.load(template))
            end

  folder = File.dirname(template_path)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  IO.write(template_path, result)
end

#params_pathObject



75
76
77
# File 'lib/lono/importer.rb', line 75

def params_path
  "#{Lono.root}/params/base/#{template_name}.txt"
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lono/importer.rb', line 13

def run
  unless options[:noop]
    download_template
    template_definition_path = add_template_definition
    create_params
    puts "Imported raw CloudFormation template and lono-fied it!"
    puts "Template definition added to #{template_definition_path}."
    puts "Params file created to #{params_path}."
  end
  puts "Template downloaded to #{template_path}." # like having this message at the end
end

#template_nameObject



84
85
86
87
88
89
# File 'lib/lono/importer.rb', line 84

def template_name
  return @options[:name] if @options[:name]
  # else infer name from the original source
  name = File.basename(@source, ".*")
  @options[:casing] == "camelcase" ? name.camelize : name.underscore.dasherize
end

#template_pathObject



80
81
82
# File 'lib/lono/importer.rb', line 80

def template_path
  "#{Lono.root}/templates/#{template_name}.#{@format}"
end