Sha256: 396bd95d112dc08febe0da9b59e32aac24144b2c284e0faa7ae16cc4bfda9feb

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

# frozen_string_literal: true

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

        prepare
      end

      def save
        Kubernetes.secret.each do |secret|
          downloaded = @items[secret.name]
          secret.files.each do |(key, path)|
            write path, Base64.decode64(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(
          Utils::Path.with_stage(path),
          body
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kubec-0.6.1 lib/kubec/secret/downloader.rb
kubec-0.6.0 lib/kubec/secret/downloader.rb
kubec-0.5.0 lib/kubec/secret/downloader.rb