Class: RCLoadEnv::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/rcloadenv/loader.rb

Overview

A class that loads environment variables from a RuntimeConfig resource.

Defined Under Namespace

Classes: UsageError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_name, exclude: [], include: [], override: false, project: nil, debug: false) ⇒ Loader

Create a loader. Alwaus uses application default credentials.

Parameters:

  • config_name (String)

    Name of the runtime config resource

  • exclude (Array<String>) (defaults to: [])

    Config keys to exclude (optional)

  • include (Array<String>) (defaults to: [])

    Config keys to include (optional)

  • override (Boolean) (defaults to: false)

    Whether to override existing environment variables (default: false)

  • project (String, nil) (defaults to: nil)

    Optional name of the cloud project. If not set, attempts to infer one from the environment.

  • debug (Boolean) (defaults to: false)

    Whether to print debug output (default: false)



45
46
47
48
49
50
51
52
53
54
# File 'lib/rcloadenv/loader.rb', line 45

def initialize config_name,
               exclude: [], include: [], override: false,
               project: nil, debug: false
  @config_name = config_name.to_s
  @project = (project || default_project).to_s
  @exclude = exclude
  @include = include
  @override = override
  @debug = debug
end

Instance Attribute Details

#config_nameString (readonly)

Returns The Runtime Config resource name.

Returns:

  • (String)

    The Runtime Config resource name



57
58
59
# File 'lib/rcloadenv/loader.rb', line 57

def config_name
  @config_name
end

#projectString (readonly)

Returns The cloud project.

Returns:

  • (String)

    The cloud project



59
60
61
# File 'lib/rcloadenv/loader.rb', line 59

def project
  @project
end

Instance Method Details

#modify_env(env = {}) ⇒ Object

Modify the given environment with the configuration. The given hash is modified in place and returned. If no hash is provided, a new one is created and returned.

Parameters:

  • env (Hash<String,String>) (defaults to: {})

    The environment to modify.

Returns:

  • the modified environment.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rcloadenv/loader.rb', line 69

def modify_env env={}
  raw_variables.each do |k, v|
    if !@exclude.empty? && @exclude.include?(k) ||
       !@include.empty? && !@include.include?(k)
      debug "Skipping config variable #{k}"
    else
      debug "Found config variable #{k}"
      key = make_env_key k
      if !env.include?(key)
        debug "Setting envvar: #{key}"
        env[key] = v
      elsif @override
        debug "Overriding envvar: #{key}"
        env[key] = v
      else
        debug "Envvar already set: #{key}"
      end
    end
  end
  env
end

#raw_variablesHash<String,String>

Returns the hash of variables retrieved from the runtime config. Variable names have the parent (project and config name) stripped. Values are all converted to strings.

Returns:

  • (Hash<String,String>)

    the variables.



117
118
119
# File 'lib/rcloadenv/loader.rb', line 117

def raw_variables
  @raw_variables ||= load_raw_variables
end

#write_dotenv(io = STDOUT) ⇒ Object

Modify the given environment with the configuration. The given hash is modified in place and returned. If no hash is provided, a new one is created and returned.

Parameters:

  • env (Hash<String,String>)

    The environment to modify.

Returns:

  • the modified environment.



99
100
101
102
103
104
105
106
107
108
# File 'lib/rcloadenv/loader.rb', line 99

def write_dotenv io=STDOUT
  raw_variables.each do |k, v|
    key = make_env_key k
    if key =~ /^[\w\.]+$/
      io.puts "#{key}=\"#{escape_for_dotenv v}\""
    else
      error "Bad key: #{key}"
    end
  end
end