Sha256: 192e13f32ff79ba50864d7c1483119e3f34e57bc9d9729540afc28b57f020d51

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Kubec
  module Config
    # :nodoc:
    class Downloader
      def initialize
        @result = `kubectl -n #{fetch(:stage, :staging)} get configmap -o json`
        @success = $CHILD_STATUS.success?
        @items = {}

        prepare
      end

      def save
        Kubernetes.config.each do |config|
          downloaded = @items[config.name]
          config.files.each do |(key, path)|
            write path, downloaded.dig('data', key)
          end
        end
      end

      private

      def prepare
        return unless @success
        items = JSON.parse(@result).dig('items') || []
        items.each do |item|
          name = item.dig('metadata', 'name').to_sym
          @items[name] = item
        end
      end

      def write(path, body)
        puts "=> #{path} saved"
        File.write(
          path_with_stage(path),
          body
        )
      end

      # TODO: Refactor
      def path_with_stage(path)
        path.split('.').tap do |ary|
          ext = ary.pop unless ary.first.empty?
          ary.push(fetch(:stage, :staging))
          ary.push(ext) if ext
        end.join('.')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kubec-0.4.1 lib/kubec/config/downloader.rb
kubec-0.4.0 lib/kubec/config/downloader.rb