Sha256: f18102aae2668ff195b10c7047add69bfa14eac2b67e99a77508c8cfb56e419a

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Terradactyl
  module Terraform
    module VersionManager
      class Defaults
        DEFAULT_INSTALL_DIR   = Gem.bindir
        DEFAULT_DOWNLOADS_URL = 'https://www.terraform.io/downloads.html'
        DEFAULT_RELEASES_URL  = 'https://releases.hashicorp.com/terraform'
        DEFAULT_VERSION       = nil

        def self.load
          new
        end

        attr_reader :version, :install_dir, :downloads_url, :releases_url

        def initialize
          load_defaults
        end

        def reset!
          load_defaults
        end

        def version=(option)
          @version = validate_semver_exp(option) || DEFAULT_VERSION
        end

        def install_dir=(option)
          @install_dir = validate_path(option) || DEFAULT_INSTALL_DIR
        end

        def downloads_url=(option)
          @downloads_url = validate_url(option) || DEFAULT_DOWNLOADS_URL
        end

        def releases_url=(option)
          @releases_url = validate_url(option) || DEFAULT_RELEASES_URL
        end

        private

        def validate_semver_exp(option)
          if (data = option.to_s.match(SEMVER_EXP_RE))
            return option if data['op']
            return option if data['semver'].match(/\d+\.\d+\.\d+(-\w+)?/)
          end

          nil
        end

        def validate_path(option)
          return nil if option.to_s.empty?

          dir = File.expand_path(option)
          Dir.exist?(dir) ? dir : nil
        end

        def validate_url(option)
          return nil if option.to_s.empty?

          uri = URI.parse(option)
          url if uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
        end

        def load_defaults
          @install_dir   = DEFAULT_INSTALL_DIR
          @downloads_url = DEFAULT_DOWNLOADS_URL
          @releases_url  = DEFAULT_RELEASES_URL
          @version       = DEFAULT_VERSION
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
terradactyl-terraform-1.1.0 lib/terradactyl/terraform/version_manager/defaults.rb
terradactyl-terraform-1.0.0 lib/terradactyl/terraform/version_manager/defaults.rb
terradactyl-terraform-0.15.0 lib/terradactyl/terraform/version_manager/defaults.rb
terradactyl-terraform-0.13.0 lib/terradactyl/terraform/version_manager/defaults.rb