lib/cloudstack-cli/base.rb in cloudstack-cli-1.3.3 vs lib/cloudstack-cli/base.rb in cloudstack-cli-1.4.0

- old
+ new

@@ -1,7 +1,8 @@ require "thor" require "cloudstack-cli/thor_patch" +require "json" require "yaml" require "open-uri" module CloudstackCli class Base < Thor @@ -77,16 +78,37 @@ end config end def filter_by(objects, key, value) - objects.select {|r| r[key.to_s].downcase == value.downcase} + if objects.size < 2 + return objects + elsif !(objects.first.has_key? key) + keys = objects.first.keys.join(", ") + say "WARNING: Filter invalid, no key \"#{key}\" found.", :yellow + say("DEBUG: Supported keys are, #{keys}.", :magenta) if options[:debug] + return objects + end + objects.select do |object| + object[key.to_s] =~ /#{value}/i + end + rescue RegexpError => e + say "ERROR: Invalid regular expression in filter - #{e.message}", :red + exit 1 end + def filter_objects(objects, filter = options[:filter]) + filter.each do |key, value| + objects = filter_by(objects, key, value) + return objects if objects.size == 0 + end + objects + end + def parse_file(file, extensions = %w(.json .yaml .yml)) handler = case File.extname(file) when ".json" - Object.const_get "MultiJson" + Object.const_get "JSON" when ".yaml", ".yml" Object.const_get "YAML" else say "File extension #{File.extname(file)} not supported. Supported extensions are #{extensions.join(', ')}", :red exit