Class: FlatKit::Command::Cat
- Inherits:
-
FlatKit::Command
- Object
- FlatKit::Command
- FlatKit::Command::Cat
- Defined in:
- lib/flat_kit/command/cat.rb
Overview
Internal: The implementation of the cat command.
TODO: Implement the –flatten commandline switch
Instance Attribute Summary
Attributes inherited from FlatKit::Command
#argv, #env, #logger, #opts, #readers, #writer
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from FlatKit::Command
Methods included from DescendantTracker
#children, #find_child, #find_children, #inherited
Constructor Details
This class inherits a constructor from FlatKit::Command
Class Method Details
.description ⇒ Object
13 14 15 |
# File 'lib/flat_kit/command/cat.rb', line 13 def self.description "Concatenate files together that have the same structure." end |
.name ⇒ Object
9 10 11 |
# File 'lib/flat_kit/command/cat.rb', line 9 def self.name "cat" end |
.parser ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/flat_kit/command/cat.rb', line 17 def self.parser ::Optimist::Parser.new do Cat.description.to_s "" <<~BANNER Concatenates files that have the same field structure together into a single file. The files can be of different formats, but must have the same fields and names. This is probably most easily usable as a way to convert CSV to JSON and vice versa. The flatfile type(s) will be automatically determined by the file name. If the inputs or output is not a file, but from stdin or stdout then the input and output types must be specified. NOTE: If converting from JSON to CSV and the input JSON does not have every possible field on ever record, then the output csv iwll be corrupted. In this case the input json should be fed through 'flatten' first or use the '--flatten' flag which will require an additional pass through the input to gather all the fields BANNER <<~USAGE Usage: fk cat file1.csv file2.csv > combinded.csv fk cat --output-format json file1.csv fk cat file1.csv.gzip -o file2.json.gzip fk cat file1.csv.gzip --output-format json | gzip -c > file1.jsonl.gz USAGE <<~OPTIONS Options: OPTIONS opt :output, "Send the output to the given path instead of standard out.", default: "<stdout>" opt :input_format, "Input format, csv or json", default: "auto", short: :none opt :output_format, "Output format, csv or json", default: "auto", short: :none end end |
Instance Method Details
#call ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/flat_kit/command/cat.rb', line 79 def call total = 0 readers.each do |r| logger.info "cat #{r.source} to #{writer.destination}" r.each do |record| writer.write(record) total += 1 end logger.info "read #{r.count} records from #{r.source}" end writer.close logger.debug "processed #{writer.count} records" logger.debug "read #{total} records" end |
#parse ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flat_kit/command/cat.rb', line 65 def parse parser = self.class.parser ::Optimist.with_standard_exception_handling(parser) do @opts = parser.parse(argv) paths = parser.leftovers @readers = ::FlatKit::Reader.create_readers_from_paths(paths: paths, fallback: opts[:input_format]) @writer = ::FlatKit::Writer.create_writer_from_path(path: opts[:output], fallback: opts[:output_format], reader_format: @readers.first.format_name) rescue ::FlatKit::Error => e raise ::Optimist::CommandlineError, e. end end |