Class: Gitlab::Shell

Inherits:
Object
  • Object
show all
Extended by:
CLI::Helpers
Defined in:
lib/gitlab/shell.rb,
lib/gitlab/shell_history.rb

Defined Under Namespace

Classes: History

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from CLI::Helpers

actions, client, confirm_command, excluded_fields, gitlab_helper, help, method_owners, output_table, record_table, required_fields, symbolize_keys, valid_command?, yaml_load_arguments!

Class Attribute Details

.argumentsObject (readonly)

Returns the value of attribute arguments.



12
13
14
# File 'lib/gitlab/shell.rb', line 12

def arguments
  @arguments
end

.commandObject (readonly)

Returns the value of attribute command.



12
13
14
# File 'lib/gitlab/shell.rb', line 12

def command
  @command
end

Class Method Details

.completionObject

Gets called when user hits TAB key to do completion



61
62
63
# File 'lib/gitlab/shell.rb', line 61

def completion
  proc { |str| actions.map(&:to_s).grep(/^#{Regexp.escape(str)}/) }
end

.execute(cmd = command, args = arguments) ⇒ Object

Execute a given command with arguements



66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/shell.rb', line 66

def execute(cmd = command, args = arguments)
  if actions.include?(cmd.to_sym)
    confirm_command(cmd)
    gitlab_helper(cmd, args)
  else
    raise "Unknown command: #{cmd}. " +
          "See the 'help' for a list of valid commands."
  end
end

.historyObject



81
82
83
# File 'lib/gitlab/shell.rb', line 81

def history
  @history ||= History.new
end

.parse_input(buffer) ⇒ Object



46
47
48
49
50
51
# File 'lib/gitlab/shell.rb', line 46

def parse_input(buffer)
  buf = Shellwords.shellwords(buffer)

  @command = buf.shift
  @arguments = buf.count > 0 ? buf : []
end

.quit_shellObject



76
77
78
79
# File 'lib/gitlab/shell.rb', line 76

def quit_shell
  history.save
  exit
end

.setupObject



53
54
55
56
57
58
# File 'lib/gitlab/shell.rb', line 53

def setup
  history.load

  Readline.completion_proc = completion
  Readline.completion_append_character = ' '
end

.startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/shell.rb', line 14

def start
  trap('INT') { quit_shell } # capture ctrl-c
  setup

  while buffer = Readline.readline('gitlab> ')
    begin
      parse_input buffer

      yaml_load_arguments! @arguments
      @arguments.map! { |arg| symbolize_keys arg }

      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

  quit_shell # save history if user presses ctrl-d
end