module Autoshell::Run

Execution stuff

Public Instance Methods

command?(cmd) click to toggle source

Check if a command is available

@param cmd [String] Name of the command @return [String] Path to the command @return [False] If command does not exist

# File lib/autoshell/run.rb, line 39
def command?(cmd)
  run('which', cmd.to_s)
rescue CommandError
  return false
end
run(*args, **opts) click to toggle source

Wrapper around Open3.capture2e

@see Open3#capture2e @return [String] command output @raise [CommandError] if the command fails

# 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

Private Instance Methods

prompt_ansi(cmd) click to toggle source

Render a prompt string for logging

@private @param cmd [Array<String>] array of command parts @return [String] ansi formatted string

# File lib/autoshell/run.rb, line 52
def prompt_ansi(cmd)
  "#{ANSI.blue(working_dir)} $ #{ANSI.white(cmd.join(' '))}"
end
prompt_text(cmd) click to toggle source

Render a prompt string for logging

@private @param cmd [Array<String>] array of command parts @return [String] plain text string

# File lib/autoshell/run.rb, line 61
def prompt_text(cmd)
  ANSI.unansi(prompt_ansi(cmd))
end