Module: Morpheus::Cli::OptionTypes

Includes:
Term::ANSIColor
Defined in:
lib/morpheus/cli/option_types.rb

Class Method Summary collapse

Class Method Details

.checkbox_prompt(option_type) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/morpheus/cli/option_types.rb', line 164

def self.checkbox_prompt(option_type)
    value_found = false
    value = nil
    while !value_found do
        print "#{option_type['fieldLabel']} (yes/no) [#{option_type['defaultValue'] == 'on' ? 'yes' : 'no'}]: "
        input = $stdin.gets.chomp!
        if input.downcase == 'yes'
          value = 'on'
        elsif input.downcase == 'no'
          value = 'off'
        else
          value = option_type['defaultValue']
        end
        if input == '?'
            help_prompt(option_type)
        elsif !value.nil? || option_type['required'] != true
          value_found = true
        end
    end
    return value
end

.confirm(message, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/morpheus/cli/option_types.rb', line 9

def self.confirm(message,options={})
  if options[:yes] == true
    return true
  end
  value_found = false
  while value_found == false do
    print "#{message} (yes/no): "
    input = $stdin.gets.chomp!
    if input.downcase == 'yes'
      return true
    elsif input.downcase == 'no'
      return false
    else
      puts "Invalid Option... Please try again."
    end
  end
end

.display_select_options(select_options = []) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/morpheus/cli/option_types.rb', line 250

def self.display_select_options(select_options = [])
  puts "\nOptions"
  puts "==============="
  select_options.each do |option|
    puts " * #{option['name']} [#{option['value']}]"
  end
  puts "\n\n"
end

.generic_prompt(option_type) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/morpheus/cli/option_types.rb', line 186

def self.generic_prompt(option_type)
    value_found = false
    value = nil
    while !value_found do
        print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}: "
        input = $stdin.gets.chomp!
        value = input.empty? ? option_type['defaultValue'] : input
        if input == '?'
            help_prompt(option_type)
        elsif !value.nil? || option_type['required'] != true
          value_found = true
        end
    end
    return value
end

.help_prompt(option_type) ⇒ Object



241
242
243
# File 'lib/morpheus/cli/option_types.rb', line 241

def self.help_prompt(option_type)
    print Term::ANSIColor.green,"  * #{option_type['fieldLabel']} [-O #{option_type['fieldName']}=] - ", Term::ANSIColor.reset , "#{option_type['description']}\n"
end

.load_source_options(source, api_client, params) ⇒ Object



246
247
248
# File 'lib/morpheus/cli/option_types.rb', line 246

def self.load_source_options(source,api_client,params)
  api_client.options.options_for_source(source,params)['data']
end

.multiline_prompt(option_type) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/morpheus/cli/option_types.rb', line 202

def self.multiline_prompt(option_type)
    value_found = false
    value = nil
    while !value_found do
        if value.nil?
          print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''} [Type 'EOF' to stop input]: \n"
        end
        input = $stdin.gets.chomp!
        # value = input.empty? ? option_type['defaultValue'] : input
        if input == '?' && value.nil?
            help_prompt(option_type)
        elsif (!value.nil? || option_type['required'] != true) && input.chomp == 'EOF'
          value_found = true
        else
          if value.nil?
            value = ''
          end
          value << input + "\n"
        end
    end
    return value
end

.number_prompt(option_type) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/morpheus/cli/option_types.rb', line 114

def self.number_prompt(option_type)
    value_found = false
    value = nil
    while !value_found do
        print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}: "
        input = $stdin.gets.chomp!
        value = input.empty? ? option_type['defaultValue'] : input.to_i
        if input == '?'
            help_prompt(option_type)
        elsif !value.nil? || option_type['required'] != true
          value_found = true
        end
    end
    return value
end

.password_prompt(option_type) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/morpheus/cli/option_types.rb', line 225

def self.password_prompt(option_type)
    value_found = false
    while !value_found do
        print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''}: "
        input = STDIN.noecho(&:gets).chomp!
        value = input
        print "\n"
        if input == '?'
            help_prompt(option_type)
        elsif !value.nil? || option_type['required'] != true
          value_found = true
        end
    end
    return value
