Sha256: c1a60b8b82b5945c6e1a2b11e8f99e0a951784799e940b48ca85c34289fa1369

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'fileutils'

require_relative './base'
require_relative '../errors'

module RakeSecrets
  module Storage
    class FileSystem < Base
      def store(path, content)
        FileUtils.mkdir_p(File.dirname(path))
        File.write(path, content)
      rescue SystemCallError, IOError
        raise(
          RakeSecrets::Errors::StoreError,
          "Failed to store at path: '#{path}'."
        )
      end

      def remove(path)
        ensure_path_exists(path)

        File.delete(path)
      rescue SystemCallError
        raise(
          RakeSecrets::Errors::RemoveError,
          "Failed to remove from path: '#{path}'."
        )
      end

      def retrieve(path)
        ensure_path_exists(path)

        File.read(path)
      rescue SystemCallError
        raise(
          RakeSecrets::Errors::RetrieveError,
          "Failed to retrieve from path: '#{path}'."
        )
      end

      private

      def ensure_path_exists(path)
        return if File.exist?(path)

        raise(Errors::NoSuchPathError, "Path '#{path}' not in storage.")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rake_secrets-0.1.0.pre.4 lib/rake_secrets/storage/file_system.rb