lib/gitlab/cli_helpers.rb in gitlab-3.3.0 vs lib/gitlab/cli_helpers.rb in gitlab-3.4.0

- old
+ new

@@ -1,11 +1,38 @@ require 'yaml' + class Gitlab::CLI # Defines methods related to CLI output and formatting. module Helpers extend self + # Returns actions available to CLI & Shell + # + # @return [Array] + def actions + @actions ||= Gitlab.actions + end + + # Returns Gitlab::Client instance + # + # @return [Gitlab::Client] + def client + @client ||= Gitlab::Client.new(endpoint: (Gitlab.endpoint || '')) + end + + # Returns method names and their owners + # + # @return [Array<Hash>] + def method_owners + @method_owners ||= actions.map do |action| + { + name: action.to_s, + owner: client.method(action).owner.to_s + } + end + end + # Returns filtered required fields. # # @return [Array] def required_fields(args) if args.any? && args.last.is_a?(String) && args.last.start_with?('--only=') @@ -47,91 +74,37 @@ exit(1) end end end - # Table with available commands. + # Gets defined help for a specific command/action. # # @return [String] - def actions_table - client = Gitlab::Client.new(endpoint: '') - actions = Gitlab.actions - methods = [] - - actions.each do |action| - methods << { - name: action, - owner: client.method(action).owner.to_s.gsub('Gitlab::Client::', '') - } + 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 - - owners = methods.map {|m| m[:owner]}.uniq.sort - methods_c = methods.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 - end end - # Decides which table to use. - # - # @return [String] + # Outputs a nicely formatted table or error msg. def output_table(cmd, args, data) case data when Gitlab::ObjectifiedHash - puts single_record_table(data, cmd, args) + puts record_table([data], cmd, args) when Array - puts multiple_record_table(data, cmd, args) - else - puts data.inspect + puts record_table(data, cmd, args) + else # probably just an error msg + puts data end end - # Table for a single record. + # Table to display records. # - # @return [String] - def single_record_table(data, cmd, args) - hash = data.to_h - keys = hash.keys.sort {|x, y| x.to_s <=> y.to_s } - keys = keys & required_fields(args) if required_fields(args).any? - keys = keys - excluded_fields(args) - - table do |t| - t.title = "Gitlab.#{cmd} #{args.join(', ')}" - - keys.each_with_index do |key, index| - case value = hash[key] - when Hash - value = 'Hash' - when nil - value = 'null' - end - - t.add_row [key, value] - t.add_separator unless keys.size - 1 == index - end - end - end - - # Table for multiple records. - # - # @return [String] - def multiple_record_table(data, cmd, args) + # @return [Terminal::Table] + def record_table(data, cmd, args) return 'No data' if data.empty? arr = data.map(&:to_h) keys = arr.first.keys.sort {|x, y| x.to_s <=> y.to_s } keys = keys & required_fields(args) if required_fields(args).any? @@ -160,11 +133,11 @@ end end end # Helper function to call Gitlab commands with args. - def gitlab_helper(cmd, args=[]) + 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? @@ -179,31 +152,25 @@ if hash.is_a?(Hash) hash = hash.each_with_object({}) do |(key, value), newhash| begin newhash[key.to_sym] = symbolize_keys(value) rescue NoMethodError - puts "error: cannot convert hash key to symbol: #{arg}" - raise + raise "error: cannot convert hash key to symbol: #{key}" end end end hash end - # Run YAML::load on each arg and symbolize hash keys if found. + # Run YAML::load on each arg. # @return [Array] - def yaml_load_and_symbolize_hash!(args) + def yaml_load_arguments!(args) args.map! do |arg| begin arg = YAML::load(arg) - - if arg.is_a?(Hash) - arg = symbolize_keys(arg) - end rescue Psych::SyntaxError - puts "error: Argument is not valid YAML syntax: #{arg}" - raise + raise "error: Argument is not valid YAML syntax: #{arg}" end arg end end