Skip to content

Commit

Permalink
ability to filter CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed May 16, 2014
1 parent c77944e commit 5ec08bc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def self.run(cmd, arguments=[])
else
if Gitlab.actions.include?(cmd.to_sym)
begin
data = arguments.any? ? Gitlab.send(cmd.to_sym, *arguments) : Gitlab.send(cmd.to_sym)
if arguments.any?
if arguments.last.start_with?('--only=') || arguments.last.start_with?('--except=')
command_args = arguments[0..-2]
end
end

data = arguments.any? ? Gitlab.send(cmd.to_sym, *command_args) : Gitlab.send(cmd.to_sym)

if data.kind_of? Gitlab::ObjectifiedHash
puts single_record_table(data, cmd, arguments)
elsif data.kind_of? Array
Expand All @@ -35,11 +42,29 @@ def self.run(cmd, arguments=[])
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(', ')}"
Expand Down Expand Up @@ -68,6 +93,8 @@ def self.multiple_record_table(data, cmd, args)
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(', ')}"
Expand Down

0 comments on commit 5ec08bc

Please sign in to comment.