Skip to content

Commit

Permalink
Initial commit of Gitlab CLI shell. Tab completion for Gitlab.actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
asedge committed Jun 6, 2014
1 parent 48ba2a1 commit 4a5f81f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ gitlab user --only=id,username
gitlab user --except=email,bio
```

## CLI Shell

Usage:

```sh
[user@host ~]$ gitlab shell
# set endpoint for session
gitlab> endpoint= http://192.168.1.20/api/v3
"http://192.168.1.20/api/v3"
# set private token for session
gitlab> private_token= Z78CpekqasoWahskVcVr
"Z78CpekqasoWahskVcVr"
# list groups
gitlab> groups
# protect a branch
gitlab> protect_branch 1 master
```

For more information, refer to [website](http://narkoz.github.io/gitlab).

## License
Expand Down
20 changes: 5 additions & 15 deletions lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'gitlab'
require 'terminal-table/import'
require_relative 'cli_helpers'
require_relative 'shell'

class Gitlab::CLI
extend Helpers
Expand All @@ -23,6 +24,8 @@ def self.run(cmd, args=[])
puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
when '-v', '--version'
puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
when 'shell'
Gitlab::Shell.start
else
unless Gitlab.actions.include?(cmd.to_sym)
puts "Unknown command. Run `gitlab help` for a list of available commands."
Expand All @@ -37,21 +40,8 @@ def self.run(cmd, args=[])

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
data = gitlab_helper(cmd,command_args) { exit(1) }
output_table(data)
end
end
end
27 changes: 27 additions & 0 deletions lib/gitlab/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ def actions_table
end
end

# Decides which table to use.
#
# @return [String]

def output_table(cmd, args, data)
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

# Table for a single record.
#
# @return [String]
Expand Down Expand Up @@ -137,5 +153,16 @@ def multiple_record_table(data, cmd, args)
end
end
end

# Helper function to call Gitlab commands, w/ 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?
end
data
end
end
end
29 changes: 29 additions & 0 deletions lib/gitlab/shell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'gitlab'
require "readline"
require_relative 'cli_helpers'

class Gitlab::Shell
extend Gitlab::CLI::Helpers

def self.start
actions = Gitlab.actions.map(&:to_s)
actions << 'endpoint=' << 'private_token='

comp = proc { |s| actions.grep(/^#{Regexp.escape(s)}/) }

Readline.completion_append_character = " "
Readline.completion_proc = comp

while buf = Readline.readline("gitlab> ", true)
next if buf.nil? || buf.empty?
buf = buf.split.map(&:chomp)
cmd = buf.shift
args = buf.count > 0 ? buf : []
confirm_command(cmd)

data = gitlab_helper(cmd,args)

output_table(cmd, args, data)
end
end
end

0 comments on commit 4a5f81f

Please sign in to comment.