Module: Gitlab::CLI::Helpers
Overview
Defines methods related to CLI output and formatting.
Instance Method Summary collapse
-
#actions_table ⇒ String
Table with available commands.
-
#confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
-
#excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
-
#multiple_record_table(data, cmd, args) ⇒ String
Table for multiple records.
-
#required_fields(args) ⇒ Array
Returns filtered required fields.
-
#single_record_table(data, cmd, args) ⇒ String
Table for a single record.
Instance Method Details
#actions_table ⇒ String
Table with available commands.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gitlab/cli_helpers.rb', line 46 def actions_table client = Gitlab::Client.new(endpoint: '') actions = Gitlab.actions methods = [] actions.each do |action| methods << { name: action, owner: client.method(action).owner.to_s.gsub('Gitlab::Client::', '') } end owners = methods.map {|m| m[:owner]}.uniq.sort methods_c = methods.group_by {|m| m[:owner]} methods_c = methods_c.map {|_, v| [_, v.sort_by {|hv| hv[:name]}] } methods_c = Hash[methods_c.sort_by(&:first).map {|k, v| [k, v]}] max_column_length = methods_c.values.max_by(&:size).size rows = max_column_length.times.map do |i| methods_c.keys.map do |key| methods_c[key][i] ? methods_c[key][i][:name] : '' end end table do |t| t.title = "Available commands (#{actions.size} total)" t.headings = owners rows.each do |row| t.add_row row end end end |
#confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gitlab/cli_helpers.rb', line 31 def confirm_command(cmd) if cmd.start_with?('remove_') || cmd.start_with?('delete_') puts "Are you sure? (y/n)" if %w(y yes).include?($stdin.gets.to_s.strip.downcase) puts 'Proceeding..' else puts 'Command aborted.' exit(1) end end end |
#excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
20 21 22 23 24 25 26 |
# File 'lib/gitlab/cli_helpers.rb', line 20 def excluded_fields(args) if args.any? && args.last.start_with?('--except=') args.last.gsub('--except=', '').split(',') else [] end end |
#multiple_record_table(data, cmd, args) ⇒ String
Table for multiple records.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/gitlab/cli_helpers.rb', line 109 def multiple_record_table(data, cmd, args) return 'No data' if data.empty? arr = data.map(&:to_h) keys = arr.first.keys.sort {|x, y| x.to_s <=> y.to_s } keys = keys & required_fields(args) if required_fields(args).any? keys = keys - excluded_fields(args) table do |t| t.title = "Gitlab.#{cmd} #{args.join(', ')}" t.headings = keys arr.each_with_index do |hash, index| values = [] keys.each do |key| case value = hash[key] when Hash value = 'Hash' when nil value = 'null' end values << value end t.add_row values t.add_separator unless arr.size - 1 == index end end end |
#required_fields(args) ⇒ Array
Returns filtered required fields.
9 10 11 12 13 14 15 |
# File 'lib/gitlab/cli_helpers.rb', line 9 def required_fields(args) if args.any? && args.last.start_with?('--only=') args.last.gsub('--only=', '').split(',') else [] end end |
#single_record_table(data, cmd, args) ⇒ String
Table for a single record.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gitlab/cli_helpers.rb', line 83 def single_record_table(data, cmd, args) hash = data.to_h keys = hash.keys.sort {|x, y| x.to_s <=> y.to_s } keys = keys & required_fields(args) if required_fields(args).any? keys = keys - excluded_fields(args) table do |t| t.title = "Gitlab.#{cmd} #{args.join(', ')}" keys.each_with_index do |key, index| case value = hash[key] when Hash value = 'Hash' when nil value = 'null' end t.add_row [key, value] t.add_separator unless keys.size - 1 == index end end end |