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

  # TODO 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.



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

def context
  @context
end

Class Method Details

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



214
215
216
217
218
219
# File 'lib/vop/shell/shell.rb', line 214

def self.run(op = nil, command_line = nil)
  if op.nil?
    op = Vop.new
  end
  self.new(op).do_it(command_line)
end

Instance Method Details

#accept_param(line) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/vop/shell/shell.rb', line 97

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

  maybe_execute
end

#do_it(command_line = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/vop/shell/shell.rb', line 194

def do_it(command_line = nil)
  #Readline.completion_append_character = ""
  #Readline.completion_proc = method(:tab_completion)

  if command_line
    parse_and_execute(command_line)
  else
    while line = @input.read(@prompt)
    #while line = Readline.readline(@prompt, true)
      if @command
        # if a command has already been selected, we 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 "\nbye"
    exit
  end
end

#maybe_executeObject



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

def maybe_execute
  mandatory = @command.mandatory_params

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

  if missing_mandatory_params.size > 0
    $logger.debug "missing params : #{missing_mandatory_params.map(&:name)}"
  end

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

      @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

#mix_arguments_and_contextObject



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

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

#parse_and_execute(command_line) ⇒ Object



123
124
125
126
127
128
129
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
163
164
165
# File 'lib/vop/shell/shell.rb', line 123

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

  if command
    $logger.debug "command : #{command}, args : #{args}"
    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
    else
      if command.end_with?("?")
        help_command = command[0..-2]
        command = "help"
        args << "name=#{help_command}"
      end

      if command == "exit"
        @input.exit
      else
        known_commands = @op.commands.keys
        if known_commands.include? command
          @command = @op.commands[command]
          @arguments = parse_command_line(args)

          maybe_execute
        else
          puts "unknown command '#{command}'"
        end
      end
    end
  end

end

#parse_command_line(args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vop/shell/shell.rb', line 105

def parse_command_line(args)
  result = {}
  unless args.empty?
    args.each do |token|
      if token.include? "="
        (key, value) = token.split("=")
        result[key] = value
      else
        default_param = @command.default_param
        if default_param
          result[default_param.name] = args
        end
      end
    end
  end
  result
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



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

def tab_completion(s)
  lookups = []

  if @command
    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
      rescue => detail
        $logger.error "problem loading lookup values for #{current_param.name} : #{detail.message}"
      end
    end
  else
    lookups = @op.commands.keys.sort
  end

  lookups.grep /^#{Regexp.escape(s)}/
end