Sha256: 41ac4a95d687db46ad8c9a97c701ae32196b5f3097321c2e3cf6041e5b60897c

Contents?: true

Size: 907 Bytes

Versions: 1

Compression:

Stored size: 907 Bytes

Contents

module Gorynich
  module Fetchers
    class ConsulSecure
      attr_reader :storage, :file_path, :consul_opts

      def initialize(storage:, file_path:, **opts)
        @storage = storage
        @file_path = file_path
        @consul_opts = opts
      end

      def fetch
        cfg = Consul.new(storage: storage, **consul_opts).fetch
        return from_file if cfg.empty?

        save_to_file(cfg)

        cfg
      rescue ::StandardError
        from_file
      end

      private

      def save_to_file(cfg)
        envs = ::Dir.glob(::Rails.root.join('config/environments/*.rb').to_s).map { |f| ::File.basename(f, '.rb') }

        ::File.open(file_path, 'w') do |f|
          f << cfg.deep_transform_keys(&:downcase).select { |k, _v| envs.include?(k) }.to_yaml.gsub(/^---/, '')
        end
      end

      def from_file
        File.new(file_path: file_path).fetch
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gorynich-1.3.4.273062 lib/gorynich/fetchers/consul_secure.rb