Sha256: fb3f25daed6e5ed4f058434a58dd98bf8e711831334849d2b449f0466201f21c

Contents?: true

Size: 1.42 KB

Versions: 31

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'psych'
require 'pathname'
require 'base64'

class EnvVariablesService
  class << self
    def substitute_env_variables(compose_file_data)
      compose_file_data.gsub(/\${1,2}\{?([?:\-_A-Za-z0-9]+)\}?/) do |variable|
        next variable if variable.start_with?('$$')

        variable_content = variable.match(/[?:\-_A-Za-z0-9]+/).to_s
        fetch_variable_value(variable_content)
      end
    end

    private

    def fetch_variable_value(variable_content)
      variable_name = variable_content.match(/^[_A-Za-z0-9]+/).to_s
      variable_value = ENV[variable_name]
      return variable_value unless variable_value.nil?
      return fetch_variable_default_value(variable_content) if variable_has_default_value?(variable_content)

      error_message = if variable_has_error_message?(variable_content)
        fetch_env_error_message(variable_content)
      else
        "Environment variable #{variable_name} doesn't exist"
      end
      raise Uffizzi::Error.new(error_message)
    end

    def variable_has_default_value?(variable_content)
      variable_content.include?('-')
    end

    def fetch_variable_default_value(variable_content)
      variable_content.split('-', 2).last
    end

    def variable_has_error_message?(variable_content)
      variable_content.include?('?')
    end

    def fetch_env_error_message(variable_content)
      variable_content.split('?', 2).last
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
uffizzi-cli-2.4.11 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.10 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.9 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.8 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.7 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.6 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.5 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.4 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.3 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.2 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.1 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.4.0 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.3.4 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.3.3 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.3.2 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.3.1 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.3.0 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.2.2 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.2.1 lib/uffizzi/services/env_variables_service.rb
uffizzi-cli-2.2.0 lib/uffizzi/services/env_variables_service.rb