lib/gitlab/cli_helpers.rb in gitlab-3.2.0 vs lib/gitlab/cli_helpers.rb in gitlab-3.3.0
- old
+ new
@@ -1,26 +1,27 @@
+require 'yaml'
class Gitlab::CLI
# Defines methods related to CLI output and formatting.
module Helpers
extend self
# Returns filtered required fields.
#
# @return [Array]
def required_fields(args)
- if args.any? && args.last.start_with?('--only=')
+ if args.any? && args.last.is_a?(String) && args.last.start_with?('--only=')
args.last.gsub('--only=', '').split(',')
else
[]
end
end
# Returns filtered excluded fields.
#
# @return [Array]
def excluded_fields(args)
- if args.any? && args.last.start_with?('--except=')
+ if args.any? && args.last.is_a?(String) && args.last.start_with?('--except=')
args.last.gsub('--except=', '').split(',')
else
[]
end
end
@@ -43,11 +44,11 @@
puts 'Proceeding..'
else
puts 'Command aborted.'
exit(1)
end
- end
+ end
end
# Table with available commands.
#
# @return [String]
@@ -168,8 +169,44 @@
puts e.message
yield if block_given?
end
data
+ end
+
+ # 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|
+ begin
+ newhash[key.to_sym] = symbolize_keys(value)
+ rescue NoMethodError
+ puts "error: cannot convert hash key to symbol: #{arg}"
+ raise
+ end
+ end
+ end
+
+ hash
+ end
+
+ # Run YAML::load on each arg and symbolize hash keys if found.
+ # @return [Array]
+ def yaml_load_and_symbolize_hash!(args)
+ args.map! do |arg|
+ begin
+ arg = YAML::load(arg)
+
+ if arg.is_a?(Hash)
+ arg = symbolize_keys(arg)
+ end
+ rescue Psych::SyntaxError
+ puts "error: Argument is not valid YAML syntax: #{arg}"
+ raise
+ end
+
+ arg
+ end
end
end
end