Sha256: 15d4a64ef28e5520ea469b04d5228aae11e262f6f5d5531e4153e199b8cca970

Contents?: true

Size: 1.26 KB

Versions: 1

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 "Command to run in new terminal app: #{cmd}"
        #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

1 entries across 1 versions & 1 rubygems

Version Path
Hitcher-0.1.1 lib/hitcher/command_runner.rb