Class: DTK::Client::Shell::InteractiveWizard

Inherits:
Object
  • Object
show all
Defined in:
lib/shell/interactive_wizard.rb

Defined Under Namespace

Classes: PromptResponse

Constant Summary collapse

PP_LINE_HEAD =
'--------------------------------- DATA ---------------------------------'
PP_LINE =
'------------------------------------------------------------------------'
INVALID_INPUT =
OsUtil.colorize(" Input is not valid. ", :yellow)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInteractiveWizard

Returns a new instance of InteractiveWizard.



30
31
# File 'lib/shell/interactive_wizard.rb', line 30

def initialize
end

Class Method Details

.interactive_user_input(wizard_dsl, recursion_call = false) ⇒ Object

InteractiveWizard.interactive_user_input is generic wizard which will return hash map based on metadata input

Example of form of param ‘wizard_params’

wizard_params = [

{:target_name     => {}},
{:description     => {:optional => true }}
{:iaas_type       => { :type => :selection, :options => [:ec2] }},
{:aws_install    => { :type => :question,
                      :question => "Do we have your permission to add necessery 'key-pair' and 'security-group' to your EC2 account?",
                      :options => ["yes","no"],
                      :required_options => ["yes"],
                      :explanation => "This permission is necessary for creation of a custom target."
                    }}

]



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
77
78
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
113
114
115
116
117
118
119
120
121
# File 'lib/shell/interactive_wizard.rb', line 49

def self.interactive_user_input(wizard_dsl, recursion_call = false)
  results = {}
  wizard_dsl = [wizard_dsl].flatten
  begin
    wizard_dsl.each do |meta_input|
      input_name = meta_input.keys.first
      display_name = input_name.to_s.gsub(/_/,' ').capitalize
       = meta_input.values.first

      # default values
      output = display_name
      validation = [:validation]
      is_password = false
      is_required = [:requried]

      case [:type]
        when nil
        when :email
          validation = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
        when :question
          output = "#{[:question]}"
          validation = [:validation]
        when :password
          is_password = true
          is_required = true
        when :repeat_password
          is_password = true
          is_required = true
          validation  = /^#{results[:password]}$/
        when :selection
          options = ""
          display_field = [:display_field]
          [:options].each_with_index do |o,i|
            if display_field
              options += "\t#{i+1}. #{o[display_field]}\n"
            else
              options += "\t#{i+1}. #{o}\n"
            end
          end
          options += OsUtil.colorize("\t0. Skip\n", :yellow) if [:skip_option]
          output = "Select '#{display_name}': \n\n #{options} \n "
          validation_range_start = [:skip_option] ? 0 : 1
          validation = (validation_range_start..[:options].size).to_a
        when :group
          # recursion call to populate second level of hash params
          puts " Enter '#{display_name}': "
          results[input_name] = self.interactive_user_input([:options], true)
          next
      end

      input = text_input(output, is_required, validation, is_password)

      if [:required_options] && ![:required_options].include?(input)
        # case where we have to give explicit permission, if answer is not affirmative
        # we terminate rest of the wizard
        OsUtil.print(" #{[:explanation]}", :red)
        return nil
      end

      # post processing
      if [:type] == :selection
        input = input.to_i == 0 ? nil : [:options][input.to_i - 1]
      end

      results[input_name] = input
    end

    return results
  rescue Interrupt => e
    puts
    raise DtkValidationError, "Exiting the wizard ..."
  end
end

.prompt_user_for_value(prompt_msg) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/shell/interactive_wizard.rb', line 143

def self.prompt_user_for_value(prompt_msg)
  value = nil
  puts "#{prompt_msg}:"
  begin 
    value = Readline.readline(": ", true)
    response_type = :value_provided
   rescue Interrupt 
    response_type = :skipped
  end
  PromptResponse.new(response_type, value)
end

.resolve_missing_params(param_list, additional_message = nil) ⇒ Object

takes hash maps with description of missing params and returns array of hash map with key, value for each missed param



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/shell/interactive_wizard.rb', line 157

def self.resolve_missing_params(param_list, additional_message = nil)
  begin
    user_provided_params, checkup_hash = [], {}

    puts "\nPlease fill in missing data.\n"
    param_list.each do |param_info|
      description =
        if param_info['display_name'] =~ Regexp.new(param_info['description'])
          param_info['display_name']
        else
          "#{param_info['display_name']} (#{param_info['description']})"
        end
      datatype_info = (param_info['datatype'] ? OsUtil.colorize(" [#{param_info['datatype'].upcase}]", :yellow) : '')
      string_identifier = OsUtil.colorize(description, :green) + datatype_info

      puts "Please enter #{string_identifier}:"
      while line = Readline.readline(": ", true)
        id = param_info['id']
        user_provided_params << {:id => id, :value => line, :display_name => param_info['display_name']}
        checkup_hash[id] = {:value => line, :description => description}
        break
      end

    end

    # pp print for provided parameters
    (checkup_hash)

    # make sure this is satisfactory
    while line = Readline.readline("Is provided information ok? (yes|no) ", true)
      # start all over again
      return resolve_missing_params(param_list) if 'no'.eql? line
      # continue with the code
      break if 'yes'.eql? line
    end

   rescue Interrupt => e
    raise DtkError::InteractiveWizardError, "You have decided to skip correction wizard.#{additional_message}"
  end

  return user_provided_params
end

.text_input(output, is_required = false, validation_regex = nil, is_password = false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/shell/interactive_wizard.rb', line 123

def self.text_input(output, is_required = false, validation_regex = nil, is_password = false)
  while line = ask("#{output}: ") { |q| q.echo = !is_password }
    if is_required && line.strip.empty?
      puts OsUtil.colorize("Field '#{output}' is required field. ", :red)
      next
    end

    if validation_regex && !validation_regex.match(line)
      puts OsUtil.colorize("Input is not valid, please enter it again. ", :red)
      next
    end

    return line
  end
end