yamlquery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd72fe86b03a3a1318464ca2693bfe3fa44c4ed6
4
+ data.tar.gz: 6335a0dfdacde846eb7ef63cd0a4e33398f46eb6
5
+ SHA512:
6
+ metadata.gz: a9f7984a2933153d9dd38c673da27bfb83fe86da2f2b1127ada40629e99205ae2c64243021e6e6727a78ab0eabaae766b27af541d4ce67258f168fd5bdbeeb96
7
+ data.tar.gz: 96c6eca597f41aefd52fa5bd7e41165585cfaf8495dd11b6deee7c845b7d49a1de7e2e33733f3ae26563d3a7e98252cc28fe6585db206a25342f9687192c391b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yamlquery.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nick B
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Yamlquery
2
+
3
+ Searches yaml files for keys and values using a simple query language. This was developed with the intention of managing puppet hiera files.
4
+
5
+ ## Examples
6
+
7
+ ```bash
8
+ # yq -y -p ../examples/example.yaml 'foo.funk==head'
9
+ ---
10
+ foo:
11
+ funk:
12
+ - head
13
+ - shoulders
14
+ ```
15
+
16
+ ## Installation
17
+
18
+ $ gem install yamlquery
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nburgess/yamlquery.
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
33
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yamlquery"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/yq ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'psych'
4
+ require 'yamlquery'
5
+
6
+ options = YamlQuery::Settings.get_options
7
+
8
+ Dir.glob(options[:yamlpath]).each do |file|
9
+ if File.file?(file)
10
+ if yaml = Psych.load_file(file)
11
+ yq = YamlQuery.new(yaml, options)
12
+ results = yq.object_search
13
+ if ! results.empty?
14
+ YamlQuery::Output.display_results(file, results, options)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ ---
2
+ foo:
3
+ bar:
4
+ - baz
5
+ table: vinny
6
+ funk:
7
+ - head
8
+ - shoulders
@@ -0,0 +1,32 @@
1
+ class YamlQuery::Output
2
+ def flatten_results
3
+ # TODO
4
+ end
5
+
6
+ def self.display_results(file, results, options)
7
+ puts file
8
+ if options[:onlyfile?]
9
+ elsif options[:depth]
10
+ gather_keys(results, options[:depth])
11
+ elsif options[:yaml?]
12
+ puts Psych.dump(results)
13
+ puts
14
+ elsif options[:oneline?]
15
+ p results
16
+ puts
17
+ else
18
+ pp results
19
+ puts
20
+ end
21
+ end
22
+
23
+ def self.gather_keys(results, depth, index = 1)
24
+ if index == depth.to_i
25
+ puts results.keys
26
+ else
27
+ results.each do |k, v|
28
+ gather_keys(v, depth, index + 1) if v.is_a?(Hash)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ require 'parslet'
2
+
3
+ class YamlQuery::Parser < Parslet::Parser
4
+ def initialize(delimiter)
5
+ @delimiter = delimiter
6
+ end
7
+ rule(:key) { match("[^\s#{@delimiter}!<>&= ]").repeat(1) >> space? }
8
+
9
+ rule(:space) { match('\s').repeat(1) }
10
+ rule(:space?) { space.maybe }
11
+
12
+ rule(:delimiter) { match("\\#{@delimiter}") >> space? }
13
+ rule(:delimiter?) { delimiter.maybe >> space? }
14
+
15
+ rule(:operator) { match('[&!=><]') >> match('=').maybe >> space? }
16
+ rule(:argument) { match('.').repeat(1) }
17
+ rule(:argument?) { argument.maybe }
18
+
19
+ rule(:keypath) { key.as(:key) >> delimiter? }
20
+ rule(:expression) { operator.as(:operator) >> argument?.as(:arg) }
21
+ rule(:expression?) { expression.maybe }
22
+ rule(:rooty) { keypath.repeat(1).as(:keys) >> expression? }
23
+ root(:rooty)
24
+ end
25
+
26
+ class YamlQuery::Transform < Parslet::Transform
27
+ rule(:key => simple(:x)) { String(x) }
28
+ end
@@ -0,0 +1,71 @@
1
+ require 'optparse'
2
+ require 'psych'
3
+
4
+ class YamlQuery::Settings
5
+ @options = {}
6
+
7
+ attr_reader :options
8
+
9
+ def self.get_options
10
+ get_rc
11
+ set_default
12
+ get_flags
13
+ @options
14
+ end
15
+
16
+ private
17
+
18
+ def self.get_flags
19
+ OptionParser.new do |opts|
20
+ opts.banner = 'Usage: yq [options] "yaml.path.to.parameter"'
21
+
22
+ opts.on('-h', '--help', 'Prints this help') do
23
+ puts opts
24
+ exit
25
+ end
26
+
27
+ opts.on('-y', '--yaml', "Generate output as yaml") do
28
+ @options[:yaml?] = true
29
+ end
30
+
31
+ opts.on('-d', '--depth DEPTH', 'Only return keys from DEPTH in output') do |depth|
32
+ @options[:depth] = depth
33
+ end
34
+
35
+ opts.on('-p', '--yamlpath PATH', 'Path to yaml files', String) do |path|
36
+ @options[:yamlpath] = path
37
+ end
38
+
39
+ opts.on('-f', '--onlyfile', 'Only list file names that match') do
40
+ @options[:onlyfile?] = true
41
+ end
42
+
43
+ opts.on('-1', '--oneline', 'Matches will be displayed in oneline mode') do
44
+ @options[:oneline?] = true
45
+ end
46
+
47
+ opts.on('--debug', 'Prints some extra output helpful for debugging') do
48
+ @options[:debug] = true
49
+ end
50
+ end.parse!
51
+
52
+ if ARGV.empty?
53
+ puts 'Query string is required.'
54
+ exit 1
55
+ else
56
+ @options[:query] = ARGV[0]
57
+ end
58
+ end
59
+
60
+ def self.get_rc
61
+ yqrc_path = "#{ENV['HOME']}/.yqrc.yaml"
62
+ if File.exist?(yqrc_path)
63
+ @options = Psych.load_file(yqrc_path)
64
+ end
65
+ end
66
+
67
+ def self.set_default
68
+ @options[:delimiter] = '.'
69
+ end
70
+
71
+ end
@@ -0,0 +1,3 @@
1
+ class YamlQuery
2
+ VERSION = '0.0.1'
3
+ end
data/lib/yamlquery.rb ADDED
@@ -0,0 +1,110 @@
1
+ class YamlQuery
2
+ def initialize(object, options)
3
+ @object = object
4
+ @options = options
5
+ tree = parse(@options)
6
+ yqt = YamlQuery::Transform.new
7
+ parsed_query = yqt.apply(tree)
8
+ if options[:debug]
9
+ pp parsed_query
10
+ end
11
+ @keys = parsed_query[:keys]
12
+ @operator = parsed_query[:operator].to_s
13
+ @arg = parsed_query[:arg].to_s
14
+ end
15
+
16
+ def object_search(object = @object, index = 0)
17
+ result_hash = {}
18
+ object.each do |k, v|
19
+ if @keys[index] == k or @keys[index] == '*'
20
+ if @keys.length == index + 1
21
+ result_hash[k] = v if compare(v)
22
+ elsif v.is_a?(Hash)
23
+ result_hash[k] = object_search(v, index + 1)
24
+ result_hash.delete(k) if result_hash[k].empty?
25
+ end
26
+ # A key shouldn't show up more than once, so we can return
27
+ # as soon as we find a match if we aren't matching a wildcard.
28
+ return result_hash unless @keys[index] == '*'
29
+ end
30
+ end
31
+ return result_hash
32
+ end
33
+
34
+ private
35
+
36
+ def parse(options)
37
+ yqp = YamlQuery::Parser.new(options[:delimiter])
38
+ yqp.parse(options[:query])
39
+ rescue Parslet::ParseFailed => failure
40
+ puts failure.cause.ascii_tree
41
+ end
42
+
43
+ def compare(value)
44
+ case @operator
45
+ when ''
46
+ return true
47
+ when '=='
48
+ equal?(value)
49
+ when '!='
50
+ notequal?(value)
51
+ when '!'
52
+ nil?(value)
53
+ when '>='
54
+ ge?(value)
55
+ when '<='
56
+ le?(value)
57
+ when '<'
58
+ lt?(value)
59
+ when '>'
60
+ gt?(value)
61
+ end
62
+ end
63
+
64
+ def assign
65
+ # not yet implemented
66
+ end
67
+
68
+ def ge?(value)
69
+ value >= @arg
70
+ end
71
+
72
+ def le?(value)
73
+ value <= @arg
74
+ end
75
+
76
+ def lt?(value)
77
+ value < @arg
78
+ end
79
+
80
+ def gt?(value)
81
+ value > @arg
82
+ end
83
+
84
+ def equal?(value)
85
+ case value
86
+ when Array, Hash
87
+ value.include?(@arg)
88
+ else
89
+ value == @arg
90
+ end
91
+ end
92
+
93
+ def notequal?(value)
94
+ value != @arg
95
+ end
96
+
97
+ def nil?(value)
98
+ case value
99
+ when Array, Hash
100
+ value.empty?
101
+ else
102
+ value.nil?
103
+ end
104
+ end
105
+ end
106
+
107
+ require 'yamlquery/parser'
108
+ require 'yamlquery/settings'
109
+ require 'yamlquery/output'
110
+ require 'pp'
data/yamlquery.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yamlquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yamlquery"
8
+ spec.version = YamlQuery::VERSION
9
+ spec.authors = ["Nicholas Burgess"]
10
+ spec.email = ["nburgess@uchicago.edu"]
11
+
12
+ spec.summary = 'YamlQuery helps you search yaml files.'
13
+ spec.description = 'Search and set values in multiple yaml files. This is particularly useful for wrangling data in puppetlabs hiera.'
14
+ spec.homepage = "http://github.com/nburg/yamlquery"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry", '~> 0.10.4'
28
+
29
+ spec.add_runtime_dependency "parslet", '~> 1.7.1'
30
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Burgess
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: parslet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.7.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.7.1
83
+ description: Search and set values in multiple yaml files. This is particularly useful
84
+ for wrangling data in puppetlabs hiera.
85
+ email:
86
+ - nburgess@uchicago.edu
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - bin/yq
101
+ - examples/example.yaml
102
+ - lib/yamlquery.rb
103
+ - lib/yamlquery/output.rb
104
+ - lib/yamlquery/parser.rb
105
+ - lib/yamlquery/settings.rb
106
+ - lib/yamlquery/version.rb
107
+ - yamlquery.gemspec
108
+ homepage: http://github.com/nburg/yamlquery
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: YamlQuery helps you search yaml files.
132
+ test_files: []