class OsEnv
def initialize(env = nil)
def initialize(env = nil) @osenv = env @content = nil @content = value_for(env) unless env.nil? end
def split
def split # we can't take advantage of `File::PATH_SEPARATOR` as code is # evaluated on the host machine path_separator = inspec.os.windows? ? ';' : ':' # -1 is required to catch cases like dir1::dir2: # where we have a trailing : @content.nil? ? [] : @content.split(path_separator, -1) end
def to_s
def to_s if @osenv.nil? 'Environment variables' else "Environment variable #{@osenv}" end end
def value_for(env)
def value_for(env) command = if inspec.os.windows? "$Env:#{env}" else 'env' end out = inspec.command(command) unless out.exit_status == 0 skip_resource "Can't read environment variables on #{os[:family]}. "\ "Tried `#{command}` which returned #{out.exit_status}" end if inspec.os.windows? out.stdout.strip else params = SimpleConfig.new(out.stdout).params params[env] end end