Module: Scripto::RunCommands

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

Defined Under Namespace

Classes: CommandLine, RunError

Instance Method Summary collapse

Instance Method Details

#run(command, args = nil) ⇒ Object

Run an external command. Raise RunError 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
# File 'lib/scripto/run_commands.rb', line 16

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

#run_capture(command, args = nil) ⇒ Object

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



22
23
24
# File 'lib/scripto/run_commands.rb', line 22

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)


42
43
44
# File 'lib/scripto/run_commands.rb', line 42

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.



28
29
30
31
# File 'lib/scripto/run_commands.rb', line 28

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)


34
35
36
37
38
39
# File 'lib/scripto/run_commands.rb', line 34

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

#shellescape(str) ⇒ Object

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



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

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