From b1602d2f5ce0a614de48d5295654d0699fe69c0d Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 21 May 2014 00:46:25 +0500 Subject: [PATCH] move methods related to CLI output to a separate module --- lib/gitlab/cli.rb | 108 +------------------------------- lib/gitlab/cli_helpers.rb | 126 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 105 deletions(-) create mode 100644 lib/gitlab/cli_helpers.rb diff --git a/lib/gitlab/cli.rb b/lib/gitlab/cli.rb index ba126b06b..4d030c183 100644 --- a/lib/gitlab/cli.rb +++ b/lib/gitlab/cli.rb @@ -1,7 +1,10 @@ require 'gitlab' require 'terminal-table/import' +require_relative 'cli_helpers' class Gitlab::CLI + extend Helpers + def self.start(args) command = args.shift.strip rescue 'help' run(command, args) @@ -37,109 +40,4 @@ def self.run(cmd, args=[]) end end end - - def self.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::', '') - } - 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 = methods_c.sort_by {|v| v.first}.to_h - max_column_length = methods_c.values.max_by {|v| v.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 - - def self.required_fields(args) - if args.any? && args.last.start_with?('--only=') - args.last.gsub('--only=', '').split(',') - else - [] - end - end - - def self.excluded_fields(args) - if args.any? && args.last.start_with?('--except=') - args.last.gsub('--except=', '').split(',') - else - [] - end - end - - def self.multiple_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? - keys = keys - excluded_fields(args) - - 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 = 'Hash' - when nil - value = 'null' - end - - values << value - end - - t.add_row values - t.add_separator unless arr.size - 1 == index - end - end - end - - def self.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 end diff --git a/lib/gitlab/cli_helpers.rb b/lib/gitlab/cli_helpers.rb new file mode 100644 index 000000000..249d9d1fc --- /dev/null +++ b/lib/gitlab/cli_helpers.rb @@ -0,0 +1,126 @@ +class Gitlab::CLI + # Defines methods related to CLI output and formatting. + module Helpers + extend self + + # Returns filtered required fields. + # + # @return [Array] + def required_fields(args) + if args.any? && args.last.start_with?('--only=') + args.last.gsub('--only=', '').split(',') + else + [] + end + end + + # Returns filtered excluded fields. + # + # @return [Array] + def excluded_fields(args) + if args.any? && args.last.start_with?('--except=') + args.last.gsub('--except=', '').split(',') + else + [] + end + end + + # Table with available commands. + # + # @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::', '') + } + 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 = methods_c.sort_by {|v| v.first}.to_h + max_column_length = methods_c.values.max_by {|v| v.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 + + # Table for a single record. + # + # @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 '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? + keys = keys - excluded_fields(args) + + 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 = 'Hash' + when nil + value = 'null' + end + + values << value + end + + t.add_row values + t.add_separator unless arr.size - 1 == index + end + end + end + end +end