module Gitlab::CLI::Helpers

Defines methods related to CLI output and formatting.

Public Instance Methods

actions() click to toggle source

Returns actions available to CLI & Shell

@return [Array]

# File lib/gitlab/cli_helpers.rb, line 13
def actions
  @actions ||= Gitlab.actions
end
client() click to toggle source

Returns Gitlab::Client instance

@return [Gitlab::Client]

# File lib/gitlab/cli_helpers.rb, line 20
def client
  @client ||= Gitlab::Client.new(endpoint: (Gitlab.endpoint || ''))
end
confirm_command(cmd) click to toggle source

Confirms command with a desctructive action.

@return [String]

# File lib/gitlab/cli_helpers.rb, line 69
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) click to toggle source

Returns filtered excluded fields.

@return [Array]

# File lib/gitlab/cli_helpers.rb, line 50
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
get_keys(args, data) click to toggle source

Helper function to get rows and keys from data returned from API call

# File lib/gitlab/cli_helpers.rb, line 195
def get_keys(args, data)
  arr = data.map(&:to_h)
  keys = arr.first.keys.sort { |x, y| x.to_s <=> y.to_s }
  keys &= required_fields(args) if required_fields(args).any?
  keys -= excluded_fields(args)
  [arr, keys]
end
gitlab_helper(cmd, args=[]) { || ... } click to toggle source

Helper function to call Gitlab commands with args.

# File lib/gitlab/cli_helpers.rb, line 204
def gitlab_helper(cmd, args=[])
  begin
    data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd)
  rescue => e
    puts e.message
    yield if block_given?
  end

  data
end
help(cmd=nil, &block) click to toggle source

Gets defined help for a specific command/action.

@return [String]

# File lib/gitlab/cli_helpers.rb, line 84
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
method_owners() click to toggle source

Returns method names and their owners

@return [Array<Hash>]

# File lib/gitlab/cli_helpers.rb, line 27
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) click to toggle source
# File lib/gitlab/cli_helpers.rb, line 104
def output_json(cmd, args, data)
  if data.empty?
    puts '{}'
  else
    hash_result = case data
                  when Gitlab::ObjectifiedHash,Gitlab::FileResponse
                    record_hash([data], cmd, args, 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) click to toggle source

Outputs a nicely formatted table or error msg.

# File lib/gitlab/cli_helpers.rb, line 93
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 msg
    puts data
  end
end
record_hash(data, cmd, args, single_value=false) click to toggle source

Renders the result of given commands and arguments into a Hash

@param [Array] data Resultset from the API call @param [String] cmd The command passed to the API @param [Array] args Options passed to the API call @param [bool] single_value If set to true, a single result should be returned @return [Hash] Result hash

# File lib/gitlab/cli_helpers.rb, line 161
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|
        case hash[key]
        when Hash
          row[key] = 'Hash'
        when StringIO
          row[key] = Base64.encode64(hash[key].read)
        when nil
          row[key] = nil
        else
          row[key] = hash[key]
        end
      end

      result.push row
    end
    result = result[0] if single_value && result.count > 0
  end

  {
    cmd: "Gitlab.#{cmd} #{args.join(', ')}".strip,
    result: result
  }
end
record_table(data, cmd, args) click to toggle source

Table to display records.

@return [Terminal::Table]

# File lib/gitlab/cli_helpers.rb, line 123
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.has_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) click to toggle source

Returns filtered required fields.

@return [Array]

# File lib/gitlab/cli_helpers.rb, line 39
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
symbolize_keys(hash) click to toggle source

Convert a hash (recursively) to use symbol hash keys @return [Hash]

# File lib/gitlab/cli_helpers.rb, line 217
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
        raise "error: cannot convert hash key to symbol: #{key}"
      end
    end
  end

  hash
end
valid_command?(cmd) click to toggle source

Confirms command is valid.

@return [Boolean]

# File lib/gitlab/cli_helpers.rb, line 61
def valid_command?(cmd)
  command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym
  Gitlab.actions.include?(command)
end
yaml_load(arg) click to toggle source

YAML::load on a single argument

# File lib/gitlab/cli_helpers.rb, line 232
def yaml_load(arg)
  begin
    yaml = YAML.load(arg)
  rescue Psych::SyntaxError
    raise "error: Argument is not valid YAML syntax: #{arg}"
  end
  yaml
end