Module: Autoshell::Run

Included in:
Base
Defined in:
lib/autoshell/run.rb

Overview

Execution stuff

Instance Method Summary collapse

Instance Method Details

#command?(cmd) ⇒ String, False

Check if a command is available

Parameters:

  • cmd (String)

    Name of the command

Returns:

  • (String)

    Path to the command

  • (False)

    If command does not exist



39
40
41
42
43
# File 'lib/autoshell/run.rb', line 39

def command?(cmd)
  run('which', cmd.to_s)
rescue CommandError
  return false
end

#run(*args, **opts) ⇒ String

Wrapper around Open3.capture2e

Returns:

  • (String)

    command output

Raises:

See Also:

  • Open3#capture2e


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/autoshell/run.rb', line 12

def run(*args, **opts)
  # reset all environment variables...
  opts[:unsetenv_others] = true unless opts.keys.include? :unsetenv_others

  # run the command
  out, status = Open3.capture2e(@env, *args, **opts)

  # cleanup output
  out.strip!

  # the prompt
  msg = "#{prompt_ansi(args)}\n#{out}"

  if status.success?
    logger.debug msg
    return out
  else
    logger.error msg
    raise CommandError, "#{prompt_text(args)}\n#{out}"
  end
end