Sha256: a42e8c897ddf62d4f38e3a952a463932a835204319ff26e882d544f8cd3f5dfc

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

require 'fileutils'
require 'zip'

require_relative 'download'
require_relative 'os'
require_relative 'command'

module TerraformDevKit
  class TerraformInstaller
    LOCAL_FILE_NAME = 'terraform.zip'.freeze

    def self.installed_terraform_version
      version = Command.run('terraform --version')[0]
      match = /Terraform v(\d+\.\d+\.\d+)/.match(version)
      match[1] unless match.nil?
    rescue
      nil
    end

    def self.install_local(version, directory: Dir.pwd)
      if installed_terraform_version == version
        puts 'Terraform already installed'
        return
      end

      FileUtils.mkdir_p(directory)
      Dir.chdir(directory) do
        download_terraform(version)
        unzip_terraform
      end
    end

    private_class_method
    def self.download_terraform(version)
      TerraformDevKit.download_file(
        "https://releases.hashicorp.com/terraform/#{version}/terraform_#{version}_#{OS.host_os}_amd64.zip",
        LOCAL_FILE_NAME,
        force_download: true
      )
    end

    private_class_method
    def self.unzip_terraform
      Zip::File.open(LOCAL_FILE_NAME) do |zip_file|
        zip_file.each do |entry|
          puts "Extracting #{entry.name}"
          entry.restore_permissions = true
          entry.extract { true }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
TerraformDevKit-0.1.13 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.12 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.11 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.10 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.9 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.8 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.7 lib/TerraformDevKit/terraform_installer.rb
TerraformDevKit-0.1.6 lib/TerraformDevKit/terraform_installer.rb