Class: Tryouts::Drill::Sergeant::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/drill/sergeant/cli.rb

Overview

CLI

The sergeant responsible for running command-line interface drills.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



19
20
21
22
23
24
# File 'lib/tryouts/drill/sergeant/cli.rb', line 19

def initialize(*args)
  require 'rye'           # Make sure rye is loaded
  @command = args.shift
  @rbox_args = args
  @rbox = Rye::Box.new
end

Instance Attribute Details

#commandObject

The command name to execute rbox.send(@command, *rbox_args)



17
18
19
# File 'lib/tryouts/drill/sergeant/cli.rb', line 17

def command
  @command
end

#rboxObject

Returns the value of attribute rbox.



11
12
13
# File 'lib/tryouts/drill/sergeant/cli.rb', line 11

def rbox
  @rbox
end

#rbox_argsObject

An Array of arguments to be sent to rbox.send(@command, *rbox_args)



14
15
16
# File 'lib/tryouts/drill/sergeant/cli.rb', line 14

def rbox_args
  @rbox_args
end

Instance Method Details

#run(block, context = nil, &inline) ⇒ Object

NOTE: Context is ignored for the CLI Sergeant.



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
# File 'lib/tryouts/drill/sergeant/cli.rb', line 27

def run(block, context=nil, &inline)
  # A Proc object takes precedence over an inline block. 
  runtime = (block.nil? ? inline : block)
  response = Tryouts::Drill::Reality.new
  
  if @command.nil?
    response.command = '[block]'
  else
    response.command = '$ ' << @rbox.preview_command(@command, *@rbox_args)
  end
  
  begin
    if runtime.nil?
      ret = @rbox.send @command, *@rbox_args
      response.output = ret.stdout
      response.ecode = ret.exit_code
      response.error = ret.stderr unless ret.stderr.empty?
    else
      response.output = @rbox.instance_eval &runtime
    end

  rescue Rye::CommandNotFound => ex
    puts ex.message, ex.backtrace if Tryouts.verbose > 2
    response.etype = ex.class
    response.ecode = ex.exit_code
    response.error = "[#{@rbox.host}] Command not found: #{ex.message}"
    response.trace = ex.backtrace
  rescue Rye::CommandError => ex
    puts ex.message, ex.backtrace if Tryouts.verbose > 2
    response.etype = ex.class
    response.ecode = ex.exit_code
    response.output = ex.stdout
    response.error = ex.stderr.join $/
    response.trace = ex.backtrace
  end
  response
end