Class: FlatKit::Command::Merge

Inherits:
FlatKit::Command show all
Defined in:
lib/flat_kit/command/merge.rb

Instance Attribute Summary collapse

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

Instance Attribute Details

#compare_keysObject (readonly)

Returns the value of attribute compare_keys.



63
64
65
# File 'lib/flat_kit/command/merge.rb', line 63

def compare_keys
  @compare_keys
end

Class Method Details

.descriptionObject



10
11
12
# File 'lib/flat_kit/command/merge.rb', line 10

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

.nameObject



6
7
8
# File 'lib/flat_kit/command/merge.rb', line 6

def self.name
  "merge"
end

.parserObject



14
15
16
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
# File 'lib/flat_kit/command/merge.rb', line 14

def self.parser
  ::Optimist::Parser.new do
    banner "#{Merge.description}"
    banner ""

    banner "    Given a set of input files that have the same structure, and are already\n    sorted by a set of keys. The Merge command will merge all those files\n    into a single output file.\n\n    The --key parameter is required, and it must be a comma separated list\n    of field nams on the input on which to use as the sort key for the merge\n    process.\n\n    There must also be at least 2 input files. Merging only 1 file into an\n    output file is the same as the 'cat' command.\n\n    The flatfile type(s) will be automatically determined by the file name.\n    If the output is not a file, but to stdout then the output type will\n    be the same as the first input file, or it can be specified as a commandline\n    switch.\n\n    The merge will do a single pass through the input to generate the\n    output.\n    BANNER\n\n    banner <<~USAGE\n\n    Usage:\n      fk merge --key surname,given_name file1.csv file2.csv > all.csv\n      fk merge --key surname,given_name --output-format json file1.csv file2.csv > all.json\n      fk merge --key field1,field2 --output-format json input*.csv | gzip -c > all.json.gz\n      fk merge --key field12 file*.json.gz -o all.json.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    opt :key, \"The comma separted list of field(s) to use for sorting the input\", required: true, type: :string\n  end\nend\n"

Instance Method Details

#callObject



83
84
85
# File 'lib/flat_kit/command/merge.rb', line 83

def call
  @merge.call
end

#parseObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/flat_kit/command/merge.rb', line 65

def parse
  parser = self.class.parser
  ::Optimist::with_standard_exception_handling(parser) do
    begin
      @opts = parser.parse(argv)
      @compare_keys = CSV.parse_line(opts[:key])
      paths = parser.leftovers
      raise ::Optimist::CommandlineError, "At least 2 input files are required" if paths.size < 2

      @merge = ::FlatKit::Merge.new(inputs: paths, input_fallback: opts[:input_format],
                                    compare_fields: @compare_keys,
                                    output: opts[:output], output_fallback: opts[:output_format])
    rescue ::FlatKit::Error => e
      raise ::Optimist::CommandlineError, e.message
    end
  end
end