Class: Kumogata2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kumogata/template/ext/kumogata.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_template_path(file = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/kumogata/template/ext/kumogata.rb', line 33

def self.get_template_path(file = nil)
  template_path = File.expand_path(File.join(File.dirname(__FILE__),
                                             '..', '..', '..','..', 'template'))
  template_path = File.join(template_path, "#{file}.rb") unless file.nil?
  template_path
end

Instance Method Details

#init(stack_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kumogata/template/ext/kumogata.rb', line 7

def init(stack_name)
  begin
    base_template = ''
    File.open(Kumogata2::Client::get_template_path('_template'), 'r'){|f|
      base_template = f.read
    }
    raise 'initialize template is empty' if base_template.empty?

    new_template = "#{stack_name}.rb"
    if File.exists? new_template
      print "#{new_template} is already exists. Are sure want to overwrite? [y/N]: "
      answer = STDIN.gets.to_s.chomp
      return nil if answer.upcase != 'Y'
    end

    File.open(new_template, 'w'){|f|
      template = base_template.gsub('#{NAME}', stack_name)
      f.write(template)
    }
    Kumogata2::Logger::Helper.log(:info, "Saved template to #{stack_name}.rb".green)
  rescue => e
    Kumogata2::Logger::Helper.log(:error, "Failed to template #{stack_name} - #{e}".red)
  end
  nil
end