Sha256: 969df1e120b6d5d61c3a5401ff0372e29953c193b9c4158ad30b2e09cb29fcee

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Extension
  module Features
    class ArgoConfig
      CONFIG_FILE_NAME = 'extension.config.yml'

      class << self
        def parse_yaml(context, permitted_keys = [])
          file_name = File.join(context.root, CONFIG_FILE_NAME)

          return {} unless File.size?(file_name)

          require 'yaml' # takes 20ms, so deferred as late as possible.
          begin
            config = YAML.load_file(file_name)

            # `YAML.load_file` returns nil if the file is not empty
            # but does not contain any parsable yml data, e.g. only comments
            # We consider this valid
            return {} if config.nil?

            unless config.is_a?(Hash)
              raise ShopifyCli::Abort, ShopifyCli::Context.message('core.yaml.error.not_hash', CONFIG_FILE_NAME)
            end

            config.transform_keys!(&:to_sym)
            assert_valid_config(config, permitted_keys) unless permitted_keys.empty?

            config
          rescue Psych::SyntaxError => e
            raise(
              ShopifyCli::Abort,
              ShopifyCli::Context.message('core.yaml.error.invalid', CONFIG_FILE_NAME, e.message)
            )
          end
        end

        private

        def assert_valid_config(config, permitted_keys)
          unpermitted_keys = config.keys.select do |k|
            !permitted_keys.include?(k)
          end

          unless unpermitted_keys.empty?
            raise(
              ShopifyCli::Abort,
              ShopifyCli::Context.message(
                'features.argo.config.unpermitted_keys',
                CONFIG_FILE_NAME,
                unpermitted_keys.map { |k| "\n- #{k}" }.join
              )
            )
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.5.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.4.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.4.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.3.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.3.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.2.0 lib/project_types/extension/features/argo_config.rb