Sha256: d0030b4fdbe65a6143fe39f11c8b0f54130588455942834c51063abb991a5790

Contents?: true

Size: 1.53 KB

Versions: 14

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require 'json'
require 'yaml'
require 'csv'

module KubernetesDeploy
  module BindingsParser
    extend self

    def parse(string)
      bindings = parse_file(string) || parse_json(string) || parse_csv(string)

      unless bindings
        raise ArgumentError, "Failed to parse bindings."
      end

      bindings
    end

    private

    def parse_file(string)
      return unless string =~ /\A@/

      begin
        file_path = string.gsub(/\A@/, '')

        case File.extname(file_path)
        when '.json'
          bindings = parse_json(File.read(file_path))
        when '.yaml', '.yml'
          bindings = YAML.load(File.read(file_path))
        else
          raise ArgumentError, "Supplied file does not appear to be JSON or YAML"
        end

        bindings
      rescue Errno::ENOENT
        raise ArgumentError, "Supplied file does not exist: #{string}"
      end
    end

    def parse_json(string)
      bindings = JSON.parse(string)

      unless bindings.is_a?(Hash)
        raise ArgumentError, "Expected JSON data to be a hash."
      end

      bindings
    rescue JSON::ParserError
      nil
    end

    def parse_csv(string)
      lines = CSV.parse(string)
      bindings = {}

      lines.each do |line|
        line.each do |binding|
          key, value = binding.split('=', 2)

          if key.blank?
            raise ArgumentError, "key is blank"
          end

          bindings[key] = value
        end
      end

      bindings
    rescue CSV::MalformedCSVError
      nil
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kubernetes-deploy-0.25.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.24.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.23.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.22.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.21.1 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.21.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.6 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.5 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.4 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.3 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.2 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.1 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.20.0 lib/kubernetes-deploy/bindings_parser.rb
kubernetes-deploy-0.19.0 lib/kubernetes-deploy/bindings_parser.rb