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, filtered_fields, get_keys, gitlab_helper, help, hex_color?, method_owners, output_json, output_table, record_hash, record_table, required_fields, symbolize_keys, valid_command?, yaml_load

Class Attribute Details

.argumentsObject (readonly)

Returns the value of attribute arguments.



14
15
16
# File 'lib/gitlab/shell.rb', line 14

def arguments
  @arguments
end

.commandObject (readonly)

Returns the value of attribute command.



14
15
16
# File 'lib/gitlab/shell.rb', line 14

def command
  @command
end

Class Method Details

.completionObject

Gets called when user hits TAB key to do completion



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

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



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

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

  confirm_command(cmd)
  gitlab_helper(cmd, args)
end

.historyObject



79
80
81
# File 'lib/gitlab/shell.rb', line 79

def history
  @history ||= History.new
end

.parse_input(buffer) ⇒ Object



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

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

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

.quit_shellObject



74
75
76
77
# File 'lib/gitlab/shell.rb', line 74

def quit_shell
  history.save
  exit
end

.setupObject



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

def setup
  history.load

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

.startObject



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
45
# File 'lib/gitlab/shell.rb', line 16

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

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

      @arguments.map! { |arg| symbolize_keys(yaml_load(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 StandardError => e
      puts e.message
    end
  end

  quit_shell # save history if user presses ctrl-d
end