yamlook 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 968d1897a00350db31375552a70066d04d14c4edd7e52406ed2188e4951bb3e2
4
+ data.tar.gz: e45f29b9f4c56114fb8c5d546cc25d91f37c785575c1331879c774523b9ed8f6
5
+ SHA512:
6
+ metadata.gz: 3a667a39333140b85e9710f5d8a87e89b3e951fe634065e2f464ddc7e4d7beb23fbfac0096acd72bf01aeaa28a0a322221dca11d1174fbc88728a0f59703a735
7
+ data.tar.gz: a1415e35e038896b2205d5fa409483aceedefee8566d43a636471b23c89bb423b1ca7b4c3c650a0f23d5af8d05ea8a9c39865443baa0aeecc12cf9771eca2880
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Viacheslav Mefodin
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.
@@ -0,0 +1,17 @@
1
+ # Yamlook
2
+
3
+ ## Installation
4
+
5
+ ```
6
+ $ gem install yamlook
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ $ yamlook some.deep.key.in.you.yaml.file
13
+ ```
14
+
15
+ ## License
16
+
17
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
5
+
6
+ require 'yamlook'
7
+
8
+ Yamlook::Search.perform(ARGV[0].split('.'))
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+
5
+ require 'yamlook/node'
6
+ require 'yamlook/node_list'
7
+ require 'yamlook/file'
8
+ require 'yamlook/search'
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ # Yaml file to perform search
5
+ class File
6
+ attr_reader :filename
7
+
8
+ def initialize(filename)
9
+ @filename = filename
10
+ end
11
+
12
+ def search(keys)
13
+ return unless yaml
14
+
15
+ findings = root_node_list.search(keys).flatten.compact.map do |finding|
16
+ finding.filename = filename
17
+ finding
18
+ end
19
+
20
+ findings if findings.any?
21
+ end
22
+
23
+ def yaml
24
+ @yaml ||= parse_file(filename)
25
+ end
26
+
27
+ private
28
+
29
+ def parse_file(filename)
30
+ ::Psych.parse_file(filename)
31
+ rescue ::Psych::Exception
32
+ nil
33
+ end
34
+
35
+ def root_node_list
36
+ NodeList.new(root_mapping.children)
37
+ end
38
+
39
+ def root_mapping
40
+ yaml.children.first
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ # Result of search - scalar node
5
+ class Node
6
+ attr_reader :value, :line, :column
7
+ attr_accessor :filename
8
+
9
+ def initialize(value:, line:, column:, filename: nil)
10
+ @value = value
11
+ @line = line
12
+ @column = column
13
+ @filename = filename
14
+ end
15
+
16
+ def self.from_scalar(scalar)
17
+ return unless scalar
18
+
19
+ new(
20
+ value: scalar.value,
21
+ line: scalar.start_line + 1,
22
+ column: scalar.start_column + 1
23
+ )
24
+ end
25
+
26
+ def to_s
27
+ "#{filename}:#{line}:#{column}\n#{value}"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ Mapping = ::Psych::Nodes::Mapping
5
+ Scalar = ::Psych::Nodes::Scalar
6
+
7
+ # List of yaml nodes
8
+ class NodeList
9
+ attr_reader :nodes
10
+
11
+ def initialize(nodes)
12
+ @nodes = nodes
13
+ end
14
+
15
+ def search(keys)
16
+ keys.map.with_index do |_, index|
17
+ key = keys[0..index].join('.')
18
+ rest_keys = keys[index + 1..-1]
19
+
20
+ result = find(key)
21
+
22
+ case result
23
+ when NodeList then result.search(rest_keys)
24
+ when Scalar then Node.from_scalar(result)
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def find(key)
32
+ scalar_index = find_scalar_index(key)
33
+ return unless scalar_index
34
+
35
+ result = nodes[scalar_index + 1]
36
+ return NodeList.new(result.children) if result.is_a?(Mapping)
37
+
38
+ result
39
+ end
40
+
41
+ def find_scalar_index(key)
42
+ nodes.find_index { |node| node.is_a?(Scalar) && node.value == key }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ # Searches for occurrences of dot-notated keys in all yaml files
5
+ # of current directory
6
+ module Search
7
+ EXTENSIONS = %w[yml yaml].freeze
8
+ PATTERN = EXTENSIONS.map { |ext| ::File.join('**', "*.#{ext}") }.freeze
9
+
10
+ module_function
11
+
12
+ def perform(keys)
13
+ findings = Dir.glob(PATTERN).map do |filename|
14
+ result = File.new(filename).search(keys)
15
+ print_progress(result)
16
+ result
17
+ end
18
+
19
+ print_result(findings.compact)
20
+ end
21
+
22
+ def print_progress(result)
23
+ print result ? "\033[32m*\033[0m" : "\033[33m.\033[0m"
24
+ end
25
+
26
+ def print_result(findings)
27
+ puts
28
+ puts '-' * 10
29
+ findings.any? ? print_success(findings) : print_failure
30
+ end
31
+
32
+ def print_failure
33
+ puts 'Nothing found.'
34
+ end
35
+
36
+ def print_success(findings)
37
+ print "Found #{findings.count} occurrences:"
38
+ findings.each do |finding|
39
+ puts
40
+ puts finding
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Viacheslav Mefodin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: psych
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.15.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '3.0'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.15.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '5.8'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '5.8'
61
+ description:
62
+ email:
63
+ - mefodin.v@gmail.com
64
+ executables:
65
+ - yamlook
66
+ extensions: []
67
+ extra_rdoc_files:
68
+ - LICENSE.txt
69
+ - README.md
70
+ files:
71
+ - LICENSE.txt
72
+ - README.md
73
+ - bin/yamlook
74
+ - lib/yamlook.rb
75
+ - lib/yamlook/file.rb
76
+ - lib/yamlook/node.rb
77
+ - lib/yamlook/node_list.rb
78
+ - lib/yamlook/search.rb
79
+ - lib/yamlook/version.rb
80
+ homepage: https://github.com/sl4vr/yamlook
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.4.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.1.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Search occurrences of dot-notated yaml keys.
103
+ test_files: []