yq 0.3.1 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7da72514fc6d9279b20ea0f0604a7583e5e204a4
4
- data.tar.gz: f200c7ce3ed94224c50afacf6d8c8841f9d58866
2
+ SHA256:
3
+ metadata.gz: d07d7b73032c8c61bbf2f18b8b9de799c52cbddeef69386953ebeb74fad9b009
4
+ data.tar.gz: 35fd24192ad40d080f3b0a1a3217930ff41b0f96ad3f57e32bfc0ad990f04e27
5
5
  SHA512:
6
- metadata.gz: e156fdc0dcb88c17ba4be467c0c36386b166b56709363195d80e6bbc16631970551c46a89d9f3c97106c12e39b79688e76d2caeec42f9419f188d915d4a7abd5
7
- data.tar.gz: 6a1e145d4159bcf11e9ebd0491bc21b6232ab12dbb56b31454c53b5789c2516b5e3ea4ef0c134cf9b68c43a53a41bb64a1ae4cb0aa7e71d9ebe8f64048280136
6
+ metadata.gz: 31b279062178bf1b4179de42208cc6e8eb98d831cdf7ffd75bc5a4e6ad8fc963f2a6871f52ecb72bc1efa4fcac2e1a5483f25b1c730c0c8b134bb5adcba75055
7
+ data.tar.gz: b52bbb79b6ee17a28a6fce17ca0c5c4354b1c9b4a899afcb869bd46376ea8b319b85f9ba136aeae6ceacd8c00dea19c739c0209bc4e18649ed3f22b54c0b5da8
@@ -1,7 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.1.0
3
+ - 2.0
4
+ - 2.4
5
+ - 2.5
5
6
  - ruby-head
6
7
  # uncomment this line if your project needs to run something other than `rake`:
7
8
  script: bundle exec rspec --require spec_helper --color --format documentation spec
data/bin/yq CHANGED
@@ -9,16 +9,16 @@ require 'yq'
9
9
 
10
10
  $stdout.sync = $stderr.sync = true
11
11
  unless defined?(LOGGER)
12
- LOGGER = Logger.new($stderr)
12
+ LOGGER = Logger.new($stderr)
13
13
  end
14
14
  LOGGER.level = Logger::INFO
15
15
 
16
16
  @options = {
17
- context: :main_loop
17
+ context: :main_loop,
18
+ output: :yaml
18
19
  }
19
20
 
20
-
21
- def prereqs
21
+ def prereqs
22
22
  ARGV << '--help' if ARGV.empty?
23
23
 
24
24
  opts = OptionParser.new do |opts|
@@ -28,15 +28,21 @@ def prereqs
28
28
  puts opts
29
29
  exit 0
30
30
  end
31
- opts.on('--verbose', '-v', 'verbose') do
31
+ opts.on('--verbose', '-v', 'verbose') do
32
32
  LOGGER.level = Logger::DEBUG
33
33
  end
34
34
  opts.on("--develop", "Run in developer mode") do
35
35
  @options[:context] = :develop
36
36
  end
37
+ opts.on('-r', '--raw-output') do
38
+ @options[:output] = :raw
39
+ end
40
+ opts.on('--json-output') do
41
+ @options[:output] = :json
42
+ end
37
43
  end
38
44
  opts.parse!
39
-
45
+
40
46
  required_vars = %w[
41
47
  ]
42
48
  required_vars.each do |v|
@@ -51,7 +57,7 @@ def start
51
57
  begin
52
58
  @yaml = $stdin.read
53
59
 
54
- puts Yq.search_yaml(ARGV[0], @yaml)
60
+ puts Yq.search_yaml(ARGV[0], @yaml, output: @options[:output])
55
61
 
56
62
  rescue => e
57
63
  LOGGER.error "Unhandled exception #{e.class}: #{e.message}"
@@ -76,4 +82,4 @@ end
76
82
 
77
83
 
78
84
  prereqs
79
- self.send(@options[:context])
85
+ self.send(@options[:context])
data/lib/yq.rb CHANGED
@@ -15,20 +15,26 @@ module Yq
15
15
  return nil
16
16
  end
17
17
 
18
- def self.search_yaml(query, yaml)
18
+ def self.search_yaml(query, yaml, output: :yaml)
19
19
  req_json = yaml_to_json(yaml)
20
+ case output
21
+ when :raw
22
+ search(query, req_json, flags: ['--raw-output'])
23
+ when :json
24
+ search(query, req_json)
25
+ when :yaml
20
26
  resp_json = search(query, req_json)
