flatito 0.1.0

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
+ SHA256:
3
+ metadata.gz: 420f7b09c4eb71cded9e1c3765b1fab02d0385bcb7e51cebed2d6f8945e8b182
4
+ data.tar.gz: 3bddb748de4716a151ccc60590f52c59d55b2506f744d217bb5bd19f05818dc7
5
+ SHA512:
6
+ metadata.gz: dde72b5db32e1d4da64f60127ce0fe0fb0072bba6db06e8a5888df3aec4839eb5ae46954f7dc5b31f819d4752c98937957d4d1082eee2b552dfff6647c9940dd
7
+ data.tar.gz: 690ccabd218f757b9282d5e02c9c0f76f98ae8e5b14dde543b148888bf7b8d9489e848659dba4c888c04c7661d38c13abe1bde0a8f87dfcac5743dab62e2c1c4
data/.rubocop.yml ADDED
@@ -0,0 +1,67 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+
5
+
6
+ require:
7
+ - rubocop-performance
8
+ - rubocop-minitest
9
+ - rubocop-rake
10
+
11
+ Metrics/ParameterLists:
12
+ Enabled: true
13
+ Exclude:
14
+ - 'lib/flatito/yaml_with_line_number.rb'
15
+ Metrics/PerceivedComplexity:
16
+ Enabled: false
17
+ Exclude:
18
+ - 'lib/flatito/yaml_with_line_number.rb'
19
+ Naming/MethodParameterName:
20
+ Enabled: true
21
+ Exclude:
22
+ - 'lib/flatito/yaml_with_line_number.rb'
23
+ Layout/DotPosition:
24
+ EnforcedStyle: trailing
25
+ Layout/LineLength:
26
+ Enabled: false
27
+ Lint/MissingSuper:
28
+ Enabled: false
29
+ Metrics/AbcSize:
30
+ Enabled: false
31
+ Metrics/BlockLength:
32
+ Enabled: false
33
+ Metrics/ClassLength:
34
+ Enabled: false
35
+ Metrics/CyclomaticComplexity:
36
+ Enabled: false
37
+ Metrics/MethodLength:
38
+ Enabled: false
39
+ Minitest/MultipleAssertions:
40
+ Enabled: false
41
+ Style/ClassAndModuleChildren:
42
+ Enabled: false
43
+ Style/CommentAnnotation:
44
+ Enabled: false
45
+ Style/ConditionalAssignment:
46
+ Enabled: false
47
+ Style/Documentation:
48
+ Enabled: false
49
+ Style/GuardClause:
50
+ Enabled: false
51
+ Style/HashSyntax:
52
+ Enabled: false
53
+ Style/IfUnlessModifier:
54
+ Enabled: false
55
+ Style/MultilineBlockChain:
56
+ Enabled: false
57
+ Style/OrAssignment:
58
+ Enabled: false
59
+ Style/StringLiterals:
60
+ EnforcedStyle: double_quotes
61
+ Style/StringLiteralsInInterpolation:
62
+ Enabled: true
63
+ EnforcedStyle: double_quotes
64
+ Style/SymbolArray:
65
+ Enabled: false
66
+ Style/WordArray:
67
+ Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ceritium@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 José Galisteo
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,51 @@
1
+ # Flatito: Grep for YAML and JSON files
2
+
3
+ A kind of grep for YAML and JSON files. It allows you to search for a key and get the value and the line number where it is located.
4
+
5
+ ![Example](docs/screenshot.png)
6
+
7
+ ## Meaning
8
+
9
+ [Esperanto](https://en.wiktionary.org/wiki/flatito): singular past nominal passive participle of flati.
10
+
11
+ ## Installation
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add flatito
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install flatito
20
+
21
+ ## Usage
22
+
23
+ ```sh
24
+ Usage: flatito PATH [options]
25
+ Example: flatito . -k hello
26
+
27
+ -h, --help Prints this help
28
+ -V, --version Show version
29
+ -k, --search-key=SEARCH Search string
30
+ --no-color Disable color output
31
+ -e, --extensions=EXTENSIONS File extensions to search, separated by comma, default: (json,yaml,yaml)
32
+ --no-skipping Do not skip hidden files
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ceritium/flatito. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ceritium/flatito/blob/master/CODE_OF_CONDUCT.md).
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the Flatito project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ceritium/flatito/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
Binary file
data/exe/flatito ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "flatito"
6
+ require "optparse"
7
+
8
+ # If no arguments are given, print help
9
+ ARGV << "-h" if ARGV.empty?
10
+
11
+ options = {}
12
+ OptionParser.new do |opts|
13
+ opts.banner = <<~HEREDOC
14
+ Usage: flatito PATH [options]
15
+ Example: flatito . -k hello
16
+
17
+ HEREDOC
18
+
19
+ opts.on("-h", "--help", "Prints this help") do
20
+ puts opts
21
+ exit
22
+ end
23
+
24
+ opts.on("-V", "--version", "Show version") do
25
+ puts "Flatito version #{Flatito::VERSION}"
26
+ exit
27
+ end
28
+
29
+ opts.on("-kSEARCH", "--search-key=SEARCH", "Search string") do |s|
30
+ options[:search] = s
31
+ end
32
+
33
+ opts.on("--no-color", "Disable color output") do
34
+ options[:no_color] = true
35
+ end
36
+
37
+ opts.on("-eEXTENSIONS", "--extensions=EXTENSIONS", "File extensions to search, separated by comma, default: (json,yaml,yaml)") do |e|
38
+ options[:extensions] = e.split(",").map(&:strip)
39
+ end
40
+
41
+ opts.on("--no-skipping", "Do not skip hidden files") do
42
+ options[:skip_hidden] = false
43
+ end
44
+ end.parse!
45
+
46
+ begin
47
+ Flatito.search(ARGV, options)
48
+ rescue Interrupt
49
+ warn "\nInterrupted"
50
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "regex_from_search"
4
+
5
+ module Flatito
6
+ class Finder
7
+ include RegexFromSearch
8
+
9
+ DEFAULT_EXTENSIONS = %w[json yml yaml].freeze
10
+
11
+ attr_reader :paths, :search, :extensions, :options, :renderer
12
+
13
+ def initialize(paths, options = {})
14
+ @paths = paths
15
+ @search = options[:search]
16
+ @extensions = prepare_extensions(options[:extensions] || DEFAULT_EXTENSIONS)
17
+ @options = options
18
+ @renderer = Renderer.build(options)
19
+ end
20
+
21
+ def call
22
+ renderer.prepare
23
+
24
+ paths.each do |path|
25
+ TreeIterator.new(path, options).each do |pathname|
26
+ renderer.print_file_progress(pathname)
27
+
28
+ if extensions.include?(pathname.extname)
29
+ flat_and_filter(pathname)
30
+ end
31
+ end
32
+ end
33
+ ensure
34
+ renderer.ending
35
+ end
36
+
37
+ private
38
+
39
+ def flat_and_filter(pathname)
40
+ items = FlattenYaml.new(pathname).items
41
+ items = filter_by_search(items) if search
42
+
43
+ return unless items.any?
44
+
45
+ renderer.print_pathname(pathname)
46
+ renderer.print_items(items)
47
+ end
48
+
49
+ def prepare_extensions(extensions)
50
+ extensions.map do |ext|
51
+ ext.start_with?(".") ? ext : ".#{ext}"
52
+ end
53
+ end
54
+
55
+ def filter_by_search(items)
56
+ items.select do |item|
57
+ regex.match?(item.key)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ class FlattenYaml
5
+ Item = Struct.new(:key, :value, :line, keyword_init: true)
6
+ def initialize(pathname)
7
+ @pathname = pathname
8
+ end
9
+
10
+ def items
11
+ with_line_numbers.compact.flat_map do |line|
12
+ flatten_hash(line) if line.is_a?(Hash)
13
+ end.compact
14
+ end
15
+
16
+ def flatten_hash(hash, prefix = nil)
17
+ hash.flat_map do |key, value|
18
+ if value.is_a?(YAMLWithLineNumber::ValueWithLineNumbers)
19
+ if value.value.is_a?(Hash)
20
+ flatten_hash(value.value, [prefix, key].compact.join("."))
21
+ else
22
+ Item.new(key: [prefix, key].compact.join("."), value: truncate(value.value.to_s), line: value.line)
23
+ end
24
+ end
25
+ end
26
+ rescue StandardError => e
27
+ warn "Error parsing #{@pathname}, #{e.message}"
28
+ end
29
+
30
+ def with_line_numbers
31
+ handler = YAMLWithLineNumber::TreeBuilder.new
32
+ handler.parser = Psych::Parser.new(handler)
33
+
34
+ handler.parser.parse(File.read(@pathname))
35
+ YAMLWithLineNumber::VisitorsToRuby.create.accept(handler.root)
36
+ rescue Psych::SyntaxError
37
+ warn "Invalid YAML #{@pathname}"
38
+
39
+ []
40
+ rescue StandardError => e
41
+ warn "Error parsing #{@pathname}, #{e.message}"
42
+
43
+ []
44
+ end
45
+
46
+ def truncate(string, max = 50)
47
+ string.length > max ? "#{string[0...max]}..." : string
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ module RegexFromSearch
5
+ def regex
6
+ @regex ||= Regexp.new(search)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "io/console"
4
+ require_relative "regex_from_search"
5
+
6
+ module Flatito
7
+ class Renderer
8
+ include RegexFromSearch
9
+
10
+ def self.build(options)
11
+ if tty?
12
+ Renderer::TTY.new(options)
13
+ else
14
+ Renderer::Plain.new(options)
15
+ end
16
+ end
17
+
18
+ def self.tty?
19
+ $stdout.tty?
20
+ end
21
+ end
22
+
23
+ class Base
24
+ attr_reader :search, :no_color
25
+
26
+ def initialize(options)
27
+ @no_color = options[:no_color] || false
28
+ @search = options[:search]
29
+ end
30
+
31
+ def prepare; end
32
+ def print_file_progress(pathname); end
33
+ def ending; end
34
+
35
+ def print_pathname(pathname)
36
+ puts colorize(pathname.to_s, :light_blue)
37
+ end
38
+
39
+ def print_items(items)
40
+ line_number_padding = items.map(&:line).max.to_s.length
41
+
42
+ items.each do |item|
43
+ print_item(item, line_number_padding)
44
+ end
45
+ puts
46
+ flush
47
+ end
48
+
49
+ def print_item(item, line_number_padding)
50
+ line_number = colorize("#{item.line.to_s.rjust(line_number_padding)}: ", :yellow)
51
+ value = if item.value.length.positive?
52
+ colorize("=> #{item.value}", :gray)
53
+ else
54
+ ""
55
+ end
56
+
57
+ puts "#{line_number} #{matched_string(item.key)} #{value}"
58
+ end
59
+
60
+ private
61
+
62
+ def flush
63
+ stdout.flush
64
+ end
65
+
66
+ def regex
67
+ @regex ||= Regexp.new(search)
68
+ end
69
+
70
+ def matched_string(string)
71
+ return string if search.nil? || no_color?
72
+
73
+ regex.match(string).to_a&.each do |match|
74
+ string = string.gsub(/#{match}/, match.colorize(:light_red))
75
+ end
76
+ string
77
+ end
78
+
79
+ def no_color?
80
+ ENV["TERM"] == "dumb" || ENV["NO_COLOR"] == "true" || no_color == true
81
+ end
82
+
83
+ def truncate(string, max = 50)
84
+ string.length > max ? "#{string[0...max]}..." : string
85
+ end
86
+
87
+ def stdout
88
+ $stdout
89
+ end
90
+
91
+ def colorize(string, color)
92
+ no_color? ? string : string.colorize(color)
93
+ end
94
+ end
95
+
96
+ class Renderer::Plain < Base
97
+ def ending
98
+ puts
99
+ end
100
+ end
101
+
102
+ class Renderer::TTY < Base
103
+ CSI = "\e["
104
+ CLEAR_LINE = "#{CSI}K\e[0G".freeze
105
+ HIDE_CURSOR = "#{CSI}?25l".freeze
106
+ SHOW_CURSOR = "#{CSI}?25h".freeze
107
+
108
+ def initialize(options)
109
+ super
110
+ require "io/console"
111
+ end
112
+
113
+ def prepare
114
+ listen_for_stdout_width_change
115
+ hide_cursor
116
+ end
117
+
118
+ def print_file_progress(pathname)
119
+ print truncate(pathname.to_s, stdout_width - 4)
120
+ clear_line
121
+ end
122
+
123
+ def print_pathname(pathname)
124
+ clear_line
125
+ super
126
+ end
127
+
128
+ def ending
129
+ clear_line
130
+ show_cursor
131
+ puts
132
+ end
133
+
134
+ def hide_cursor
135
+ print HIDE_CURSOR
136
+ end
137
+
138
+ def show_cursor
139
+ print SHOW_CURSOR
140
+ end
141
+
142
+ def clear_line
143
+ print CLEAR_LINE
144
+ end
145
+
146
+ private
147
+
148
+ def stdout_width
149
+ @stdout_width ||= stdout.winsize[1]
150
+ rescue StandardError
151
+ 80
152
+ end
153
+
154
+ def listen_for_stdout_width_change
155
+ Signal.trap(:WINCH) do
156
+ @stdout_width = stdout.winsize[1]
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ class TreeIterator
5
+ include Enumerable
6
+
7
+ attr_reader :base_path, :skip_hidden
8
+
9
+ def initialize(base_path, options = {})
10
+ @base_path = base_path
11
+ @skip_hidden = options[:skip_hidden] || true
12
+ end
13
+
14
+ def each(&block)
15
+ tree(Pathname.new(base_path), &block)
16
+ end
17
+
18
+ def tree(parent, &block)
19
+ if parent.directory?
20
+ return if parent.symlink?
21
+
22
+ parent.each_child.each do |pathname|
23
+ next if skip_hidden && pathname.basename.to_s[0] == "."
24
+
25
+ tree(pathname, &block)
26
+ end
27
+ else
28
+ yield(parent)
29
+ end
30
+ rescue Errno::ELOOP => e
31
+ warn "Error reading #{parent}, #{e.message}"
32
+
33
+ []
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "psych"
4
+
5
+ module Flatito
6
+ # Most of this magic comes from the following gist with a few modifications:
7
+ # https://gist.github.com/johncarney/7332f7b2075b86ea52177a4a82453806
8
+ module YAMLWithLineNumber
9
+ ValueWithLineNumbers = Struct.new(:value, :line)
10
+
11
+ class Whatever
12
+ attr_reader :klassname
13
+
14
+ def initialize(klassname)
15
+ @klassname = klassname
16
+ end
17
+
18
+ def to_s
19
+ "[object: #{klassname}]"
20
+ end
21
+
22
+ def method_missing(*)
23
+ self
24
+ end
25
+
26
+ def respond_to_missing?(*)
27
+ false
28
+ end
29
+ end
30
+
31
+ class ClassLoader < Psych::ClassLoader
32
+ def find(klassname)
33
+ @cache[klassname] ||= resolve(klassname)
34
+ rescue ArgumentError
35
+ Whatever.new(klassname)
36
+ end
37
+ end
38
+
39
+ class NodesScalar < Psych::Nodes::Scalar
40
+ attr_reader :line_number
41
+
42
+ def initialize(*args, line_number)
43
+ super(*args)
44
+ @line_number = line_number
45
+ end
46
+ end
47
+
48
+ class TreeBuilder < Psych::TreeBuilder
49
+ attr_accessor :parser
50
+
51
+ def scalar(*args)
52
+ node = NodesScalar.new(*args, parser.mark.line)
53
+ @last.children << node
54
+ node
55
+ end
56
+ end
57
+
58
+ class VisitorsToRuby < Psych::Visitors::ToRuby
59
+ def self.create(symbolize_names: false, freeze: false, strict_integer: false)
60
+ class_loader = ClassLoader.new
61
+ scanner = Psych::ScalarScanner.new(class_loader, strict_integer: strict_integer)
62
+ new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze)
63
+ end
64
+
65
+ def visit_Flatito_YAMLWithLineNumber_NodesScalar(node) # rubocop:disable Naming/MethodName
66
+ visit_Psych_Nodes_Scalar(node)
67
+ end
68
+
69
+ private
70
+
71
+ def revive_hash(hash, node, _klass = nil)
72
+ node.children.each_slice(2) do |k, v|
73
+ key = accept(k)
74
+ val = accept(v)
75
+
76
+ if key == "<<" && k.tag != "tag:yaml.org,2002:str"
77
+ case v
78
+ when Psych::Nodes::Alias, Psych::Nodes::Mapping
79
+ begin
80
+ hash.merge! val
81
+ rescue TypeError
82
+ hash[key] = val
83
+ end
84
+ when Psych::Nodes::Sequence
85
+ begin
86
+ h = {}
87
+ val.reverse_each do |value|
88
+ h.merge! value
89
+ end
90
+ hash.merge! h
91
+ rescue TypeError
92
+ hash[key] = val
93
+ end
94
+ else
95
+ hash[key] = val
96
+ end
97
+ else
98
+ if k.is_a? NodesScalar
99
+ val = ValueWithLineNumbers.new(val, k.line_number + 1)
100
+ end
101
+
102
+ hash[key] = val
103
+ end
104
+ end
105
+
106
+ hash
107
+ end
108
+ end
109
+ end
110
+ end
data/lib/flatito.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "colorize"
4
+
5
+ require_relative "flatito/version"
6
+ require_relative "flatito/tree_iterator"
7
+ require_relative "flatito/flatten_yaml"
8
+ require_relative "flatito/finder"
9
+ require_relative "flatito/yaml_with_line_number"
10
+ require_relative "flatito/renderer"
11
+ require_relative "flatito/regex_from_search"
12
+
13
+ module Flatito
14
+ def self.search(paths, options)
15
+ Finder.new(paths, options).call
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flatito
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - José Galisteo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A kind of grep for YAML and JSON files. It allows you to search for a
28
+ key and get the value and the line number where it is located.
29
+ email:
30
+ - ceritium@gmail.com
31
+ executables:
32
+ - flatito
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".rubocop.yml"
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - docs/screenshot.png
42
+ - exe/flatito
43
+ - lib/flatito.rb
44
+ - lib/flatito/finder.rb
45
+ - lib/flatito/flatten_yaml.rb
46
+ - lib/flatito/regex_from_search.rb
47
+ - lib/flatito/renderer.rb
48
+ - lib/flatito/tree_iterator.rb
49
+ - lib/flatito/version.rb
50
+ - lib/flatito/yaml_with_line_number.rb
51
+ homepage: https://github.com/ceritium/flatito
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/ceritium/flatito
56
+ source_code_uri: https://github.com/ceritium/flatito
57
+ rubygems_mfa_required: 'true'
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.5.6
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Grep for YAML and JSON files
77
+ test_files: []