lib/cloud_powers/zenv.rb in cloud_powers-0.2.7.23 vs lib/cloud_powers/zenv.rb in cloud_powers-1.0.0

- old
+ new

@@ -1,17 +1,17 @@ require 'dotenv' -require_relative 'helper' +require 'cloud_powers/helpers' module Smash module CloudPowers # This module provides some environment variable management and functionality # Hopefully it should provide us with some "Zen", when dealing with normally # annoying env issues. Meh, it probably won't but I like the name, so it stays :} # System ENV, dotenv ENV and instance variables are considered for now but this # will also use elasticache/redis...some other stuff too, in the coming versions module Zenv - include Smash::CloudPowers::Helper + include Smash::CloudPowers::Helpers # Attempts to find a file by searching the current directory for the file # then walking up the file tree and searching at each stop all the way up # to the root directory # @@ -49,16 +49,20 @@ # Parameters [key <String]: The key to search for # # Returns # the value of the +key+ searched for def i_vars(key = '') + name = to_i_var(key) + + # if no key is given, return a +Hash+ of all i-var/value pairs if key.empty? - return self.instance_variables.inject({}) do |r,v| - r.tap { |h| h[to_snake(v)] = self.instance_variable_get(to_i_var(v)) } + return self.instance_variables.inject({}) do |r, v| + r.tap { |h| h[name] = self.instance_variable_get(name) } end end - self.instance_variable_get(to_i_var(key)) + + self.instance_variable_get(name) end # PROJECT_ROOT should be set as early as possible in this Node's initilize # method. This method tries to search for it, using #zfind() and if a `nil` # result is returned from that search, `pwd` is used as the PROJECT_ROOT. @@ -117,10 +121,11 @@ # * if a +key+ is given as a parameter, +String+ # * if no +key+ is given as a parameter, +Hash+ # with this structure +{ key => value, ... }+ is returned for all keys with a value. # Keys with no value are ommitted from the result. def system_vars(key = '') + name = to_snake(key).upcase if key.empty? # Separate key-value pairs from the large string received by `ENV` separate_pairs = `ENV`.split(/\n/).map do |string_pair| string_pair.split('=') end @@ -129,18 +134,17 @@ # * keys with no value are removed separate_pairs.inject({}) do |res, pair| res.tap { |h_res| h_res[pair.first] = pair.last unless (pair.first == pair.last) } end else - res = `printf "#{to_snake(key).upcase}"` - return res.empty? ? nil : res + Object::ENV.has_key?(name) ? Object::ENV.fetch(name) : nil end end # ZFind looks for the key in a preditermined order of importance: # * i-vars are considered first becuase they might be tracking different - # locations for multiple tasks or something like that. + # locations for multiple jobs or something like that. # * dotenv files are second because they were manually set, so for sure # it's important # * System Env[@] variables are up next. Hopefully by this time we've found # our information but if not, it should "search" through the system env too. # @@ -152,13 +156,10 @@ # # Notes # * TODO: implement a search for all 3 that can find close matches def zfind(key) project_root if @project_root.nil? - res = (i_vars[to_snake(key).upcase] or - env_vars[to_snake(key).upcase] unless @project_root.nil?) or - system_vars[to_snake(key).upcase] - (res.nil? or res.empty?) ? nil : res + i_vars(key) || env_vars(key) || system_vars(key) end end end end