Class: Kontena::Cli::Stacks::YAML::Prompt

Inherits:
Opto::Resolver
  • Object
show all
Includes:
Common
Defined in:
lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb

Instance Method Summary collapse

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Instance Method Details

#askObject



62
63
64
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 62

def ask
  prompt.ask(question_text, default: option.default, echo: echo?)
end

#boolObject



51
52
53
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 51

def bool
  prompt.yes?(question_text, default: option.default.nil? || option.default)
end

#boolean?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 14

def boolean?
  option.type == 'boolean'
end

#echo?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 55

def echo?
  return true if option.handler.nil?
  return true if option.handler.options.nil?
  return true if option.handler.options[:echo].nil?
  option.handler.options[:echo]
end

#enumObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 32

def enum
  opts = option.handler.options[:options]
  opts << { label: '(Other)', value: nil, description: '(Other)' } if enum_can_be_other?

  answer = prompt.select(question_text) do |menu|
    menu.enum ':' # makes it show numbers before values, you can press the number to select.
    menu.default(opts.index {|opt| opt[:value] == option.default }.to_i + 1) if option.default
    opts.each do |opt|
      menu.choice opt[:label], opt[:value]
    end
  end

  if answer.nil? && enum_can_be_other?
    ask
  else
    answer
  end
end

#enum?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 10

def enum?
  option.type == 'enum'
end

#enum_can_be_other?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 28

def enum_can_be_other?
  enum? && option.handler.options[:can_be_other] ? true : false
end

#prompt_wordObject



18
19
20
21
22
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 18

def prompt_word
  return "Select" if enum?
  return "Enable" if boolean?
  "Enter"
end

#question_textObject



24
25
26
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 24

def question_text
  (!hint.nil? && hint != option.name) ? "#{hint} :" : "#{prompt_word} #{option.label || option.name} :"
end

#resolveObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb', line 66

def resolve
  return nil if option.skip?
  if enum?
    enum
  elsif boolean?
    bool
  else
    ask
  end
end