21
- resp_yaml = json_to_yaml(resp_json)
22
- return resp_yaml
27
+ json_to_yaml(resp_json)
28
+ end
23
29
  end
24
30
 
25
- def self.search(query, json)
26
- cmd = which('jq') + %Q[ '#{query}']
31
+ def self.search(query, json, flags: [])
32
+ cmd = [which('jq')] + flags + [query]
27
33
  input = json
28
34
  output = ""
29
35
  LOGGER.debug "sending jq #{cmd}"
30
36
 
31
- Open3.popen2(cmd) do |i, o, t|
37
+ Open3.popen2(*cmd) do |i, o, t|
32
38
  begin
33
39
  pid = t.pid
34
40
 
@@ -1,3 +1,3 @@
1
1
  module Yq
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -2,22 +2,31 @@ describe Yq do
2
2
  subject { described_class }
3
3
 
4
4
  let(:hash) {
5
- { "foo" => { "bar" => { "baz" => "value" }}}
5
+ { "foo" => { "bar" => { "'" => "value" }}}
6
6
  }
7
7
 
8
8
  let(:yaml) {<<EOF}
9
9
  foo:
10
10
  bar:
11
- baz: value
11
+ "'": value
12
+ EOF
13
+
14
+ # support equivalent YAML syntaxes (depends on ruby version)
15
+ let(:yaml_regexp) {<<EOF}
16
+ foo:
17
+ bar:
18
+ ("'"|''''|! ''''): value
12
19
  EOF
13
20
 
14
21
  let(:json) {<<EOF.chomp}
15
- {"foo":{"bar":{"baz":"value"}}}
22
+ {"foo":{"bar":{"'":"value"}}}
16
23
  EOF
17
24
 
25
+ let(:jq_query) { %q({"\\t": .foo.bar["'"]}) }
26
+
18
27
  let(:jq_response) {<<EOF}
19
28
  {
20
- "baz": "value"
29
+ "\\t": "value"
21
30
  }
22
31
  EOF
23
32
 
@@ -73,13 +82,14 @@ EOF
73
82
  EOF
74
83
 
75
84
  describe '.search' do
76
- subject { described_class.search('.foo.bar', json) }
85
+
86
+ subject { described_class.search(jq_query, json) }
77
87
 
78
88
  it { is_expected.to match(jq_response) }
79
89
 
80
90
  it 'passes it through to jq' do
81
91
  allow(Yq).to receive(:which).with('jq').and_return('/bin/jq')
82
- expect(Open3).to receive(:popen2).with(%q[/bin/jq '.foo.bar']).and_return(jq_response)
92
+ expect(Open3).to receive(:popen2).with('/bin/jq' , jq_query).and_return(jq_response)
83
93
  subject
84
94
  end
85
95
 
@@ -99,7 +109,7 @@ EOF
99
109
 
100
110
  describe '.hash_to_yaml' do
101
111
  subject { described_class.hash_to_yaml(hash) }
102
- it { is_expected.to match(yaml) }
112
+ it { is_expected.to match(yaml_regexp) }
103
113
  end
104
114
 
105
115
  describe '.yaml_to_json' do
@@ -109,7 +119,7 @@ EOF
109
119
 
110
120
  describe '.json_to_yaml' do
111
121
  subject { described_class.json_to_yaml(json) }
112
- it { is_expected.to match(yaml) }
122
+ it { is_expected.to match(yaml_regexp) }
113
123
 
114
124
  context 'non-json response' do
115
125
  subject { described_class.json_to_yaml(jq_non_json_response) }
@@ -125,7 +135,7 @@ EOF
125
135
  describe '.search_yaml' do
126
136
  subject { described_class.search_yaml('.foo.bar', yaml) }
127
137
 
128
- it { is_expected.to match("baz: value") }
138
+ it { is_expected.to match(/("'"|''''|! ''''): value/) }
129
139
  end
130
140
 
131
141
 
data/yq.gemspec CHANGED
@@ -23,6 +23,4 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec", '~> 3.3'
25
25
  spec.add_development_dependency "pry"
26
-
27
- spec.add_runtime_dependency "json2yaml"
28
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Park
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-10 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: json2yaml
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  description: |-
84
70
  This unceremoniously shells out to a jq available in $PATH.
85
71
  Please make sure jq is installed.
@@ -125,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
111
  version: '0'
126
112
  requirements: []
127
113
  rubyforge_project:
128
- rubygems_version: 2.4.8
114
+ rubygems_version: 2.7.7
129
115
  signing_key:
130
116
  specification_version: 4
131
117
  summary: A JQ wrapper for YAML