lib/cloudstack-cli/base.rb in cloudstack-cli-0.14.1 vs lib/cloudstack-cli/base.rb in cloudstack-cli-0.15.0

- old
+ new

@@ -1,7 +1,8 @@ require "thor" require "yaml" +require "open-uri" module CloudstackCli class Base < Thor include Thor::Actions include CloudstackCli::Helper @@ -72,9 +73,31 @@ project end def filter_by(objects, key, value) objects.select {|r| r[key].downcase == value.downcase} + end + + def parse_file(file, extensions = %w(.json .yaml .yml)) + handler = case File.extname(file) + when ".json" + 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 + end + begin + return handler.load open(file){|f| f.read} + rescue SystemCallError + say "Can't find the file #{file}.", :red + exit 1 + rescue => e + say "Error parsing #{File.extname(file)} file:", :red + say e.message + exit 1 + end end end end end