Sha256: c1774942a333cf0a3f793a450cce178e4720b13520a2dd444fcc6209ed7e73c2

Contents?: true

Size: 1007 Bytes

Versions: 3

Compression:

Stored size: 1007 Bytes

Contents

module Berkshelf
  module Vagrant
    module Action
      # @author Jamie Winsor <reset@riotgames.com>
      class LoadShelf
        include Berkshelf::Vagrant::EnvHelpers

        def initialize(app, env)
          @app = app
        end

        def call(env)
          unless berkshelf_enabled?(env)
            return @app.call(env)
          end

          shelf = load_shelf

          if shelf.nil?
            shelf = cache_shelf(Berkshelf::Vagrant.mkshelf)
          end

          env[:berkshelf].shelf = shelf

          @app.call(env)
        end

        # @param [String] path
        #
        # @return [String]
        def cache_shelf(path)
          FileUtils.mkdir_p(File.dirname(path))

          File.open(cache_file, 'w+') do |f|
            f.write(path)
          end

          path
        end

        # @return [String, nil]
        def load_shelf
          return nil unless File.exist?(cache_file)

          File.read(cache_file).chomp
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
vagrant-berkshelf-1.2.0 lib/berkshelf/vagrant/action/load_shelf.rb
berkshelf-vagrant-1.1.2 lib/berkshelf/vagrant/action/load_shelf.rb
berkshelf-vagrant-1.1.0 lib/berkshelf/vagrant/action/load_shelf.rb