# frozen_string_literal: true require 'yaml' module Diffend # Module for all the components related to setting up the config module Config # Class responsible for fetching the config from .diffend.yml module Fetcher # All the errors for missing keys in the configuration file MISSING_KEY_ERRORS = [ Errors::ProjectIdMissingInConfigurationFile, Errors::ShareableIdMissingInConfigurationFile, Errors::ShareableKeyMissingInConfigurationFile, Errors::BuildPathMissingInConfigurationFile ].freeze class << self # @param build_path [String] path of the current build # # @return [OpenStruct] open struct with config details # # @example # details = Fetcher.new.call('./') # details.build_path #=> './' def call(build_path) build(build_path) rescue Errors::MissingConfigurationFile Bundler.ui.error(build_missing_error_message(build_path)) raise Diffend::Errors::HandledException rescue Errors::EmptyConfigurationFile Bundler.ui.error(build_empty_error_message(build_path)) raise Diffend::Errors::HandledException rescue Errors::MalformedConfigurationFile Bundler.ui.error(build_malformed_error_message(build_path)) raise Diffend::Errors::HandledException rescue *MISSING_KEY_ERRORS => e Bundler.ui.error(build_missing_key_error_message(e)) raise Diffend::Errors::HandledException end private # @param build_path [String] path of the current build # # @return [OpenStruct] open struct with config details def build(build_path) content = ERB.new( File.read( FileFinder.call(build_path) ) ).result raise Errors::EmptyConfigurationFile if content.empty? OpenStruct.new( parse_file(content) .merge(build_path: build_path) .merge(diffend_env: ENV['DIFFEND_ENV'] || 'development') ) .tap(&Validator.method(:call)) end def parse_file(content) YAML.safe_load(content) rescue Psych::SyntaxError raise Errors::MalformedConfigurationFile end # @param build_path [String] path of the current build # # @return [String] missing configuration file message def build_missing_error_message(build_path) <<~MSG \nWe were unable to locate Diffend configuration file.\n Please make sure that .diffend.yml is present in #{build_path} folder.\n MSG end # @return [String] empty configuration file message def build_empty_error_message <<~MSG \nYour Diffend configuration file is empty.\n Please re-setup.\n MSG end # @return [String] malformed configuration file message def build_malformed_error_message <<~MSG \nYour Diffend configuration file is malformed.\n Please re-setup.\n MSG end # @return [String] malformed configuration file message def build_missing_key_error_message(exception) missing_key = missing_key_from_exception(exception) <<~MSG \nYour Diffend configuration file is missing #{missing_key} key.\n Please re-setup.\n MSG end def missing_key_from_exception(exception) case exception when Errors::ProjectIdMissingInConfigurationFile then 'project_id' when Errors::ShareableIdMissingInConfigurationFile then 'shareable_id' when Errors::ShareableKeyMissingInConfigurationFile then 'shareable_key' when Errors::BuildPathMissingInConfigurationFile then 'build_path' end end end end end end