lib/gitlab/shell.rb in gitlab-3.3.0 vs lib/gitlab/shell.rb in gitlab-3.4.0
- old
+ new
@@ -6,82 +6,81 @@
require 'shellwords'
class Gitlab::Shell
extend Gitlab::CLI::Helpers
- # Start gitlab shell and run infinite loop waiting for user input
- def self.start
- history.load
- actions = Gitlab.actions
+ class << self
+ attr_reader :arguments, :command
- comp = proc { |s| actions.map(&:to_s).grep(/^#{Regexp.escape(s)}/) }
-
- Readline.completion_proc = comp
- Readline.completion_append_character = ' '
-
- client = Gitlab::Client.new(endpoint: '')
-
- while buf = Readline.readline('gitlab> ')
+ def start
trap('INT') { quit_shell } # capture ctrl-c
+ setup
- next if buf.nil? || buf.empty?
- quit_shell if buf == 'exit'
+ while buffer = Readline.readline('gitlab> ')
+ begin
+ parse_input buffer
- history << buf
+ yaml_load_arguments! @arguments
+ @arguments.map! { |arg| symbolize_keys arg }
- begin
- buf = Shellwords.shellwords(buf)
- rescue ArgumentError => e
- puts e.message
- next
+ case buffer
+ when nil, ''
+ next
+ when 'exit'
+ quit_shell
+ when /^\bhelp\b+/
+ puts help(arguments[0]) { |out| out.gsub!(/Gitlab\./, 'gitlab> ') }
+ else
+ history << buffer
+
+ data = execute command, arguments
+ output_table command, arguments, data
+ end
+ rescue => e
+ puts e.message
+ end
end
- cmd = buf.shift
- args = buf.count > 0 ? buf : []
+ quit_shell # save history if user presses ctrl-d
+ end
- if cmd == 'help'
- methods = []
+ def parse_input(buffer)
+ buf = Shellwords.shellwords(buffer)
- actions.each do |action|
- methods << {
- name: action.to_s,
- owner: client.method(action).owner.to_s
- }
- end
+ @command = buf.shift
+ @arguments = buf.count > 0 ? buf : []
+ end
- args[0].nil? ? Gitlab::Help.get_help(methods) :
- Gitlab::Help.get_help(methods, args[0])
- next
- end
+ def setup
+ history.load
- syntax_errors = false
+ Readline.completion_proc = completion
+ Readline.completion_append_character = ' '
+ end
- begin
- yaml_load_and_symbolize_hash!(args)
- rescue
- syntax_errors = true
- end
+ # Gets called when user hits TAB key to do completion
+ def completion
+ proc { |str| actions.map(&:to_s).grep(/^#{Regexp.escape(str)}/) }
+ end
- # errors have been displayed, return to the prompt
- next if syntax_errors
-
- data = if actions.include?(cmd.to_sym)
+ # Execute a given command with arguements
+ def execute(cmd = command, args = arguments)
+ if actions.include?(cmd.to_sym)
confirm_command(cmd)
gitlab_helper(cmd, args)
else
- "'#{cmd}' is not a valid command. " +
- "See the 'help' for a list of valid commands."
+ raise "Unknown command: #{cmd}. " +
+ "See the 'help' for a list of valid commands."
end
+ end
- output_table(cmd, args, data)
+ def quit_shell
+ history.save
+ exit
end
- end
- def self.quit_shell
- history.save
- exit
- end
+ def history
+ @history ||= History.new
+ end
- def self.history
- @history ||= History.new
- end
+ end # class << self
end