jgrep 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/jgrep +46 -4
- data/jgrep.gemspec +1 -1
- data/lib/jgrep.rb +1 -1
- metadata +2 -2
data/bin/jgrep
CHANGED
@@ -1,16 +1,58 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'jgrep'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {:flat => false}
|
7
|
+
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: jgrep [options] \"expression\""
|
10
|
+
opts.on("-s", "--simple FIELD", "Display a single field from each of the resulting json documents") do |field|
|
11
|
+
options[:field] = field
|
12
|
+
end
|
13
|
+
opts.on("-f", "--flat", "Display non pretty json") do
|
14
|
+
options[:flat] = true
|
15
|
+
end
|
16
|
+
|
17
|
+
end.parse!
|
18
|
+
|
4
19
|
|
5
20
|
begin
|
6
|
-
raise "Please pass a valid arugment to jgrep" if ARGV == []
|
21
|
+
raise "Please pass a valid arugment to jgrep" if ARGV == [] || ARGV ==["-s"]
|
7
22
|
rescue Exception => e
|
8
23
|
STDERR.puts e
|
9
24
|
exit 1
|
10
25
|
end
|
11
26
|
|
12
|
-
|
27
|
+
begin
|
28
|
+
json = STDIN.read
|
13
29
|
|
30
|
+
unless options[:field]
|
31
|
+
result = JGrep::jgrep((json), ARGV.last)
|
32
|
+
unless result == "[]"
|
33
|
+
(options[:flat] == false) ? puts(JSON.pretty_generate(result)) : puts(result.to_json)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
if options[:field] =~ /=|<|>/
|
37
|
+
raise "'-s' flag requires a field to display"
|
38
|
+
end
|
14
39
|
|
15
|
-
result = JGrep::jgrep((json), ARGV
|
16
|
-
|
40
|
+
result = JGrep::jgrep((json), ARGV.last)
|
41
|
+
|
42
|
+
result.each do |r|
|
43
|
+
options[:field].split(".").each do |node|
|
44
|
+
unless r.is_a? Array
|
45
|
+
r = r[node]
|
46
|
+
else
|
47
|
+
break
|
48
|
+
end
|
49
|
+
end
|
50
|
+
unless r.nil?
|
51
|
+
(!r.is_a?(Array) || !r.is_a?(Hash)) ? puts(r) : puts(JSON.pretty_generate(r))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
rescue Exception => e
|
56
|
+
STDERR.puts "Error - #{e}"
|
57
|
+
exit 1
|
58
|
+
end
|
data/jgrep.gemspec
CHANGED
data/lib/jgrep.rb
CHANGED