Class: FlatKit::Command::Cat

Inherits:
FlatKit::Command show all
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

for, #initialize, names

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Constructor Details

This class inherits a constructor from FlatKit::Command

Class Method Details

.descriptionObject



13
14
15
# File 'lib/flat_kit/command/cat.rb', line 13

def self.description
  "Concatenate files together that have the same structure."
end

.nameObject



9
10
11
# File 'lib/flat_kit/command/cat.rb', line 9

def self.name
  "cat"
end

.parserObject



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
    banner Cat.description.to_s
    banner ""

    banner "      Concatenates files that have the same field structure together into\n      a single file. The files can be of different formats, but must have\n      the same fields and names.\n\n      This is probably most easily usable as a way to convert CSV to JSON\n      and vice versa.\n\n      The flatfile type(s) will be automatically determined by the file name.\n      If the inputs or output is not a file, but from stdin or stdout then\n      the input and output types must be specified.\n\n      NOTE: If converting from JSON to CSV and the input JSON does not have\n            every possible field on ever record, then the output csv iwll\n            be corrupted.\n\n            In this case the input json should be fed through 'flatten' first\n            or use the '--flatten' flag which will require an additional pass\n            through the input to gather all the fields\n    BANNER\n\n    banner <<~USAGE\n\n      Usage:\n        fk cat file1.csv file2.csv > combinded.csv\n        fk cat --output-format json file1.csv\n        fk cat file1.csv.gzip -o file2.json.gzip\n        fk cat file1.csv.gzip --output-format json | gzip -c > file1.jsonl.gz\n\n    USAGE\n\n    banner <<~OPTIONS\n\n      Options:\n\n    OPTIONS\n\n    opt :output, \"Send the output to the given path instead of standard out.\", default: \"<stdout>\"\n    opt :input_format, \"Input format, csv or json\", default: \"auto\", short: :none\n    opt :output_format, \"Output format, csv or json\", default: \"auto\", short: :none\n  end\nend\n"

Instance Method Details

#callObject



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

#parseObject



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.message
  end
end