Sha256: ec87cd18f896c295863b72f0ba1519c8feaa5b9da6215cf118627ad7734403fd

Contents?: true

Size: 855 Bytes

Versions: 5

Compression:

Stored size: 855 Bytes

Contents

require 'singleton'
require 'escape'
require 'fileutils'
require 'posix/spawn'

class RemoteTable
  class Executor
    include ::Singleton
    # we should really be piping things i think
    def bang(path, cmd)
      srand # in case this was forked by resque
      tmp_path = "#{path}.bang.#{rand}"
      backtick_with_reporting "cat #{::Escape.shell_single_word path} | #{cmd} > #{::Escape.shell_single_word tmp_path}"
      ::FileUtils.mv tmp_path, path
    end

    def backtick_with_reporting(cmd, raise_on_error = false)
      cmd = cmd.gsub /\n/m, ' '
      pid = ::POSIX::Spawn.spawn({ 'PATH' => '/usr/local/bin:/usr/bin:/bin' }, cmd)
      stat = ::Process::waitpid pid
      if raise_on_error and not stat.success?
        raise %{
From the remote_table gem...

Command failed:
#{cmd}

Exit code:
#{stat.exitstatus}
}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remote_table-1.2.2 lib/remote_table/executor.rb
remote_table-1.2.1 lib/remote_table/executor.rb
remote_table-1.2.0 lib/remote_table/executor.rb
remote_table-1.1.10 lib/remote_table/executor.rb
remote_table-1.1.9 lib/remote_table/executor.rb