Module: Nugrant::Helper::Env::Exporter
- Defined in:
- lib/nugrant/helper/env/exporter.rb
Constant Summary collapse
- DEFAULT_AUTOENV_PATH =
"./.env"
- DEFAULT_SCRIPT_PATH =
"./nugrant2env.sh"
- VALID_EXPORTERS =
[:autoenv, :script, :terminal]
Class Method Summary collapse
-
.autoenv_exporter(bag, options = {}) ⇒ side-effect
Creates an autoenv script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
-
.command(type, key, value, options = {}) ⇒ Object
Given a key and a value, return a string representation of the command type requested.
-
.export(bag, options = {}) ⇒ side-effect
Generic function to export a bag.
-
.export_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to export an environment variable.
-
.script_exporter(bag, options = {}) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
-
.terminal_exporter(bag, options = {}) ⇒ side-effect
Export to terminal the commands that are required to export or unset a bunch of environment variables taken from the bag.
-
.unset_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to unset an environment variable.
-
.valid?(exporter) ⇒ Boolean
Returns true if the exporter name received is a valid valid export, false otherwise.
Class Method Details
.autoenv_exporter(bag, options = {}) ⇒ side-effect
Creates an autoenv script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
Options:
* :autoenv_path => The path where to write the script, defaults to `./.env`.
* :escape_value => If true, escape the value to export (or unset), default to true.
* :io => The io where the command should be written, default to nil which create the autoenv on disk.
* :namer => The namer used to transform bag segments into variable name, default to Namer::default().
* :override => If true, variable a exported even when the override an existing env key, default to true.
* :type => The type of command, default to :export.
45 46 47 48 49 50 51 |
# File 'lib/nugrant/helper/env/exporter.rb', line 45 def self.autoenv_exporter(bag, = {}) io = [:io] || (File.open(File.([:autoenv_path] || DEFAULT_AUTOENV_PATH), "wb")) terminal_exporter(bag, .merge({:io => io})) ensure io.close() if io end |
.command(type, key, value, options = {}) ⇒ Object
Given a key and a value, return a string representation of the command type requested. Available types:
* :export => A bash compatible export command
* :unset => A bash compatible export command
146 147 148 149 150 151 152 153 154 |
# File 'lib/nugrant/helper/env/exporter.rb', line 146 def self.command(type, key, value, = {}) # TODO: Replace by a map type => function name case when type == :export export_command(key, value, ) when type == :unset unset_command(key, value, ) end end |
.export(bag, options = {}) ⇒ side-effect
Generic function to export a bag. This walk the bag, for each element, it creates the key using the namer and then forward the key and value to the block if the variable does not override an existing environment variable or if options :override is set to true.
Options:
* :namer => The namer used to transform bag parents into variable name, default to Namer::default().
* :override => If true, variable a exported even when the override an existing env key, default to true.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/nugrant/helper/env/exporter.rb', line 123 def self.export(bag, = {}) namer = [:namer] || Env::Namer.default() override = .fetch(:override, true) variables = {} bag.walk do |path, key, value| key = namer.call(path) variables[key] = value if override or not ENV[key] end variables.sort().each do |key, value| yield key, value end end |
.export_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to export an environment variable.
Options:
* :escape_value (true) => If true, escape the value to export.
170 171 172 173 174 175 176 |
# File 'lib/nugrant/helper/env/exporter.rb', line 170 def self.export_command(key, value, = {}) value = value.to_s() value = Shellwords.escape(value) if [:escape_value] == nil || [:escape_value] # TODO: Handle platform differently "export #{key}=#{value}" end |
.script_exporter(bag, options = {}) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
Options:
* :escape_value => If true, escape the value to export (or unset), default to true.
* :io => The io where the command should be written, default to nil which create the script on disk.
* :namer => The namer used to transform bag segments into variable name, default to Namer::default().
* :override => If true, variable a exported even when the override an existing env key, default to true.
* :script_path => The path where to write the script, defaults to `./nugrant2env.sh`.
* :type => The type of command, default to :export.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nugrant/helper/env/exporter.rb', line 72 def self.script_exporter(bag, = {}) io = [:io] || (File.open(File.([:script_path] || DEFAULT_SCRIPT_PATH), "wb")) io.puts("#!/bin/env sh") io.puts() terminal_exporter(bag, .merge({:io => io})) ensure io.close() if io end |
.terminal_exporter(bag, options = {}) ⇒ side-effect
Export to terminal the commands that are required to export or unset a bunch of environment variables taken from the bag.
Options:
* :escape_value => If true, escape the value to export (or unset), default to true.
* :io => The io where the command should be displayed, default to $stdout.
* :namer => The namer used to transform bag segments into variable name, default to Namer::default().
* :override => If true, variable a exported even when the override an existing env key, default to true.
* :type => The type of command, default to :export.
99 100 101 102 103 104 105 106 |
# File 'lib/nugrant/helper/env/exporter.rb', line 99 def self.terminal_exporter(bag, = {}) io = [:io] || $stdout type = [:type] || :export export(bag, ) do |key, value| io.puts(command(type, key, value, )) end end |
.unset_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to unset an environment variable.
188 189 190 191 |
# File 'lib/nugrant/helper/env/exporter.rb', line 188 def self.unset_command(key, value, = {}) # TODO: Handle platform differently "unset #{key}" end |
.valid?(exporter) ⇒ Boolean
Returns true if the exporter name received is a valid valid export, false otherwise.
22 23 24 |
# File 'lib/nugrant/helper/env/exporter.rb', line 22 def self.valid?(exporter) VALID_EXPORTERS.include?(exporter) end |