Class: Bookingit::ShellCommand

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/bookingit/shell_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: nil, path: '.', expected_exit_status: 0, &block) ⇒ ShellCommand

Returns a new instance of ShellCommand.



10
11
12
13
14
15
16
17
18
# File 'lib/bookingit/shell_command.rb', line 10

def initialize(command: nil,path: '.',expected_exit_status: 0, &block)
  @command = command
  @path = path
  @exit_status_checker = if block.nil?
                           ->(exit_code) { exit_code == expected_exit_status }
                         else
                           block
                         end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/bookingit/shell_command.rb', line 8

def command
  @command
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



8
9
10
# File 'lib/bookingit/shell_command.rb', line 8

def exit_code
  @exit_code
end

#expected_exit_statusObject (readonly)

Returns the value of attribute expected_exit_status.



8
9
10
# File 'lib/bookingit/shell_command.rb', line 8

def expected_exit_status
  @expected_exit_status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



8
9
10
# File 'lib/bookingit/shell_command.rb', line 8

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



8
9
10
# File 'lib/bookingit/shell_command.rb', line 8

def stdout
  @stdout
end

Instance Method Details

#run!Object



20
21
22
23
24
25
26
27
28
# File 'lib/bookingit/shell_command.rb', line 20

def run!
  chdir @path do
    @stdout, @stderr, status = Open3.capture3(@command)
    @exit_code = status.exitstatus
  end
  unless @exit_status_checker.(@exit_code)
    raise UnexpectedShellCommandExit.new(@command,@stdout,@stderr)
  end
end