Class: ShellCommand

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_fct, args: [], requires_sudo: false, exit_on_fail: true) ⇒ ShellCommand



79
80
81
82
83
84
# File 'lib/labdb_manager.rb', line 79

def initialize(cmd_fct, args: [], requires_sudo: false, exit_on_fail: true)
  @cmd_fct = cmd_fct
  @requires_sudo = requires_sudo
  @exit_on_fail = exit_on_fail
  @args = args
end

Class Method Details

.prepend_login_shell(cmd) ⇒ Object

A command that gets run in a login shell.



75
76
77
# File 'lib/labdb_manager.rb', line 75

def self.(cmd)
  "/bin/bash --login -c #{cmd}"
end

Instance Method Details

#call(dry_run: false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/labdb_manager.rb', line 86

def call(dry_run: false)
  # Run the command.
  command_string = @cmd_fct.call *@args
  puts (COMMAND_PREFIX + command_string).green
  unless dry_run then
    `#{ShellCommand. command_string}`
    if $?.exitstatus > 0 and @exit_on_fail
      puts ("Encountered an unresolvable error while running #{command_string}.  " +
            "Please resolve the problem manually and re-run").red
      exit($?.exitstatus)
    end
  end
  $?.exitstatus
end

#to_sObject



101
102
103
# File 'lib/labdb_manager.rb', line 101

def to_s
  @cmd_fct.call
end