File: cli.rb

package info (click to toggle)
ruby-gitlab 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,660 kB
  • sloc: ruby: 12,582; makefile: 7; sh: 4
file content (89 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
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
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

require 'gitlab'
require 'terminal-table/import'
require_relative 'cli_helpers'
require_relative 'shell'

class Gitlab::CLI
  extend Helpers

  # Starts a new CLI session.
  #
  # @example
  #   Gitlab::CLI.start(['help'])
  #   Gitlab::CLI.start(['help', 'issues'])
  #
  # @param [Array] args The command and it's optional arguments.
  def self.start(args)
    command = begin
      args.shift.strip
    rescue StandardError
      'help'
    end
    run(command, args)
  end

  # Processes a CLI command and outputs a result to the stream (stdout).
  #
  # @example
  #   Gitlab::CLI.run('help')
  #   Gitlab::CLI.run('help', ['issues'])
  #
  # @param [String] cmd The name of a command.
  # @param [Array] args The optional arguments for a command.
  # @return [nil]
  def self.run(cmd, args = [])
    case cmd
    when 'help'
      puts help(args.shift) { |out| out.gsub!(/Gitlab\./, 'gitlab ') }
    when 'info'
      endpoint = Gitlab.endpoint || 'not set'
      private_token = Gitlab.private_token || 'not set'
      puts "Gitlab endpoint is #{endpoint}"
      puts "Gitlab private token is #{private_token}"
      puts "Ruby Version is #{RUBY_VERSION}"
      puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
    when '-v', '--version'
      puts "Gitlab Ruby Gem #{Gitlab::VERSION}"
    when 'shell'
      Gitlab::Shell.start
    else
      if args.include? '--json'
        @json_output = true
        args.delete '--json'
      end

      unless valid_command?(cmd)
        puts 'Unknown command. Run `gitlab help` for a list of available commands.'
        exit(0) if ENV['CI'] # FIXME: workaround to exit with 0 on passed specs
        exit(1)
      end

      command_args = args.any? && args.last.start_with?('--only=', '--except=') ? args[0..-2] : args

      begin
        command_args.map! { |arg| symbolize_keys(yaml_load(arg)) }
      rescue StandardError => e
        puts e.message
        exit 1
      end

      confirm_command(cmd)

      data = gitlab_helper(cmd, command_args) { exit(1) }

      render_output(cmd, args, data)
    end
  end

  # Helper method that checks whether we want to get the output as json
  # @return [nil]
  def self.render_output(cmd, args, data)
    if defined?(@json_output) && @json_output
      output_json(cmd, args, data)
    else
      output_table(cmd, args, data)
    end
  end
end