Module: Gitlab::CLI::Helpers
- Included in:
- Gitlab::CLI, Help, Shell
- Defined in:
- lib/gitlab/cli_helpers.rb
Overview
Defines methods related to CLI output and formatting.
Class Method Summary collapse
-
.actions ⇒ Array
Returns actions available to CLI & Shell.
-
.client ⇒ Gitlab::Client
Returns Gitlab::Client instance.
-
.confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
-
.excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
-
.filtered_fields(args, key) ⇒ Array
Returns fields filtered by a keyword.
-
.get_keys(args, data) ⇒ Object
Helper function to get rows and keys from data returned from API call.
-
.gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
-
.help(cmd = nil, &block) ⇒ String
Gets defined help for a specific command/action.
-
.hex_color?(arg) ⇒ Boolean
Check if arg is a color in 6-digit hex notation with leading ‘#’ sign.
-
.method_owners ⇒ Array<Hash>
Returns method names and their owners.
- .output_json(cmd, args, data) ⇒ Object
-
.output_table(cmd, args, data) ⇒ Object
Outputs a nicely formatted table or error message.
-
.record_hash(data, cmd, args, single_value: false) ⇒ Hash
Renders the result of given commands and arguments into a Hash.
-
.record_table(data, cmd, args) ⇒ Terminal::Table
Table to display records.
-
.required_fields(args) ⇒ Array
Returns filtered required fields.
-
.symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys.
-
.valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
-
.yaml_load(arg) ⇒ Object
YAML::load on a single argument.
Class Method Details
.actions ⇒ Array
Returns actions available to CLI & Shell
16 17 18 |
# File 'lib/gitlab/cli_helpers.rb', line 16 def actions @actions ||= Gitlab.actions end |
.client ⇒ Gitlab::Client
Returns Gitlab::Client instance
23 24 25 |
# File 'lib/gitlab/cli_helpers.rb', line 23 def client @client ||= Gitlab::Client.new(endpoint: Gitlab.endpoint || '') end |
.confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gitlab/cli_helpers.rb', line 73 def confirm_command(cmd) return unless cmd.start_with?('remove_', '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 |
.excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
49 50 51 |
# File 'lib/gitlab/cli_helpers.rb', line 49 def excluded_fields(args) filtered_fields(args, '--except=') end |
.filtered_fields(args, key) ⇒ Array
Returns fields filtered by a keyword.
56 57 58 59 60 |
# File 'lib/gitlab/cli_helpers.rb', line 56 def filtered_fields(args, key) return [] unless args.any? && args.last.is_a?(String) && args.last.start_with?(key) args.last.gsub(key, '').split(',') end |
.get_keys(args, data) ⇒ Object
Helper function to get rows and keys from data returned from API call
200 201 202 203 204 205 206 |
# File 'lib/gitlab/cli_helpers.rb', line 200 def get_keys(args, data) arr = data.map(&:to_h) keys = arr.first.keys.sort_by(&:to_s) keys &= required_fields(args) if required_fields(args).any? keys -= excluded_fields(args) [arr, keys] end |
.gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
209 210 211 212 213 214 |
# File 'lib/gitlab/cli_helpers.rb', line 209 def gitlab_helper(cmd, args = []) args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd) rescue StandardError => e puts e. yield if block_given? end |
.help(cmd = nil, &block) ⇒ String
Gets defined help for a specific command/action.
89 90 91 92 93 94 95 |
# File 'lib/gitlab/cli_helpers.rb', line 89 def help(cmd = nil, &block) if cmd.nil? || Gitlab::Help.help_map.key?(cmd) Gitlab::Help.actions_table(cmd) else Gitlab::Help.get_help(cmd, &block) end end |
.hex_color?(arg) ⇒ Boolean
Check if arg is a color in 6-digit hex notation with leading ‘#’ sign
231 232 233 234 235 |
# File 'lib/gitlab/cli_helpers.rb', line 231 def hex_color?(arg) pattern = /\A#\h{6}\Z/ pattern.match(arg) end |
.method_owners ⇒ Array<Hash>
Returns method names and their owners
30 31 32 33 34 35 36 37 |
# File 'lib/gitlab/cli_helpers.rb', line 30 def method_owners @method_owners ||= actions.map do |action| { name: action.to_s, owner: client.method(action).owner.to_s } end end |
.output_json(cmd, args, data) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gitlab/cli_helpers.rb', line 109 def output_json(cmd, args, data) if data.respond_to?(:empty?) && data.empty? puts '{}' else hash_result = case data when Gitlab::ObjectifiedHash, Gitlab::FileResponse record_hash([data], cmd, args, single_value: true) when Gitlab::PaginatedResponse record_hash(data, cmd, args) else { cmd: cmd, data: data, args: args } end puts JSON.pretty_generate(hash_result) end end |
.output_table(cmd, args, data) ⇒ Object
Outputs a nicely formatted table or error message.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gitlab/cli_helpers.rb', line 98 def output_table(cmd, args, data) case data when Gitlab::ObjectifiedHash, Gitlab::FileResponse puts record_table([data], cmd, args) when Gitlab::PaginatedResponse puts record_table(data, cmd, args) else # probably just an error message puts data end end |
.record_hash(data, cmd, args, single_value: false) ⇒ Hash
Renders the result of given commands and arguments into a Hash
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/gitlab/cli_helpers.rb', line 166 def record_hash(data, cmd, args, single_value: false) if data.empty? result = nil else arr, keys = get_keys(args, data) result = [] arr.each do |hash| row = {} keys.each do |key| row[key] = case hash[key] when Hash 'Hash' when StringIO Base64.encode64(hash[key].read) when nil nil else hash[key] end end result.push row end result = result[0] if single_value && result.count.positive? end { cmd: "Gitlab.#{cmd} #{args.join(', ')}".strip, result: result } end |
.record_table(data, cmd, args) ⇒ Terminal::Table
Table to display records.
128 129 130 131 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 |
# File 'lib/gitlab/cli_helpers.rb', line 128 def record_table(data, cmd, args) return 'No data' if data.empty? arr, keys = get_keys(args, data) 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 = value.key?('id') ? value['id'] : 'Hash' when StringIO value = 'File' 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.
42 43 44 |
# File 'lib/gitlab/cli_helpers.rb', line 42 def required_fields(args) filtered_fields(args, '--only=') end |
.symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/gitlab/cli_helpers.rb', line 218 def symbolize_keys(hash) if hash.is_a?(Hash) hash = hash.each_with_object({}) do |(key, value), new_hash| new_hash[key.to_sym] = symbolize_keys(value) rescue NoMethodError raise "Error: cannot convert hash key to symbol: #{key}" end end hash end |
.valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
65 66 67 68 |
# File 'lib/gitlab/cli_helpers.rb', line 65 def valid_command?(cmd) command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym Gitlab.actions.include?(command) end |
.yaml_load(arg) ⇒ Object
YAML::load on a single argument
238 239 240 241 242 |
# File 'lib/gitlab/cli_helpers.rb', line 238 def yaml_load(arg) hex_color?(arg) ? arg : YAML.safe_load(arg) rescue Psych::SyntaxError raise "Error: Argument is not valid YAML syntax: #{arg}" end |