Skip to content

Commit

Permalink
Redo the actions_table to make it more readable. CLI can now display …
Browse files Browse the repository at this point in the history
…the same help as the Shell. Closes #106.
  • Loading branch information
asedge committed Feb 20, 2015
1 parent da18909 commit 2133562
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.start(args)
def self.run(cmd, args=[])
case cmd
when 'help'
puts actions_table
puts help(args.shift) { |out| out.gsub!(/Gitlab\./, 'gitlab ') }
when 'info'
endpoint = Gitlab.endpoint ? Gitlab.endpoint : 'not set'
private_token = Gitlab.private_token ? Gitlab.private_token : 'not set'
Expand Down
29 changes: 7 additions & 22 deletions lib/gitlab/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,14 @@ def confirm_command(cmd)
end
end

# Table with available commands.
# Gets defined help for a specific command/action.
#
# @return [Terminal::Table]
def actions_table
owners = method_owners.map {|m| m[:owner].gsub(/Gitlab::(?:Client::)?/,'')}.uniq.sort
methods_c = method_owners.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
# @return [String]
def help(cmd = nil, &block)
if cmd.nil? or Gitlab::Help.help_map.has_key?(cmd)
Gitlab::Help.actions_table(cmd)
else
Gitlab::Help.get_help(cmd, &block)
end
end

Expand Down
42 changes: 36 additions & 6 deletions lib/gitlab/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def get_help(cmd)

if $? == 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'."
Expand All @@ -30,14 +32,44 @@ def get_help(cmd)
#
# @return [String]
def ri_cmd
@ri_cmd if @ri_cmd

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

which_ri
end

# 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.).
#
# @return [Hash<Array>]
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

# Table with available commands.
#
# @return [Terminal::Table]
def actions_table(topic = nil)
rows = topic ? help_map[topic] : help_map.keys
table do |t|
t.title = topic || "Help Topics"

@ri_cmd = which_ri
# 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

# Returns full namespace of a command (e.g. Gitlab::Client::Branches.cmd)
Expand All @@ -50,8 +82,6 @@ def namespace(cmd)
# Massage output from 'ri'.
def change_help_output!(cmd, output_str)
output_str.gsub!(/#{cmd}\((.*?)\)/m, cmd+' \1')
output_str.gsub!(/Gitlab\./, 'gitlab> ')
output_str.gsub!(/Gitlab\..+$/, '')
output_str.gsub!(/\,[\s]*/, ' ')
end

Expand Down
6 changes: 1 addition & 5 deletions lib/gitlab/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def start
when 'exit'
quit_shell
when /^\bhelp\b+/
puts help arguments[0]
puts help(arguments[0]) { |out| out.gsub!(/Gitlab\./, 'gitlab> ') }
else
history << buffer

Expand Down Expand Up @@ -63,10 +63,6 @@ def completion
proc { |str| actions.map(&:to_s).grep(/^#{Regexp.escape(str)}/) }
end

def help(cmd = nil)
cmd.nil? ? actions_table : Gitlab::Help.get_help(cmd)
end

# Execute a given command with arguements
def execute(cmd = command, args = arguments)
if actions.include?(cmd.to_sym)
Expand Down
3 changes: 1 addition & 2 deletions spec/gitlab/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
context "when command is help" do
it "should show available actions" do
output = capture_output { Gitlab::CLI.run('help') }
expect(output).to include('Available commands')
expect(output).to include('Help Topics')
expect(output).to include('MergeRequests')
expect(output).to include('team_members')
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/gitlab/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end
it "should return a String of modified output" do
Gitlab::Help.change_help_output! @cmd, @help_output
expect(@help_output).to eq("gitlab> create_branch 4 'new-branch' 'master'")
expect(@help_output).to eq("Gitlab.create_branch 4 'new-branch' 'master'")
end
end

Expand Down

0 comments on commit 2133562

Please sign in to comment.