Sha256: 60c34d18c6db5729540040c618467fbd83745dd1dcbca23171608d32c2aeae2c

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'toolrack'
require 'tempfile'

require_relative 'user_prompt'

module Hitcher
  module CommandRunner
    include Antrapol::ToolRack::ConditionUtils
    include Hitcher::UserPrompt
    # functions to assist in running a command line app

    class TerminalNotDefined < StandardError; end

    def run_in_new_terminal(cmd, &block)
      term = Hitcher::Global.instance.config.terminal
      if not_empty?(term)
        notice "\nCommand to run in new terminal app: #{cmd}\n\n"
        #tf = Tempfile.new
        #File.open(tf,"w") do |f|
        #  f.write "#!/bin/sh"
        #  f.write cmd
        #  f.write "read -p \"Any key\""
        #end
        `#{term} -x "#{cmd}"`
        #`#{term} -x sh -c "./#{tf}; bash"`
        term
      else
        raise TerminalNotDefined, "Terminal is not defined in the config file"
      end
    end # run_in_new_terminal

    def run_command(cmd, &block)

      out = []
      err = []
      c = TTY::Command.new
      # raise TTY::Command::ExitError if command exit code is non-zero
      c.run(cmd) do |o,e|
        out << o if not_empty?(o)
        err << e if not_empty?(e)
      end

      if not_empty?(err)
        [false, out, err]
      else
        [true, out, err]
      end

    end # run_command

  end # module CommandRunner
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Hitcher-0.1.3 lib/hitcher/command_runner.rb
Hitcher-0.1.2 lib/hitcher/command_runner.rb