end

.prompt(option_types, options = {}, api_client = nil, api_params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/morpheus/cli/option_types.rb', line 27

def self.prompt(option_types, options={}, api_client=nil,api_params={})
  results = {}
  options = options || {}
  # puts "Options Prompt #{options}"
  option_types.sort { |x,y| x['displayOrder'].to_i <=> y['displayOrder'].to_i }.each do |option_type|
    context_map = results
    value = nil
    value_found=false
    if option_type['fieldContext']
      results[option_type['fieldContext']] ||= {}
      context_map = results[option_type['fieldContext']]
      if options[option_type['fieldContext']] and options[option_type['fieldContext']].key?(option_type['fieldLabel'])
        value = options[option_type['fieldContext']][option_type['fieldLabel']]
        value_found = true
      end
    end

    if value_found == false && options.key?(option_type['fieldName'])
      value = options[option_type['fieldName']]
      value_found = true
    end

    if !value_found
      if option_type['type'] == 'number'
        value = number_prompt(option_type)
      elsif option_type['type'] == 'password'
       value = password_prompt(option_type)
      elsif option_type['type'] == 'checkbox'
        value = checkbox_prompt(option_type)
      elsif option_type['type'] == 'radio'
        value = radio_prompt(option_type)
      elsif option_type['type'] == 'textarea'
        value = multiline_prompt(option_type)
      elsif option_type['type'] == 'code-editor'
        value = multiline_prompt(option_type)  
      elsif option_type['type'] == 'select'
        value = select_prompt(option_type,api_client, api_params)
      elsif option_type['type'] == 'hidden'
        value = option_type['defaultValue']
        input = value
      else
        value = generic_prompt(option_type)
      end
      
    end
    context_map[option_type['fieldName']] = value
  end

  return results
end

.radio_prompt(option_type) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/morpheus/cli/option_types.rb', line 79

def self.radio_prompt(option_type)
    value_found = false
    value = nil
    options = []
    if option_type['config'] and option_type['config']['radioOptions']
        option_type['config']['radioOptions'].each do |radio_option|
            options << {key: radio_option['key'], checked: radio_option['checked']}
        end
    end
    optionString = options.collect{ |b| b[:checked] ? "(#{b[:key]})" : b[:key]}.join(', ')
    while !value_found do
        print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }[#{optionString}]: "
        input = $stdin.gets.chomp!
        if input == '?'
            help_prompt(option_type)
        else
            if input.nil? || input.empty?
                selectedOption = options.find{|o| o[:checked] == true}
            else
                selectedOption = options.find{|o| o[:key].downcase == input.downcase}   
            end
            
            if selectedOption
                value = selectedOption[:key]
            else
                puts "Invalid Option. Please select from #{optionString}."
            end
            if !value.nil? || option_type['required'] != true
              value_found = true
            end
        end
    end
    return value
end

.select_prompt(option_type, api_client, api_params = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/morpheus/cli/option_types.rb', line 130

def self.select_prompt(option_type,api_client, api_params={})
  value_found = false
  value = nil
  if option_type['optionSource']
    source_options = load_source_options(option_type['optionSource'],api_client,api_params)
  end
  if !source_options.nil? && !source_options.count == 1 && option_type['skipSingleOption'] == true
    value_found = true
    value = source_option['value']
  end
  while !value_found do
      print "#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''} ['?' for options]: "
      input = $stdin.gets.chomp!
      if option_type['optionSource']
        source_option = source_options.find{|b| b['name'] == input || (!b['value'].nil? && b['value'].to_s == input) || (b['value'].nil? && input.empty?)}
        if source_option
          value = source_option['value']
        elsif !input.nil?  && !input.empty?
          input = '?'
        end
      else
        value = input.empty? ? option_type['defaultValue'] : input
      end
      
      if input == '?'
          help_prompt(option_type)
          display_select_options(source_options)
      elsif !value.nil? || option_type['required'] != true
        value_found = true
      end
  end
  return value
end