Class: Vop::Shell

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, input = nil) ⇒ Shell

Returns a new instance of Shell.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vop/shell/shell.rb', line 12

def initialize(op, input = nil)
  @op = op
  @context = {}

  @formatter = ShellFormatter.new

  # override for testing
  if input.nil?
    input = ShellInputReadline.new(method(:tab_completion))
  end
  @input = input

  trap('INT') {
    handle_interrupt
  }

  reset
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/vop/shell/shell.rb', line 9

def context
  @context
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



10
11
12
# File 'lib/vop/shell/shell.rb', line 10

def last_response
  @last_response
end

Class Method Details

.run(op = nil, command_line = nil) ⇒ Object



261
262
263
264
265
266
# File 'lib/vop/shell/shell.rb', line 261

def self.run(op = nil, command_line = nil)
  if op.nil?
    op = Vop.new(origin: "shell:#{Process.pid}@#{`hostname`.strip}")
  end
  self.new(op).do_it(command_line)
end

Instance Method Details

#accept_param(line) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/vop/shell/shell.rb', line 102

def accept_param(line)
  unless line.nil?
    current_param = @missing_params.shift
    $logger.debug "value for param #{current_param.name} : #{line}"
    @arguments[current_param.name] = line

    maybe_execute
  end
end

#complete_command_line(s) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/vop/shell/shell.rb', line 218

def complete_command_line(s)
  potential_command, potential_cmd, potential_args = parse(s)
  #$logger.debug "? >>#{potential_cmd}<< (#{potential_args}) [#{Readline.line_buffer}]"
  if potential_cmd
    default_param = potential_cmd.default_param
    if default_param
      lookups = default_param.lookup(mix_arguments_and_context)
      lookups
        .grep(/^#{Regexp.escape(s.split.last)}/)
        .map do |lookup|
          "#{potential_command} #{lookup}"
        end
    end
  else
    lookups = @op.commands.keys.sort.grep /^#{Regexp.escape(s)}/
  end

end

#complete_for_command(s) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/vop/shell/shell.rb', line 198

def complete_for_command(s)
  current_param = @missing_params.first
  if current_param && current_param.options.has_key?(:lookup)
    begin
      lookup_block = current_param.options[:lookup]

      # the lookup block might want the previously collected params as input
      lookups = if lookup_block&.arity > 0
        params_for_lookup = mix_arguments_and_context
        lookup_block.call(params_for_lookup)
      else
        lookup_block.call()
      end
      lookups.grep /^#{Regexp.escape(s)}/
    rescue => detail
      $logger.error "problem loading lookup values for #{current_param.name} : #{detail.message}"
    end
  end
end

#do_it(command_line = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/vop/shell/shell.rb', line 245

def do_it(command_line = nil)
  if command_line && command_line != ""
    parse_and_execute(command_line)
  else
    while line = @input.read(@prompt)
      if @command
        # if a command has already been selected, ask for missing params
        accept_param(line)
      else
        # otherwise input is treated as regular command line (command + args)
        parse_and_execute(line)
      end
    end
  end
end

#handle_interruptObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/vop/shell/shell.rb', line 42

def handle_interrupt
  if @command
    reset
    puts
    print @prompt
  else
    puts "\n"
    exit
  end
end

#handle_special(command) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vop/shell/shell.rb', line 116

def handle_special(command)
  if command.start_with?('$')
    if command.start_with?('$vop')
      puts "executing #{command}"
      puts eval command
    else
      puts "unknown $-command #{command} - try '$vop' maybe?"
    end
  elsif command.start_with?('@')
    if command.start_with?('@op')
      puts "executing #{command}"
      puts eval command
    else
      puts "unknown @-command #{command} - try '@op' maybe?"
    end
  end
end

#is_special?(command) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/vop/shell/shell.rb', line 112

def is_special?(command)
  command.start_with?('$') || command.start_with?('@') || command.end_with?('?')
end

#maybe_executeObject



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
# File 'lib/vop/shell/shell.rb', line 74

def maybe_execute
  if missing_mandatory_params.size > 0
    @missing_params = missing_mandatory_params
    @prompt = "#{@command.short_name}.#{@missing_params.first.name} ? "
  else
    begin
      request = @op.prepare_request(@command.short_name, @arguments, @context, @command.short_name)
      request.shell = self
      response = @op.execute_request(request)

      # log the last response for the "detail" command
      unless @command.short_name == "detail"
        @last_response = response
      end

      # mix context changes from the response into the local context
      @context.merge! response.context

      display_type = @formatter.analyze(request, response)
      formatted = @formatter.format(request, response, display_type)
      puts formatted
    rescue => detail
      puts "[ERROR] #{detail.message}\n#{detail.backtrace.join("\n")}"
    end
    reset
  end
end

#missing_mandatory_params(command = @command, arguments = @arguments) ⇒ Object



67
68
69
70
71
72
# File 'lib/vop/shell/shell.rb', line 67

def missing_mandatory_params(command = @command, arguments = @arguments)
  command.mandatory_params.delete_if do |param|
    arguments.keys.include?(param.name) ||
    (@context.keys.include?(param.name) && param.wants_context)
  end
end

#mix_arguments_and_context(command = nil, arguments = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vop/shell/shell.rb', line 53

def mix_arguments_and_context(command = nil, arguments = nil)
  result = arguments || @arguments
  @context.each do |k,v|
    cmd = command || @command
    if cmd
      param = (cmd).param(k)
      if param && param.wants_context
        result[k] = @context[k]
      end
    end
  end
  result
end

#parse(command_line) ⇒ Object



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
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vop/shell/shell.rb', line 134

def parse(command_line)
  (command, *args) = command_line.split

  arguments = {}
  if command
    $logger.debug "command : #{command}, args : #{args}"

    if command.end_with?("??")
      target_command = command[0..-3]
      command = "source"
      arguments["name"] = target_command
    elsif command.end_with?("?")
      target_command = command[0..-2]
      command = "help"
      arguments["name"] = target_command
    end

    known_commands = @op.commands.keys
    if known_commands.include? command
      cmd = @op.commands[command]

      unless args.empty? || is_special?(command)
        args.each do |token|
          if token.include? "="
            (key, value) = token.split("=")
            arguments[key] = value
          else
            default_param = cmd.default_param(mix_arguments_and_context)
            if default_param
              arguments[default_param.name] = args
            end
          end
        end
      end
      [command, cmd, arguments]
    else
      [command, nil, arguments]
    end
  end
end

#parse_and_execute(command_line) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/vop/shell/shell.rb', line 175

def parse_and_execute(command_line)
  command, cmd, arguments = parse(command_line)

  if command
    $logger.debug "command : #{command}, args : #{arguments}"
    if is_special?(command)
      handle_special(command_line)
    else
      if command == "exit"
        @input.exit
      else
        if cmd
          @command = cmd
          @arguments = arguments
          maybe_execute
        else
          puts "unknown command '#{command}'"
        end
      end
    end
  end
end

#resetObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/vop/shell/shell.rb', line 31

def reset
  @command = nil
  @arguments = {}

  @prompt = if @context.has_key?("prompt")
    @context["prompt"]
  else
    ">> "
  end
end

#tab_completion(s) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/vop/shell/shell.rb', line 237

def tab_completion(s)
  if @command
    complete_for_command(s)
  else
    complete_command_line(s)
  end
end