Sha256: 45f94a841e120c167f905641be279d49480dbc382ec7b91d394ead6f72d2414c

Contents?: true

Size: 1.34 KB

Versions: 22

Compression:

Stored size: 1.34 KB

Contents

require "clamp"

module Swa
  module CLI

    module DataOutput

      extend Clamp::Option::Declaration

      option "--format", "FORMAT", "format for data output",
             :attribute_name => :output_format,
             :environment_variable => "SWA_OUTPUT_FORMAT",
             :default => "YAML"

      option ["--json", "-J"], :flag, "output data in JSON format" do
        self.output_format = "JSON"
      end

      option ["--yaml", "-Y"], :flag, "output data in YAML format" do
        self.output_format = "YAML"
      end

      def output_format=(arg)
        arg = arg.upcase
        unless %w(JSON YAML).member?(arg)
          raise ArgumentError, "unrecognised data format: #{arg.inspect}"
        end
        @output_format = arg
      end

      protected

      def format_data(data)
        case output_format
        when "JSON"
          MultiJson.dump(data, :pretty => true)
        when "YAML"
          YAML.dump(data)
        else
          raise "bad output format: #{output_format}"
        end
      end

      def display_data(data, jmespath_expression = nil)
        unless jmespath_expression.nil?
          data = JMESPath.search(jmespath_expression, data)
        end
        puts format_data(data)
      rescue JMESPath::Errors::SyntaxError => e
        signal_error("invalid JMESPath expression")
      end

    end

  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
swa-0.8.3 lib/swa/cli/data_output.rb
swa-0.8.2 lib/swa/cli/data_output.rb
swa-0.8.1 lib/swa/cli/data_output.rb
swa-0.8.0 lib/swa/cli/data_output.rb
swa-0.7.7 lib/swa/cli/data_output.rb
swa-0.7.6 lib/swa/cli/data_output.rb
swa-0.7.5 lib/swa/cli/data_output.rb
swa-0.7.4 lib/swa/cli/data_output.rb
swa-0.7.3 lib/swa/cli/data_output.rb
swa-0.7.2 lib/swa/cli/data_output.rb
swa-0.7.1 lib/swa/cli/data_output.rb
swa-0.7.0 lib/swa/cli/data_output.rb
swa-0.6.1 lib/swa/cli/data_output.rb
swa-0.6.0 lib/swa/cli/data_output.rb
swa-0.5.2 lib/swa/cli/data_output.rb
swa-0.5.1 lib/swa/cli/data_output.rb
swa-0.5.0 lib/swa/cli/data_output.rb
swa-0.4.4 lib/swa/cli/data_output.rb
swa-0.4.3 lib/swa/cli/data_output.rb
swa-0.4.2 lib/swa/cli/data_output.rb