lib/cloudstrap/config.rb in cloudstrap-0.29.1.pre vs lib/cloudstrap/config.rb in cloudstrap-0.29.5.pre

- old
+ new

@@ -7,127 +7,110 @@ include ::Contracts::Core include ::Contracts::Builtin Contract None => String def region - @region ||= ENV.fetch('BOOTSTRAP_REGION') do - config.fetch('region') do - 'us-west-2' - end - end + lookup(:region) { 'us-west-2' } end Contract None => String def cache_path - @cache_path ||= ENV.fetch('BOOTSTRAP_CACHE_PATH') do - config.fetch('cache_path') { [workdir, '.cache'].join('/') } - end + lookup(:cache_path) { [workdir, '.cache'].join('/') } end Contract None => String def vpc_cidr_block - @vpc_cidr_block ||= ENV.fetch('BOOTSTRAP_VPC_CIDR_BLOCK') do - config.fetch('vpc_cidr_block') { '10.0.0.0/16' } - end + lookup(:vpc_cidr_block) { '10.0.0.0/16' } end Contract None => String def public_cidr_block - @public_cidr_block ||= ENV.fetch('BOOTSTRAP_PUBLIC_CIDR_BLOCK') do - config.fetch('public_cidr_block') do - vpc_cidr_block.gsub(/([[:digit:]]{1,3}\.?){2,2}\/[[:digit:]]{1,2}$/, '0.0/24') - end + lookup(:public_cidr_block) do + vpc_cidr_block.gsub(/([[:digit:]]{1,3}\.?){2,2}\/[[:digit:]]{1,2}$/, '0.0/24') end end Contract None => String def private_cidr_block - @private_cidr_block ||= ENV.fetch('BOOTSTRAP_PRIVATE_CIDR_BLOCK') do - config.fetch('private_cidr_block') do - vpc_cidr_block.gsub(/([[:digit:]]{1,3}\.?){2,2}\/[[:digit:]]{1,2}$/, '1.0/24') - end + lookup(:private_cidr_block) do + vpc_cidr_block.gsub(/([[:digit:]]{1,3}\.?){2,2}\/[[:digit:]]{1,2}$/, '1.0/24') end end Contract None => String def ami_owner - @ami_owner ||= ENV.fetch('BOOTSTRAP_AMI_OWNER') do - config.fetch('ami_owner') do - '099720109477' - end - end + lookup(:ami_owner) { '099720109477' } end Contract None => String def ubuntu_release - @distribution ||= ENV.fetch('BOOTSTRAP_UBUNTU_RELEASE') do - config.fetch('ubuntu_release') do - '14.04' - end - end + lookup(:ubuntu_release) { '14.04' } end Contract None => String def instance_type - @instance_type ||= ENV.fetch('BOOTSTRAP_INSTANCE_TYPE') do - config.fetch('instance_type') do - 't2.micro' - end - end + lookup(:instance_type) { 't2.micro' } end Contract None => String def ssh_dir - @ssh_dir ||= File.expand_path(ENV.fetch('BOOTSTRAP_SSH_DIR') do - [workdir, '.ssh'].join('/') - end) + lookup(:ssh_dir) { [workdir, '.ssh'].join('/') } end Contract None => String def ssh_username - @ssh_username ||= ENV.fetch('BOOTSTRAP_SSH_USERNAME') do - config.fetch('ssh_username') do - 'ubuntu' - end - end + lookup(:ssh_username) { 'ubuntu' } end Contract None => String def hdp_dir @hdp_dir ||= File.expand_path(ENV.fetch('BOOTSTRAP_HDP_DIR') { dir }) end Contract None => String def hdp_origin - @hdp_origin ||= ENV.fetch('BOOTSTRAP_HDP_BOOTSTRAP_ORIGIN') do - config.fetch('hdp_bootstrap_origin') - end + lookup :hdp_bootstrap_origin end Contract None => String def hdp_version - @hdp_archive ||= ENV.fetch('BOOTSTRAP_HDP_BOOTSTRAP_VERSION') do - config.fetch('hdp_bootstrap_version') - end + lookup :hdp_bootstrap_version end Contract None => String def hdp_package_url - @hdp_package_url ||= ENV.fetch('BOOTSTRAP_HDP_BOOTSTRAP_PACKAGE_URL') do - config.fetch('hdp_bootstrap_package_url') do - "#{hdp_origin}/hcp-bootstrap_#{hdp_version.gsub('+', '%2B')}_amd64.deb" - end + lookup(:hdp_bootstrap_package_url) do + "#{hdp_origin}/hcp-bootstrap_#{hdp_version.gsub('+', '%2B')}_amd64.deb" end end Contract None => String def bootstrap_properties_seed_url - ENV.fetch('BOOTSTRAP_PROPERTIES_SEED_URL') do - config.fetch('bootstrap_properties_seed_url') - end + lookup :bootstrap_properties_seed_url end private + + StringToString = Func[Maybe[String] => Maybe[String]] + + Contract RespondTo[:to_s], Maybe[Or[String, StringToString]] => Maybe[String] + def memoize(key, value = nil) + key = key.to_s.tap { |k| k.prepend('@') unless k.start_with?('@') } + return instance_variable_get(key) if instance_variable_defined?(key) + + instance_variable_set(key, block_given? ? yield(key) : value) + end + + Contract RespondTo[:to_s], Maybe[Or[String, StringToString]] => String + def lookup(key = __callee__, default = nil) + memoize(key) do + ENV.fetch("BOOTSTRAP_#{key.to_s.upcase}") do + config.fetch(key.to_s) do + block_given? ? yield(key) : default + end + end + end + end Contract None => String def workdir @workdir ||= ENV.fetch('BOOTSTRAP_WORKDIR') { Dir.pwd } end