lib/gitlab/cli_helpers.rb in gitlab-4.5.0 vs lib/gitlab/cli_helpers.rb in gitlab-4.6.0

- old
+ new

@@ -1,13 +1,15 @@ +# frozen_string_literal: true + require 'yaml' require 'json' require 'base64' class Gitlab::CLI # Defines methods related to CLI output and formatting. module Helpers - extend self + module_function # Returns actions available to CLI & Shell # # @return [Array] def actions @@ -69,37 +71,37 @@ def confirm_command(cmd) return unless cmd.start_with?('remove_', 'delete_') puts 'Are you sure? (y/n)' - if %w(y yes).include?($stdin.gets.to_s.strip.downcase) + if %w[y yes].include?($stdin.gets.to_s.strip.downcase) puts 'Proceeding..' else puts 'Command aborted.' exit(1) end end # Gets defined help for a specific command/action. # # @return [String] - def help(cmd=nil, &block) + def help(cmd = nil, &block) if cmd.nil? || Gitlab::Help.help_map.key?(cmd) Gitlab::Help.actions_table(cmd) else Gitlab::Help.get_help(cmd, &block) end end - # Outputs a nicely formatted table or error msg. + # Outputs a nicely formatted table or error message. def output_table(cmd, args, data) case data when Gitlab::ObjectifiedHash, Gitlab::FileResponse puts record_table([data], cmd, args) when Gitlab::PaginatedResponse puts record_table(data, cmd, args) - else # probably just an error msg + else # probably just an error message puts data end end def output_json(cmd, args, data) @@ -157,35 +159,35 @@ # @param [Array] data Resultset from the API call # @param [String] cmd The command passed to the API # @param [Array] args Options passed to the API call # @param [bool] single_value If set to true, a single result should be returned # @return [Hash] Result hash - def record_hash(data, cmd, args, single_value=false) + def record_hash(data, cmd, args, single_value = false) if data.empty? result = nil else arr, keys = get_keys(args, data) result = [] arr.each do |hash| row = {} keys.each do |key| - case hash[key] - when Hash - row[key] = 'Hash' - when StringIO - row[key] = Base64.encode64(hash[key].read) - when nil - row[key] = nil - else - row[key] = hash[key] - end + row[key] = case hash[key] + when Hash + 'Hash' + when StringIO + Base64.encode64(hash[key].read) + when nil + nil + else + hash[key] + end end result.push row end - result = result[0] if single_value && result.count > 0 + result = result[0] if single_value && result.count.positive? end { cmd: "Gitlab.#{cmd} #{args.join(', ')}".strip, result: result @@ -200,14 +202,14 @@ keys -= excluded_fields(args) [arr, keys] end # Helper function to call Gitlab commands with args. - def gitlab_helper(cmd, args=[]) + def gitlab_helper(cmd, args = []) begin data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd) - rescue => e + rescue StandardError => e puts e.message yield if block_given? end data @@ -215,24 +217,31 @@ # Convert a hash (recursively) to use symbol hash keys # @return [Hash] def symbolize_keys(hash) if hash.is_a?(Hash) - hash = hash.each_with_object({}) do |(key, value), newhash| + hash = hash.each_with_object({}) do |(key, value), new_hash| begin - newhash[key.to_sym] = symbolize_keys(value) + new_hash[key.to_sym] = symbolize_keys(value) rescue NoMethodError raise "Error: cannot convert hash key to symbol: #{key}" end end end hash end + # Check if arg is a color in 6-digit hex notation with leading '#' sign + def hex_color?(arg) + pattern = /\A#\h{6}\Z/ + + pattern.match(arg) + end + # YAML::load on a single argument def yaml_load(arg) - YAML.safe_load(arg) + hex_color?(arg) ? arg : YAML.safe_load(arg) rescue Psych::SyntaxError raise "Error: Argument is not valid YAML syntax: #{arg}" end end end