Module: Minfra::Cli::Ask

Defined in:
lib/minfra/cli/ask.rb

Class Method Summary collapse

Class Method Details

.boolean(question) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/minfra/cli/ask.rb', line 6

def self.boolean(question)
  answered = false
  until answered
    $stdout.write "#{question} (y/n)"
    answer = $stdin.gets.chomp
    if %w[y n].include?(answer)
      answered = true
    else
      $stdout.write("I just understand 'y' and 'n', again: ")
    end
  end
  answer == 'y'
end

.interactive_template(template_file, out_file, placeholders = {}) ⇒ Object



39
40
41
42
43
# File 'lib/minfra/cli/ask.rb', line 39

def self.interactive_template(template_file, out_file, placeholders = {})
  templater = Templater.new(File.read(template_file))
  params = self.placeholders(templater, placeholders)
  File.write(out_file, templater.render(params))
end

.placeholders(templater, placeholders = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/minfra/cli/ask.rb', line 32

def self.placeholders(templater, placeholders = {})
  templater.check_missing do |name|
    placeholders[name] = text("I need a value for: #{name}") unless placeholders[name]
  end
  placeholders
end

.text(question, default = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/minfra/cli/ask.rb', line 20

def self.text(question, default = nil)
  answer = nil
  loop do
    message = format('%s%s: ', question, default && " (#{default})")
    $stdout.write message
    answer = $stdin.gets.chomp
    answer = default if answer == ''
    break unless answer.nil?
  end
  answer
end