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 Method Summary collapse

Methods included from CLI::Helpers

actions_table, confirm_command, excluded_fields, gitlab_helper, multiple_record_table, output_table, required_fields, single_record_table, symbolize_keys, valid_command?, yaml_load_and_symbolize_hash!

Class Method Details

.historyObject



84
85
86
# File 'lib/gitlab/shell.rb', line 84

def self.history
  @history ||= History.new
end

.quit_shellObject



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

def self.quit_shell
  history.save
  exit
end

.startObject

Start gitlab shell and run infinite loop waiting for user input



12
13
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/shell.rb', line 12

def self.start
  history.load
  actions = Gitlab.actions

  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> ')
    trap('INT') { quit_shell } # capture ctrl-c

    next if buf.nil? || buf.empty?
    quit_shell if buf == 'exit'

    history << buf

    begin
      buf = Shellwords.shellwords(buf)
    rescue ArgumentError => e
      puts e.message
      next
    end

    cmd = buf.shift
    args = buf.count > 0 ? buf : []

    if cmd == 'help'
      methods = []

      actions.each do |action|
        methods << {
          name: action.to_s,
          owner: client.method(action).owner.to_s
        }
      end

      args[0].nil? ? Gitlab::Help.get_help(methods) :
                     Gitlab::Help.get_help(methods, args[0])
      next
    end

    syntax_errors = false

    begin
      yaml_load_and_symbolize_hash!(args)
    rescue
      syntax_errors = true
    end

    # errors have been displayed, return to the prompt
    next if syntax_errors 

    data = 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."
    end

    output_table(cmd, args, data)
  end
end