From 21335623009553197b61826d9894a739de152665 Mon Sep 17 00:00:00 2001 From: Sean Edge Date: Tue, 17 Feb 2015 18:59:52 -0500 Subject: [PATCH] Redo the actions_table to make it more readable. CLI can now display the same help as the Shell. Closes #106. --- lib/gitlab/cli.rb | 2 +- lib/gitlab/cli_helpers.rb | 29 +++++++-------------------- lib/gitlab/help.rb | 42 +++++++++++++++++++++++++++++++++------ lib/gitlab/shell.rb | 6 +----- spec/gitlab/cli_spec.rb | 3 +-- spec/gitlab/help_spec.rb | 2 +- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/lib/gitlab/cli.rb b/lib/gitlab/cli.rb index ef949debf..e06eeaf42 100644 --- a/lib/gitlab/cli.rb +++ b/lib/gitlab/cli.rb @@ -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' diff --git a/lib/gitlab/cli_helpers.rb b/lib/gitlab/cli_helpers.rb index dc42d05ab..b2aef0d67 100644 --- a/lib/gitlab/cli_helpers.rb +++ b/lib/gitlab/cli_helpers.rb @@ -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 diff --git a/lib/gitlab/help.rb b/lib/gitlab/help.rb index 83c528279..9a6e97b13 100644 --- a/lib/gitlab/help.rb +++ b/lib/gitlab/help.rb @@ -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'." @@ -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] + 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) @@ -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 diff --git a/lib/gitlab/shell.rb b/lib/gitlab/shell.rb index c781af4a3..39c06d0e2 100644 --- a/lib/gitlab/shell.rb +++ b/lib/gitlab/shell.rb @@ -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 @@ -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) diff --git a/spec/gitlab/cli_spec.rb b/spec/gitlab/cli_spec.rb index 8b002ec64..e8452ff6c 100644 --- a/spec/gitlab/cli_spec.rb +++ b/spec/gitlab/cli_spec.rb @@ -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 diff --git a/spec/gitlab/help_spec.rb b/spec/gitlab/help_spec.rb index 63ec8e1dc..d6f778bd8 100644 --- a/spec/gitlab/help_spec.rb +++ b/spec/gitlab/help_spec.rb @@ -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