-
-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
307 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$:.unshift File.expand_path('../../lib', __FILE__) | ||
|
||
require 'gitlab/cli' | ||
|
||
Gitlab::CLI.start(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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) | ||
end | ||
|
||
def self.run(cmd, args=[]) | ||
case cmd | ||
when 'help' | ||
puts actions_table | ||
when '-v', '--version' | ||
puts "Gitlab Ruby Gem #{Gitlab::VERSION}" | ||
else | ||
unless Gitlab.actions.include?(cmd.to_sym) | ||
puts "Unknown command. Run `gitlab help` for a list of available commands." | ||
exit(1) | ||
end | ||
|
||
if args.any? && (args.last.start_with?('--only=') || args.last.start_with?('--except=')) | ||
command_args = args[0..-2] | ||
else | ||
command_args = args | ||
end | ||
|
||
confirm_command(cmd) | ||
|
||
begin | ||
data = args.any? ? Gitlab.send(cmd, *command_args) : Gitlab.send(cmd) | ||
rescue => e | ||
puts e.message | ||
exit(1) | ||
end | ||
|
||
case data | ||
when Gitlab::ObjectifiedHash | ||
puts single_record_table(data, cmd, args) | ||
when Array | ||
puts multiple_record_table(data, cmd, args) | ||
else | ||
puts data.inspect | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
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 | ||
|
||
# Confirms command with a desctructive action. | ||
# | ||
# @return [String] | ||
def confirm_command(cmd) | ||
if cmd.start_with?('remove_') || cmd.start_with?('delete_') | ||
puts "Are you sure? (y/n)" | ||
if %w(y yes).include?($stdin.gets.to_s.strip.downcase) | ||
puts 'Proceeding..' | ||
else | ||
puts 'Command aborted.' | ||
exit(1) | ||
end | ||
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 = 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 | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
module Gitlab | ||
# Wrapper for the Gitlab REST API. | ||
class Client < API | ||
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f} | ||
Dir[File.expand_path('../client/*.rb', __FILE__)].each {|f| require f} | ||
|
||
include SystemHooks | ||
include Users | ||
include Branches | ||
include Groups | ||
include Issues | ||
include Notes | ||
include MergeRequests | ||
include Milestones | ||
include Snippets | ||
include Notes | ||
include Projects | ||
include Repositories | ||
include Branches | ||
include MergeRequests | ||
include Groups | ||
include Snippets | ||
include SystemHooks | ||
include Users | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'spec_helper' | ||
|
||
describe Gitlab::CLI do | ||
describe ".run" do | ||
context "when command is version" do | ||
it "should show gem version" do | ||
output = capture_output { Gitlab::CLI.run('-v') } | ||
expect(output).to eq("Gitlab Ruby Gem #{Gitlab::VERSION}\n") | ||
end | ||
end | ||
|
||
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('MergeRequests') | ||
expect(output).to include('team_members') | ||
end | ||
end | ||
|
||
context "when command is user" do | ||
before do | ||
stub_get("/user", "user") | ||
@output = capture_output { Gitlab::CLI.run('user') } | ||
end | ||
|
||
it "should show executed command" do | ||
expect(@output).to include('Gitlab.user') | ||
end | ||
|
||
it "should show user data" do | ||
expect(@output).to include('name') | ||
expect(@output).to include('John Smith') | ||
end | ||
end | ||
end | ||
|
||
describe ".start" do | ||
context "when command with excluded fields" do | ||
before do | ||
stub_get("/user", "user") | ||
args = ['user', '--except=id,email,name'] | ||
@output = capture_output { Gitlab::CLI.start(args) } | ||
end | ||
|
||
it "should show user data with excluded fields" do | ||
expect(@output).to_not include('John Smith') | ||
expect(@output).to include('bio') | ||
expect(@output).to include('created_at') | ||
end | ||
end | ||
|
||
context "when command with required fields" do | ||
before do | ||
stub_get("/user", "user") | ||
args = ['user', '--only=id,email,name'] | ||
@output = capture_output { Gitlab::CLI.start(args) } | ||
end | ||
|
||
it "should show user data with required fields" do | ||
expect(@output).to include('id') | ||
expect(@output).to include('name') | ||
expect(@output).to include('email') | ||
expect(@output).to include('John Smith') | ||
expect(@output).to_not include('bio') | ||
expect(@output).to_not include('created_at') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters