Skip to content

Commit

Permalink
Allow pass the content via stdin
Browse files Browse the repository at this point in the history
For example:

```
cat test/fixtures/merge.yml | flatito
```
  • Loading branch information
ceritium committed Mar 20, 2024
1 parent 6b47d9a commit 63acd12
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 43 deletions.
11 changes: 7 additions & 4 deletions exe/flatito
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require "flatito"
require "optparse"

# If no arguments are given, print help
ARGV << "-h" if ARGV.empty?
stdin = $stdin.read unless $stdin.tty?
ARGV << "-h" if ARGV.empty? && !stdin

options = {}
OptionParser.new do |opts|
Expand Down Expand Up @@ -43,8 +44,10 @@ OptionParser.new do |opts|
end
end.parse!

begin
Flatito::Config.prepare_with_options(options)

if stdin
Flatito.flat_content(stdin, options)
else
Flatito.search(ARGV, options)
rescue Interrupt
warn "\nInterrupted"
end
15 changes: 13 additions & 2 deletions lib/flatito.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@
require_relative "flatito/yaml_with_line_number"
require_relative "flatito/renderer"
require_relative "flatito/regex_from_search"
require_relative "flatito/print_items"
require_relative "flatito/config"

module Flatito
def self.search(paths, options)
Finder.new(paths, options).call
class << self
def search(paths, options = {})
Finder.new(paths, options).call
rescue Interrupt
warn "\nInterrupted"
end

def flat_content(content, options = {})
items = FlattenYaml.items_from_content(content)
PrintItems.call(options[:search], items)
end
end
end
17 changes: 17 additions & 0 deletions lib/flatito/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Flatito
module Config
@stdout = $stdout
@stderr = $stderr
@stdin = $stdin

class << self
attr_accessor :renderer, :stdout, :stder, :stdin

def prepare_with_options(options)
self.renderer = Renderer.build(options)
end
end
end
end
16 changes: 7 additions & 9 deletions lib/flatito/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ class Finder

DEFAULT_EXTENSIONS = %w[json yml yaml].freeze

attr_reader :paths, :search, :extensions, :options, :renderer
attr_reader :paths, :search, :extensions, :options

def initialize(paths, options = {})
@paths = paths
@search = options[:search]
@extensions = prepare_extensions(options[:extensions] || DEFAULT_EXTENSIONS)
@options = options
@renderer = Renderer.build(options)
end

def call
Expand All @@ -36,14 +35,13 @@ def call

private

def flat_and_filter(pathname)
items = FlattenYaml.new(pathname).items
items = filter_by_search(items) if search

return unless items.any?
def renderer
Config.renderer
end

renderer.print_pathname(pathname)
renderer.print_items(items)
def flat_and_filter(pathname)
items = FlattenYaml.items_from_path(pathname)
PrintItems.call(search, items, pathname)
end

def prepare_extensions(extensions)
Expand Down
19 changes: 16 additions & 3 deletions lib/flatito/flatten_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
module Flatito
class FlattenYaml
Item = Struct.new(:key, :value, :line, keyword_init: true)
def initialize(pathname)
@pathname = pathname
class << self
def items_from_path(pathname)
content = File.read(pathname)
new(content).items
end

def items_from_content(content)
new(content).items
end
end

attr_reader :content

def initialize(content)
@content = content
end

def items
Expand All @@ -31,7 +44,7 @@ def with_line_numbers
handler = YAMLWithLineNumber::TreeBuilder.new
handler.parser = Psych::Parser.new(handler)

handler.parser.parse(File.read(@pathname))
handler.parser.parse(content)
YAMLWithLineNumber::VisitorsToRuby.create.accept(handler.root)
rescue Psych::SyntaxError
warn "Invalid YAML #{@pathname}"
Expand Down
17 changes: 17 additions & 0 deletions lib/flatito/print_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Flatito
class PrintItems
def self.call(search, items, pathname = nil)
items = filter_by_search(items) if search
return unless items.any?

renderer.print_pathname(pathname) if pathname
renderer.print_items(items)
end

def self.renderer
Config.renderer
end
end
end
30 changes: 11 additions & 19 deletions lib/flatito/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ class Renderer
include RegexFromSearch

def self.build(options)
if tty?
if Config.stdout.tty?
Renderer::TTY.new(options)
else
Renderer::Plain.new(options)
end
end

def self.tty?
$stdout.tty?
end
end

class Base
Expand All @@ -33,7 +29,7 @@ def print_file_progress(pathname); end
def ending; end

