Module: Gitlab::Help

Extended by:
CLI::Helpers
Defined in:
lib/gitlab/help.rb

Class Method Summary collapse

Methods included from CLI::Helpers

actions, client, confirm_command, excluded_fields, get_keys, gitlab_helper, help, method_owners, output_json, output_table, record_hash, record_table, required_fields, symbolize_keys, valid_command?, yaml_load

Class Method Details

.actions_table(topic = nil) ⇒ Terminal::Table

Table with available commands.

Returns:

  • (Terminal::Table)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gitlab/help.rb', line 61

def actions_table(topic=nil)
  rows = topic ? help_map[topic] : help_map.keys
  table do |t|
    t.title = topic || 'Help Topics'

    # add_row expects an array and we have strings hence the map.
    rows.sort.map { |r| [r] }.each_with_index do |row, index|
      t.add_row row
      t.add_separator unless rows.size - 1 == index
    end
  end
end

.change_help_output!(cmd, output_str) ⇒ Object

Massage output from ‘ri’.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gitlab/help.rb', line 82

def change_help_output!(cmd, output_str)
  output_str.gsub!(/#{cmd}\((.*?)\)/m, cmd + ' \1')
  output_str.gsub!(/\,[\s]*/, ' ')

  # Ensure @option descriptions are on a single line
  output_str.gsub!(/\n\[/, " \[")
  output_str.gsub!(/\s(@)/, "\n@")
  output_str.gsub!(/(\])\n(\:)/, '\1 \2')
  output_str.gsub!(/(\:.*)(\n)(.*\.)/, '\1 \3')
  output_str.gsub!(/\{(.+)\}/, '"{\1}"')
end

.get_help(cmd) ⇒ String

Returns the (modified) help from the ‘ri’ command or returns an error.

Returns:

  • (String)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gitlab/help.rb', line 11

def get_help(cmd)
  cmd_namespace = namespace cmd

  if cmd_namespace
    ri_output = `#{ri_cmd} -T #{cmd_namespace} 2>&1`.chomp

    if $CHILD_STATUS == 0
      change_help_output! cmd, ri_output
      yield ri_output if block_given?

      ri_output
    else
      "Ri docs not found for #{cmd}, please install the docs to use 'help'."
    end
  else
    "Unknown command: #{cmd}."
  end
end

.help_mapHash<Array>

A hash map that contains help topics (Branches, Groups, etc.) and a list of commands that are defined under a topic (create_branch, branches, protect_branch, etc.).

Returns:

  • (Hash<Array>)


47
48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/help.rb', line 47

def help_map
  @help_map ||= begin
    actions.each_with_object({}) do |action, hsh|
      key = client.method(action).
            owner.to_s.gsub(/Gitlab::(?:Client::)?/, '')
      hsh[key] ||= []
      hsh[key] << action.to_s
    end
  end
end

.namespace(cmd) ⇒ Object

Returns full namespace of a command (e.g. Gitlab::Client::Branches.cmd)



75
76
77
78
79
# File 'lib/gitlab/help.rb', line 75

def namespace(cmd)
  method_owners.select { |method| method[:name] == cmd }.
    map { |method| method[:owner] + '.' + method[:name] }.
    shift
end

.ri_cmdString

Finds the location of ‘ri’ on a system.

Returns:

  • (String)


33
34
35
36
37
38
39
40
# File 'lib/gitlab/help.rb', line 33

def ri_cmd
  which_ri = `which ri`.chomp
  if which_ri.empty?
    raise "'ri' tool not found in $PATH. Please install it to use the help."
  end

  which_ri
end