lib/packer/binary/executable.rb in packer-binary-0.1.1 vs lib/packer/binary/executable.rb in packer-binary-0.2.0

- old
+ new

@@ -1,39 +1,57 @@ module Packer module Binary + # This class handles downloading, extracting, and saving the associated + # binary file class Executable include Packer::Binary::Helpers + # Raised when the current platform is unsupported + class PlatformUnsupported < StandardError; end + + # The download URI for the binary package + PACKER_DOWNLOAD_URI = 'releases.hashicorp.com'.freeze + + # Loads global config values and creates the download directory if the + # current platform is supported def initialize @packer_version = Packer::Binary.config.version @download_path = "#{Packer::Binary.config.download_path.chomp('/')}/packer-binary/#{@packer_version}/bin" @download_filename = "#{@packer_version}-packer.zip" FileUtils.mkdir_p @download_path - raise 'Your OS is not supported' unless supported? + raise PlatformUnsupported unless supported? end + # Returns the path to the binary if it exists + # @return [String] absolute path of the binary def binary Dir["#{@download_path}/packer*"][0] end + # Downloads the zipfile from the origin def download - return "#{binary} already exists" if binary_exist? + return if binary_exist? msg("Downloading https://#{download_domain}/#{download_uri}") require 'open-uri' - File.open("#{@download_path}/#{@download_filename}", "wb") do |saved_file| + File.open("#{@download_path}/#{@download_filename}", 'wb') do |saved_file| # the following "open" is provided by open-uri - open("https://#{download_domain}/#{download_uri}", "rb") do |read_file| + open("https://#{download_domain}/#{download_uri}", 'rb') do |read_file| saved_file.write(read_file.read) end end + end - extract + # Extracts the binary from the zipfile and makes it executable + def extract + return if binary_exist? + + Compressor.extract("#{@download_path}/#{@download_filename}", @download_path) make_exe end private @@ -47,23 +65,18 @@ def make_exe FileUtils.chmod('a+x', binary) if binary_exist? end - def extract - Compressor::extract("#{@download_path}/#{@download_filename}", @download_path) - end - def download_uri "packer/#{@packer_version}/packer_#{@packer_version}_freebsd_amd64.zip" if OS.freebsd? "packer/#{@packer_version}/packer_#{@packer_version}_darwin_amd64.zip" if OS.mac? "packer/#{@packer_version}/packer_#{@packer_version}_windows_amd64.zip" if OS.windows? "packer/#{@packer_version}/packer_#{@packer_version}_linux_amd64.zip" if OS.linux? end def download_domain - 'releases.hashicorp.com' + PACKER_DOWNLOAD_URI end - end end end