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

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.

Parameters:

  • bag

    The bag to create the script for.

Returns:

  • (side-effect)

    Creates a script file containing commands to export or unset environment variables for bag.



45
46
47
48
49
50
51
# File 'lib/nugrant/helper/env/exporter.rb', line 45

def self.autoenv_exporter(bag, options = {})
  io = options[:io] || (File.open(File.expand_path(options[:autoenv_path] || DEFAULT_AUTOENV_PATH), "wb"))

  terminal_exporter(bag, options.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, options = {})
  # TODO: Replace by a map type => function name
  case
  when type == :export
    export_command(key, value, options)
  when type == :unset
    unset_command(key, value, options)
  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.

Parameters:

  • bag

    The bag to export.

Returns:

  • (side-effect)

    Yields each key and value to a block



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, options = {})
  namer = options[:namer] || Env::Namer.default()
  override = options.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.

Parameters:

  • key

    The key of the environment variable to export. It cannot be nil.

  • value

    The value of the environment variable to export

Returns:

  • The export command, as a string



170
171
172
173
174
175
176
# File 'lib/nugrant/helper/env/exporter.rb', line 170

def self.export_command(key, value, options = {})
  value = value.to_s()
  value = Shellwords.escape(value) if options[:escape_value] == nil || options[: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.

Parameters:

  • bag

    The bag to create the script for.

Returns:

  • (side-effect)

    Creates a script file containing commands to export or unset environment variables for bag.



72
73
74
75
76
77
78
79
80
81
# File 'lib/nugrant/helper/env/exporter.rb', line 72

def self.script_exporter(bag, options = {})
  io = options[:io] || (File.open(File.expand_path(options[:script_path] || DEFAULT_SCRIPT_PATH), "wb"))

  io.puts("#!/bin/env sh")
  io.puts()

  terminal_exporter(bag, options.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.

Parameters:

  • bag

    The bag to create the script for.

Returns:

  • (side-effect)

    Outputs to io the commands generated.



99
100
101
102
103
104
105
106
# File 'lib/nugrant/helper/env/exporter.rb', line 99

def self.terminal_exporter(bag, options = {})
  io = options[:io] || $stdout
  type = options[:type] || :export

  export(bag, options) do |key, value|
    io.puts(command(type, key, value, options))
  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.

Parameters:

  • key

    The key of the environment variable to export. It cannot be nil.

Returns:

  • The unset command, as a string



188
189
190
191
# File 'lib/nugrant/helper/env/exporter.rb', line 188

def self.unset_command(key, value, options = {})
  # TODO: Handle platform differently
  "unset #{key}"
end

.valid?(exporter) ⇒ Boolean

Returns true if the exporter name received is a valid valid export, false otherwise.

Parameters:

  • exporter

    The exporter name to check validity

Returns:

  • (Boolean)

    true if exporter is valid, false otherwise.



22
23
24
# File 'lib/nugrant/helper/env/exporter.rb', line 22

def self.valid?(exporter)
  VALID_EXPORTERS.include?(exporter)
end