Module: Kerbi::Utils::Cli
- Defined in:
- lib/utils/cli.rb
Overview
Mechanical work for all things CLI that does not deserve to be in the CLI helper mixin.
Constant Summary collapse
- LIST_TABLE_STYLE =
{ border_left: false, border_right: false, border_top: false, border_x: "", border_y: "", border_i: '', padding_left: 0, }.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.
-
.make_k8s_auth_bundle(run_opts) ⇒ Hash
Given the various Kubernetes authentication options and configs, generates a Hash with the necessary data/schema to pass onto the internal k8s authentication logic.
-
.run_mixers(mixer_classes, values, release_name) ⇒ List<Hash>
Convenience method for running and compiling the output of several mixers.
Instance Method Summary collapse
Class Method Details
.coerce_hash_or_array(actual, **options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/utils/cli.rb', line 23 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.
96 97 98 |
# File 'lib/utils/cli.rb', line 96 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.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/utils/cli.rb', line 42 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/utils/cli.rb', line 79 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/utils/cli.rb', line 56 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.
103 104 105 106 107 108 109 110 |
# File 'lib/utils/cli.rb', line 103 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 |
.make_k8s_auth_bundle(run_opts) ⇒ Hash
Given the various Kubernetes authentication options and configs, generates a Hash with the necessary data/schema to pass onto the internal k8s authentication logic.
This method only delegates. Actual work done is done here at: Kerbi::Utils::K8sAuth.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/utils/cli.rb', line 121 def self.make_k8s_auth_bundle(run_opts) case run_opts.k8s_auth_type when "kube-config" Kerbi::Utils::K8sAuth.kube_config_bundle( path: run_opts.kube_config_path, name: run_opts.kube_context_name ) when "basic" Kerbi::Utils::K8sAuth.basic_auth_bundle( username: run_opts.k8s_auth_username, password: run_opts.k8s_auth_password ) when "token" Kerbi::Utils::K8sAuth.token_auth_bundle( bearer_token: run_opts.k8s_auth_token, ) when "in-cluster" Kerbi::Utils::K8sAuth.in_cluster_auth_bundle else raise "Bad k8s connect type '#{run_opts.k8s_auth_type}'" end 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.
16 17 18 19 20 21 |
# File 'lib/utils/cli.rb', line 16 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 |
Instance Method Details
#resolve_remote_engine_url ⇒ Object
144 145 146 |
# File 'lib/utils/cli.rb', line 144 def resolve_remote_engine_url "" end |