2020-11-13 19:09:22 +01:00
|
|
|
require "cbor"
|
|
|
|
|
|
|
|
if ARGV.size >= 1 && ARGV[0] == "-h"
|
|
|
|
puts "usage: cq < file.cbor"
|
|
|
|
puts "usage: cq [attribute] < file"
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def dig(data, attribute : String)
|
|
|
|
case data
|
|
|
|
when Hash
|
|
|
|
if v = data[attribute]?
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
else
|
|
|
|
STDERR.puts "attribute not found: #{attribute}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-29 01:08:18 +01:00
|
|
|
buffer = Bytes.new 1_000_000 # 1 MB
|
|
|
|
|
|
|
|
until STDIN.read(buffer) == 0
|
|
|
|
decoder = CBOR::Decoder.new(buffer)
|
|
|
|
while data = decoder.read_value
|
|
|
|
break if data == 0
|
|
|
|
|
|
|
|
if ARGV.size == 0
|
|
|
|
pp data
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
ARGV.each do |attribute|
|
|
|
|
current_data = data.clone
|
|
|
|
attribute.split(/[.]/).each do |attr|
|
|
|
|
current_data = dig current_data, attr
|
|
|
|
end
|
|
|
|
pp current_data
|
|
|
|
end
|
2020-11-13 19:09:22 +01:00
|
|
|
end
|
2020-11-29 01:08:18 +01:00
|
|
|
buffer = Bytes.new 1_000_000 # 1 MB
|
2020-11-13 19:09:22 +01:00
|
|
|
end
|