Sha256: 4de33416a49ef44df6f9e2e75069f891b9c271ed112439b0538189c0e9b1d2a8

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

require 'ansi/code'
require 'open3'

module Autoshell
  # Execution stuff
  module Run
    # Wrapper around Open3.capture2e
    #
    # @see Open3#capture2e
    # @return [String] command output
    # @raise [CommandError] if the command fails
    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

    # 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
    def command?(cmd)
      run('which', cmd.to_s)
    rescue CommandError
      return false
    end

    private

    # Render a prompt string for logging
    #
    # @private
    # @param cmd [Array<String>] array of command parts
    # @return [String] ansi formatted string
    def prompt_ansi(cmd)
      "#{ANSI.blue(working_dir)} $ #{ANSI.white(cmd.join(' '))}"
    end

    # Render a prompt string for logging
    #
    # @private
    # @param cmd [Array<String>] array of command parts
    # @return [String] plain text string
    def prompt_text(cmd)
      ANSI.unansi(prompt_ansi(cmd))
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
autoshell-1.0.6 lib/autoshell/run.rb
autoshell-1.0.5 lib/autoshell/run.rb
autoshell-1.0.4 lib/autoshell/run.rb
autoshell-1.0.3 lib/autoshell/run.rb
autoshell-1.0.2 lib/autoshell/run.rb
autoshell-1.0.1 lib/autoshell/run.rb
autoshell-1.0.0.pre.beta lib/autoshell/run.rb