Module: Kerbi::Utils::Cli
- Defined in:
- lib/utils/cli.rb
Constant Summary collapse
- LIST_TABLE_STYLE =
{ border_left: false, border_right: false, border_top: false, border_x: "", border_y: "", border_i: "" }.freeze
- DESCRIBE_TABLE_STYLE =
{ all_separators: true, border_x: "-", border_y: "", border_i: "" }.freeze
Class Method Summary collapse
- .coerce_hash_or_array(actual, **options) ⇒ Object
-
.dicts_to_json(dicts) ⇒ String
Turns list of key-symbol dicts into their pretty JSON representation.
-
.dicts_to_yaml(dicts) ⇒ String
Turns list of key-symbol dicts into their pretty YAML representation.
- .fmt_table_value(value) ⇒ Object
- .list_to_table(entries, serializer_cls) ⇒ Object
-
.load_kerbifile(root_dir) ⇒ Object
Searches the expected paths for the kerbifile and ruby-loads it.
-
.run_mixers(mixer_classes, values, release_name) ⇒ List<Hash>
Convenience method for running and compiling the output of several mixers.
Class Method Details
.coerce_hash_or_array(actual, **options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/utils/cli.rb', line 19 def self.coerce_hash_or_array(actual, **) if (type = [:coerce_type]).present? if type == 'Array' actual.is_a?(Array) ? actual : [actual] elsif type == 'Hash' actual.is_a?(Array) ? actual[0].to_h : actual.to_h else raise "Unrecognized type coercion #{type}" end else actual end end |
.dicts_to_json(dicts) ⇒ String
Turns list of key-symbol dicts into their pretty JSON representation.
92 93 94 |
# File 'lib/utils/cli.rb', line 92 def self.dicts_to_json(dicts) JSON.pretty_generate(dicts) end |
.dicts_to_yaml(dicts) ⇒ String
Turns list of key-symbol dicts into their pretty YAML representation.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utils/cli.rb', line 38 def self.dicts_to_yaml(dicts) if dicts.is_a?(Array) dicts.each_with_index.map do |h, i| raw = YAML.dump(h.deep_stringify_keys) raw.gsub("---\n", i.zero? ? '' : "---\n\n") end.join("\n") else return "{}" if dicts.empty? as_yaml = YAML.dump(dicts.deep_stringify_keys) as_yaml.gsub("---\n", "") end end |
.fmt_table_value(value) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/utils/cli.rb', line 75 def self.fmt_table_value(value) if value.is_a?(Hash) flattened = Kerbi::Utils::Misc.flatten_hash(value) stringified = flattened.deep_stringify_keys dicts_to_yaml(stringified) elsif value.is_a?(Array) value.join(",") else value.to_s end end |
.list_to_table(entries, serializer_cls) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/utils/cli.rb', line 52 def self.list_to_table(entries, serializer_cls) if entries.is_a?(Array) table = Terminal::Table.new( headings: serializer_cls.header_titles, rows: entries.map(&:values) ) table.style = LIST_TABLE_STYLE table.to_s else table = Terminal::Table.new do |t| #noinspection RubyResolve entries.each do |key, value| new_key = key.upcase.to_s.bold new_value = fmt_table_value(value) t.add_row [new_key, new_value] end t.style = DESCRIBE_TABLE_STYLE end table.to_s end end |
.load_kerbifile(root_dir) ⇒ Object
Searches the expected paths for the kerbifile and ruby-loads it.
99 100 101 102 103 104 105 106 |
# File 'lib/utils/cli.rb', line 99 def self.load_kerbifile(root_dir) root_dir ||= Dir.pwd abs_path = "#{root_dir}/kerbifile.rb" exists = File.exists?(abs_path) raise Kerbi::KerbifileNotFoundError.new(root: root_dir) unless exists #noinspection RubyResolve load(abs_path) end |
.run_mixers(mixer_classes, values, release_name) ⇒ List<Hash>
Convenience method for running and compiling the output of several mixers. Returns all result dicts in a flat array preserving the order they were created in.
12 13 14 15 16 17 |
# File 'lib/utils/cli.rb', line 12 def self.run_mixers(mixer_classes, values, release_name) mixer_classes.inject([]) do |whole, gen_class| mixer_instance = gen_class.new(values, release_name: release_name) whole + mixer_instance.run.flatten end end |