Sha256: 370127bd940245759faa08bb314b93765a8ef51ca0190c3018992018a61b6a00

Contents?: true

Size: 1.74 KB

Versions: 20

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

20 entries across 20 versions & 1 rubygems

Version Path
shopify-cli-2.4.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.3.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.2.2 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.2.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.2.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.1.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.0.2 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.0.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-2.0.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.14.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.13.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.13.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.12.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.11.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.10.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.9.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.9.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.8.0 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.7.1 lib/project_types/extension/features/argo_config.rb
shopify-cli-1.7.0 lib/project_types/extension/features/argo_config.rb