Method: Yq.search

Defined in:
lib/yq.rb

.search(query, json) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yq.rb', line 25

def self.search(query, json)
  cmd = which('jq') + %Q[ '#{query}']
  input = json
  output = ""
  LOGGER.debug "sending jq #{cmd}"

  Open3.popen2(cmd) do |i, o, t|
    begin
      pid = t.pid

      if input
        i.puts input
        i.close
      end

      Timeout.timeout(5) do
        o.each { |v|
          output << v
        }
      end
    rescue Timeout::Error
      LOGGER.warn "Timing out #{t.inspect} after 1 second"
      Process.kill(15, pid)
    ensure
      status = t.value
      raise "JQ failed to exit cleanly" unless status.success?
    end
  end
  return output
end