Sha256: 91472fe3459194c3107bcd2f53b8aad54904487d0700889027a0dab10b8eb718

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'scmd'

module CapUtil

  class LocalCmdRunner
    def initialize(cmd_str)
      @cmd = Scmd.new(cmd_str)
    end

    def run!(input=nil)
      CapUtil.say_bulleted "running `#{@cmd}'"
      @cmd.run(input)

      if !@cmd.success?
        CapUtil.say_error(@cmd.stderr)
        CapUtil.halt
      end
      @cmd
    end
  end

  def self.run_locally(cmd_str)
    LocalCmdRunner.new(cmd_str).run!
  end

  def self.run_locally_with_stdin(cmd_str, input)
    LocalCmdRunner.new(cmd_str).run!(input)
  end

  module Run

    def self.included(receiver)
      receiver.send(:extend,  RunLocalMethods)
      receiver.send(:include, RunLocalMethods)
      receiver.send(:include, InstanceMethods)
    end

    module RunLocalMethods
      def run_locally(*args); CapUtil.run_locally(*args); end
      def run_locally_with_stdin(*args); CapUtil.run_locally_with_stdin(*args); end
    end

    module InstanceMethods

      def run(*args, &block)
        cap.run(*args, &block)
      end

      def run_with_stdin(cmd_str, input, opts={})
        run(cmd_str, opts.merge(:pty => true)) {|ch, stream, out| ch.send_data(input + "\n")}
      end

      def run_as(user, cmd_str, opts={}, &block)
        as_cmd_str = "su #{user} -lc '#{cmd_str.gsub("'", "\\'")}'"
        run(as_cmd_str, opts, &block)
      end

      def run_as_with_stdin(user, cmd_str, input, opts={})
        run_as(user, cmd_str, opts) {|ch, stream, out| ch.send_data(input + "\n")}
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cap-util-1.0.1 lib/cap-util/run.rb
cap-util-1.0.0 lib/cap-util/run.rb
cap-util-1.0.0.rc1 lib/cap-util/run.rb