Class: Shred::Commands::ShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/shred/commands/base.rb

Defined Under Namespace

Classes: CommandLine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_lines: nil, success_msg: nil, error_msg: nil, output: nil) ⇒ ShellCommand

Returns a new instance of ShellCommand.

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
# File 'lib/shred/commands/base.rb', line 61

def initialize(command_lines: nil, success_msg: nil, error_msg: nil, output: nil)
  @command_lines = Array(command_lines).compact
  raise ArgumentError, "At least one command line is required" if command_lines.empty?
  @success_msg = success_msg
  @error_msg = error_msg
  @output = output
  @out = File.open(output, 'w') if output
end

Instance Attribute Details

#command_linesObject (readonly)

Returns the value of attribute command_lines.



59
60
61
# File 'lib/shred/commands/base.rb', line 59

def command_lines
  @command_lines
end

#error_msgObject (readonly)

Returns the value of attribute error_msg.



59
60
61
# File 'lib/shred/commands/base.rb', line 59

def error_msg
  @error_msg
end

#outObject (readonly)

Returns the value of attribute out.



59
60
61
# File 'lib/shred/commands/base.rb', line 59

def out
  @out
end

#outputObject (readonly)

Returns the value of attribute output.



59
60
61
# File 'lib/shred/commands/base.rb', line 59

def output
  @output
end

#success_msgObject (readonly)

Returns the value of attribute success_msg.



59
60
61
# File 'lib/shred/commands/base.rb', line 59

def success_msg
  @success_msg
end

Instance Method Details

#run(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shred/commands/base.rb', line 70

def run(&block)
  exit_status = nil
  command_lines.each_with_index do |command_line, i|
    command_line = CommandLine.new(command_line: command_line, out: out)
    exit_status = if block_given?
      yield(command_line)
    else
      command_line.run
    end
    break unless exit_status.success?
  end
  exit_status
ensure
  out.close if out
end