Sha256: ebc23dbecdfb70ef523b3396088aecb26f688908b91054459f8091578290776d

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

module Berkshelf
  class CookbookSource
    # @author Jamie Winsor <jamie@vialstudios.com>
    class GitLocation
      include Location

      attr_accessor :uri
      attr_accessor :branch

      def initialize(name, options)
        @name = name
        @uri = options[:git]
        @branch = options[:branch] || options[:ref] || options[:tag]
      end

      def download(destination)
        tmp_clone = Dir.mktmpdir
        ::Berkshelf::Git.clone(uri, tmp_clone)
        ::Berkshelf::Git.checkout(tmp_clone, branch) if branch
        unless branch
          self.branch = ::Berkshelf::Git.rev_parse(tmp_clone)
        end

        unless File.chef_cookbook?(tmp_clone)
          msg = "Cookbook '#{name}' not found at git: #{uri}" 
          msg << " with branch '#{branch}'" if branch
          raise CookbookNotFound, msg
        end

        cb_path = File.join(destination, "#{self.name}-#{self.branch}")

        FileUtils.mv(tmp_clone, cb_path, :force => true)

        cb_path
      rescue Berkshelf::GitError
        msg = "Cookbook '#{name}' not found at git: #{uri}" 
        msg << " with branch '#{branch}'" if branch
        raise CookbookNotFound, msg
      end

      def to_s
        s = "git: '#{uri}'"
        s << " with branch '#{branch}'" if branch
        s
      end

      private

        def git
          @git ||= Berkshelf::Git.new(uri)
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
berkshelf-0.3.3 lib/berkshelf/cookbook_source/git_location.rb
berkshelf-0.3.2 lib/berkshelf/cookbook_source/git_location.rb
berkshelf-0.3.1 lib/berkshelf/cookbook_source/git_location.rb
berkshelf-0.3.0 lib/berkshelf/cookbook_source/git_location.rb
berkshelf-0.2.0 lib/berkshelf/cookbook_source/git_location.rb