Module: Scripto::RunCommands

Included in:
Scripto, Main
Defined in:
lib/scripto/run_commands.rb

Defined Under Namespace

Classes: CommandLine, Error

Instance Method Summary collapse

Instance Method Details

#run(command, args = nil) ⇒ Object

Run an external command. Raise Error if something goes wrong. The command will be echoed if verbose?.

Usage is similar to Kernel#system. If args is nil, command will be passed to the shell. If args are included, the command and args will be run directly without the shell.



16
17
18
19
20
# File 'lib/scripto/run_commands.rb', line 16

def run(command, args = nil)
  cmd = CommandLine.new(command, args)
  vputs(cmd)
  cmd.run
end

#run_capture(command, args = nil) ⇒ Object

Run a command and capture the output like backticks. See #run for details.



24
25
26
# File 'lib/scripto/run_commands.rb', line 24

def run_capture(command, args = nil)
  CommandLine.new(command, args).capture
end

#run_fails?(command, args = nil) ⇒ Boolean

Returns true if the command fails. See #run for details.

Returns:

  • (Boolean)


46
47
48
# File 'lib/scripto/run_commands.rb', line 46

def run_fails?(command, args = nil)
  !run_succeeds?(command, args)
end

#run_quietly(command, args = nil) ⇒ Object

Run a command and suppress output by redirecting to /dev/null. See #run for details.



30
31
32
33
# File 'lib/scripto/run_commands.rb', line 30

def run_quietly(command, args = nil)
  cmd = CommandLine.new(command, args)
  run("#{cmd} > /dev/null 2> /dev/null")
end

#run_succeeds?(command, args = nil) ⇒ Boolean

Returns true if the command succeeds. See #run for details.

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/scripto/run_commands.rb', line 36

def run_succeeds?(command, args = nil)
  begin
    run_quietly(command, args)
    true
  rescue Error
    false
  end
end

#shellescape(str) ⇒ Object

Escape str if necessary. Useful for passing arguments to a shell.



51
52
53
# File 'lib/scripto/run_commands.rb', line 51

def shellescape(str)
  Shellwords.escape(str)
end