lib/kumogata/client.rb in kumogata-0.4.9 vs lib/kumogata/client.rb in kumogata-0.4.10

- old
+ new

@@ -27,15 +27,27 @@ true end def convert(path_or_url) template = open_template(path_or_url) + output_format = @options.output_format - if ruby_template?(path_or_url) - JSON.pretty_generate(template).colorize_as(:json) - else + unless output_format + output_format = case guess_format(path_or_url) + when :ruby then :json + when :json then :ruby + when :yaml then :json + end + end + + case output_format + when :ruby devaluate_template(template).chomp.colorize_as(:ruby) + when :json + JSON.pretty_generate(template).colorize_as(:json) + when :yaml + YAML.dump(template).colorize_as(:yaml) end end def update(path_or_url, stack_name) validate_stack_name(stack_name) @@ -129,25 +141,37 @@ end private ########################################################### def open_template(path_or_url) - format = @options.format || (ruby_template?(path_or_url) ? :ruby : :json) + format = @options.format || guess_format(path_or_url) open(path_or_url) do |f| case format when :ruby evaluate_template(f, path_or_url) when :json JSON.parse(f.read) + when :yaml + parsed = YAML.load(f.read) + Kumogata::Utils.stringify(parsed) else raise "Unknown format: #{format}" end end end - def ruby_template?(path_or_url) - File.extname(path_or_url) == '.rb' + def guess_format(path_or_url) + case File.extname(path_or_url) + when '.rb' + :ruby + when '.json', '.js' + :json + when '.yml', '.yaml' + :yaml + else + :json + end end def evaluate_template(template, path_or_url) key_converter = proc do |key| key = key.to_s