Module: Gitlab::CLI::Helpers
- Extended by:
- Helpers
- Included in:
- Gitlab::CLI, Helpers, Help, Shell
- Defined in:
- lib/gitlab/cli_helpers.rb
Overview
Defines methods related to CLI output and formatting.
Instance Method Summary collapse
-
#actions ⇒ Array
Returns actions available to CLI & Shell.
-
#client ⇒ Gitlab::Client
Returns Gitlab::Client instance.
-
#confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
-
#excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
-
#gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
-
#help(cmd = nil, &block) ⇒ String
Gets defined help for a specific command/action.
-
#method_owners ⇒ Array<Hash>
Returns method names and their owners.
-
#output_table(cmd, args, data) ⇒ Object
Outputs a nicely formatted table or error msg.
-
#record_table(data, cmd, args) ⇒ Terminal::Table
Table to display records.
-
#required_fields(args) ⇒ Array
Returns filtered required fields.
-
#symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys.
-
#valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
-
#yaml_load_arguments!(args) ⇒ Array
Run YAML::load on each arg.
Instance Method Details
#actions ⇒ Array
Returns actions available to CLI & Shell
11 12 13 |
# File 'lib/gitlab/cli_helpers.rb', line 11 def actions @actions ||= Gitlab.actions end |
#client ⇒ Gitlab::Client
Returns Gitlab::Client instance
18 19 20 |
# File 'lib/gitlab/cli_helpers.rb', line 18 def client @client ||= Gitlab::Client.new(endpoint: (Gitlab.endpoint || '')) end |
#confirm_command(cmd) ⇒ String
Confirms command with a desctructive action.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gitlab/cli_helpers.rb', line 67 def confirm_command(cmd) if cmd.start_with?('remove_') || cmd.start_with?('delete_') puts "Are you sure? (y/n)" if %w(y yes).include?($stdin.gets.to_s.strip.downcase) puts 'Proceeding..' else puts 'Command aborted.' exit(1) end end end |
#excluded_fields(args) ⇒ Array
Returns filtered excluded fields.
48 49 50 51 52 53 54 |
# File 'lib/gitlab/cli_helpers.rb', line 48 def excluded_fields(args) if args.any? && args.last.is_a?(String) && args.last.start_with?('--except=') args.last.gsub('--except=', '').split(',') else [] end end |
#gitlab_helper(cmd, args = []) ⇒ Object
Helper function to call Gitlab commands with args.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gitlab/cli_helpers.rb', line 138 def gitlab_helper(cmd, args = []) begin data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd) rescue => e puts e. yield if block_given? end data end |
#help(cmd = nil, &block) ⇒ String
Gets defined help for a specific command/action.
82 83 84 85 86 87 88 |
# File 'lib/gitlab/cli_helpers.rb', line 82 def help(cmd = nil, &block) if cmd.nil? or Gitlab::Help.help_map.has_key?(cmd) Gitlab::Help.actions_table(cmd) else Gitlab::Help.get_help(cmd, &block) end end |
#method_owners ⇒ Array<Hash>
Returns method names and their owners
25 26 27 28 29 30 31 32 |
# File 'lib/gitlab/cli_helpers.rb', line 25 def method_owners @method_owners ||= actions.map do |action| { name: action.to_s, owner: client.method(action).owner.to_s } end end |
#output_table(cmd, args, data) ⇒ Object
Outputs a nicely formatted table or error msg.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gitlab/cli_helpers.rb', line 91 def output_table(cmd, args, data) case data when Gitlab::ObjectifiedHash puts record_table([data], cmd, args) when Array puts record_table(data, cmd, args) else # probably just an error msg puts data end end |
#record_table(data, cmd, args) ⇒ Terminal::Table
Table to display records.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gitlab/cli_helpers.rb', line 105 def record_table(data, cmd, args) return 'No data' if data.empty? arr = data.map(&:to_h) keys = arr.first.keys.sort {|x, y| x.to_s <=> y.to_s } keys = keys & required_fields(args) if required_fields(args).any? keys = keys - excluded_fields(args) table do |t| t.title = "Gitlab.#{cmd} #{args.join(', ')}" t.headings = keys arr.each_with_index do |hash, index| values = [] keys.each do |key| case value = hash[key] when Hash value = 'Hash' when nil value = 'null' end values << value end t.add_row values t.add_separator unless arr.size - 1 == index end end end |
#required_fields(args) ⇒ Array
Returns filtered required fields.
37 38 39 40 41 42 43 |
# File 'lib/gitlab/cli_helpers.rb', line 37 def required_fields(args) if args.any? && args.last.is_a?(String) && args.last.start_with?('--only=') args.last.gsub('--only=', '').split(',') else [] end end |
#symbolize_keys(hash) ⇒ Hash
Convert a hash (recursively) to use symbol hash keys
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gitlab/cli_helpers.rb', line 151 def symbolize_keys(hash) if hash.is_a?(Hash) hash = hash.each_with_object({}) do |(key, value), newhash| begin newhash[key.to_sym] = symbolize_keys(value) rescue NoMethodError raise "error: cannot convert hash key to symbol: #{key}" end end end hash end |
#valid_command?(cmd) ⇒ Boolean
Confirms command is valid.
59 60 61 62 |
# File 'lib/gitlab/cli_helpers.rb', line 59 def valid_command?(cmd) command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym Gitlab.actions.include?(command) end |
#yaml_load_arguments!(args) ⇒ Array
Run YAML::load on each arg.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/gitlab/cli_helpers.rb', line 167 def yaml_load_arguments!(args) args.map! do |arg| begin arg = YAML::load(arg) rescue Psych::SyntaxError raise "error: Argument is not valid YAML syntax: #{arg}" end arg end end |