Module: Gitlab::CLI::Helpers
- Extended by:
- Helpers
- Included in:
- Gitlab::CLI, Helpers, Help, Shell
- Defined in:
- lib/gitlab/cli_helpers.rb
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.
-
#gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
-
#multiple_record_table(data, cmd, args) ⇒ String
Table for multiple records.
-
#output_table(cmd, args, data) ⇒ String
Decides which table to use.
-
#required_fields(args) ⇒ Array
Returns filtered required fields.
-
#single_record_table(data, cmd, args) ⇒ String
Table for a single record.
-
#symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys.
-
#valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
-
#yaml_load_and_symbolize_hash!(args) ⇒ Array
Run YAML::load on each arg and symbolize hash keys if found.
Instance Method Details
#actions_table ⇒ String
Table with available commands.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gitlab/cli_helpers.rb', line 55 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.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gitlab/cli_helpers.rb', line 40 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.
21 22 23 24 25 26 27 |
# File 'lib/gitlab/cli_helpers.rb', line 21 def excluded_fields(args) if args.any? && args.last.is_a?(String) && args.last.start_with?('--except=') args.last.gsub('--except=', '').split(',') else [] end end |
#gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/gitlab/cli_helpers.rb', line 165 def gitlab_helper(cmd, args=[]) begin data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd) rescue => e puts e. yield if block_given? end data end |
#multiple_record_table(data, cmd, args) ⇒ String
Table for multiple records.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/gitlab/cli_helpers.rb', line 132 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 |
#output_table(cmd, args, data) ⇒ String
Decides which table to use.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gitlab/cli_helpers.rb', line 92 def output_table(cmd, args, data) case data when Gitlab::ObjectifiedHash puts single_record_table(data, cmd, args) when Array puts multiple_record_table(data, cmd, args) else puts data.inspect end end |
#required_fields(args) ⇒ Array
Returns filtered required fields.
10 11 12 13 14 15 16 |
# File 'lib/gitlab/cli_helpers.rb', line 10 def required_fields(args) if args.any? && args.last.is_a?(String) && 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/gitlab/cli_helpers.rb', line 106 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 |
#symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gitlab/cli_helpers.rb', line 178 def symbolize_keys(hash) if hash.is_a?(Hash) hash = hash.each_with_object({}) do |(key, value), newhash| begin newhash[key.to_sym] = symbolize_keys(value) rescue NoMethodError puts "error: cannot convert hash key to symbol: #{arg}" raise end end end hash end |
#valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
32 33 34 35 |
# File 'lib/gitlab/cli_helpers.rb', line 32 def valid_command?(cmd) command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym Gitlab.actions.include?(command) end |
#yaml_load_and_symbolize_hash!(args) ⇒ Array
Run YAML::load on each arg and symbolize hash keys if found.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/gitlab/cli_helpers.rb', line 195 def yaml_load_and_symbolize_hash!(args) args.map! do |arg| begin arg = YAML::load(arg) if arg.is_a?(Hash) arg = symbolize_keys(arg) end rescue Psych::SyntaxError puts "error: Argument is not valid YAML syntax: #{arg}" raise end arg end end |