Skip to content

Commit

Permalink
improve help message
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed May 20, 2014
1 parent 04c377a commit 4183a52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 36 additions & 4 deletions lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ def self.start(args)
def self.run(cmd, args=[])
case cmd
when 'help'
puts 'Available commands:'
puts '-' * 19
puts Gitlab.actions.sort.join(', ')
puts actions_table
when '-v', '--version'
puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
else
unless Gitlab.actions.include?(cmd.to_sym)
puts 'Unknown command'
puts "Unknown command. Run `gitlab help` for a list of available commands."
exit(1)
end

Expand All @@ -40,6 +38,40 @@ def self.run(cmd, args=[])
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(',')
Expand Down
2 changes: 1 addition & 1 deletion spec/gitlab/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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('Available commands')
end
end

Expand Down

0 comments on commit 4183a52

Please sign in to comment.