def print_pathname(pathname)
puts colorize(pathname.to_s, :light_blue)
stdout.puts colorize(pathname.to_s, :light_blue)
end

def print_items(items)
Expand All @@ -42,8 +38,8 @@ def print_items(items)
items.each do |item|
print_item(item, line_number_padding)
end
puts
flush
stdout.puts
stdout.flush
end

def print_item(item, line_number_padding)
Expand All @@ -54,15 +50,11 @@ def print_item(item, line_number_padding)
""
end

puts "#{line_number} #{matched_string(item.key)} #{value}"
stdout.puts "#{line_number} #{matched_string(item.key)} #{value}"
end

private

def flush
stdout.flush
end

def regex
@regex ||= Regexp.new(search)
end
Expand All @@ -85,7 +77,7 @@ def truncate(string, max = 50)
end

def stdout
$stdout
Config.stdout
end

def colorize(string, color)
Expand All @@ -95,7 +87,7 @@ def colorize(string, color)

class Renderer::Plain < Base
def ending
puts
stdout.puts
end
end

Expand Down Expand Up @@ -128,19 +120,19 @@ def print_pathname(pathname)
def ending
clear_line
show_cursor
puts
stdout.puts
end

def hide_cursor
print HIDE_CURSOR
stdout.print HIDE_CURSOR
end

def show_cursor
print SHOW_CURSOR
stdout.print SHOW_CURSOR
end

def clear_line
print CLEAR_LINE
stdout.print CLEAR_LINE
end

private
Expand Down
31 changes: 31 additions & 0 deletions test/flatito/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "test_helper"

class Flatito::ConfigTest < Minitest::Test
class DummyTTY < StringIO
def tty?
true
end
end

class DummyNotTTY < StringIO
def tty?
false
end
end

test "when stdout is tty set renderer as tty" do
Flatito::Config.stdout = DummyTTY.new
Flatito::Config.prepare_with_options({})

assert_kind_of Flatito::Renderer::TTY, Flatito::Config.renderer
end

test "when stdout is not tty set renderer as plain" do
Flatito::Config.stdout = DummyNotTTY.new
Flatito::Config.prepare_with_options({})

assert_kind_of Flatito::Renderer::Plain, Flatito::Config.renderer
end
end
22 changes: 16 additions & 6 deletions test/flatito/flatten_yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Flatito::FlattenYamlTest < Minitest::Test
test "a json" do
items = Flatito::FlattenYaml.new("test/fixtures/a.json").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/a.json")

assert_equal 5, items.size
assert_equal "one", items[0].key
Expand All @@ -13,7 +13,7 @@ class Flatito::FlattenYamlTest < Minitest::Test
end

test "no nested" do
items = Flatito::FlattenYaml.new("test/fixtures/no_nested.yml").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/no_nested.yml")

assert_equal 3, items.size
assert_equal "one", items[0].key
Expand All @@ -22,7 +22,7 @@ class Flatito::FlattenYamlTest < Minitest::Test
end

test "nested" do
items = Flatito::FlattenYaml.new("test/fixtures/nested.yml").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/nested.yml")

assert_equal 3, items.size
assert_equal "nested1.one", items[0].key
Expand All @@ -39,7 +39,7 @@ class Flatito::FlattenYamlTest < Minitest::Test
end

test "with merging hashes" do
items = Flatito::FlattenYaml.new("test/fixtures/merge.yml").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/merge.yml")

assert_equal 3, items.size

Expand All @@ -57,7 +57,7 @@ class Flatito::FlattenYamlTest < Minitest::Test
end

test "multiline values" do
items = Flatito::FlattenYaml.new("test/fixtures/multiline.yml").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/multiline.yml")

assert_equal "en.long_message", items[0].key
assert_equal 2, items[0].line
Expand All @@ -67,12 +67,22 @@ class Flatito::FlattenYamlTest < Minitest::Test
end

test "with ruby objects" do
items = Flatito::FlattenYaml.new("test/fixtures/with_ruby_objects.yml").items
items = Flatito::FlattenYaml.items_from_path("test/fixtures/with_ruby_objects.yml")

assert_equal 3, items.size

assert_equal "two", items[1].key
assert_equal "[object: OpenStruct]", items[1].value
assert_equal 2, items[1].line
end

test "items from content with duplicated keys" do
content = File.read("test/fixtures/no_nested.yml")
items = Flatito::FlattenYaml.items_from_content(content)

assert_equal 3, items.size
assert_equal "one", items[0].key
assert_equal "One", items[0].value
assert_equal 1, items[0].line
end
end

0 comments on commit 63acd12

Please sign in to comment.