Method: Gitlab::CLI::Helpers.record_hash

Defined in:
lib/gitlab/cli_helpers.rb

.record_hash(data, cmd, args, single_value: false) ⇒ Hash

Renders the result of given commands and arguments into a Hash

Parameters:

  • data (Array)

    Resultset from the API call

  • cmd (String)

    The command passed to the API

  • args (Array)

    Options passed to the API call

  • single_value (bool) (defaults to: false)

    If set to true, a single result should be returned

Returns:

  • (Hash)

    Result hash

[View source]

165
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
# File 'lib/gitlab/cli_helpers.rb', line 165

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