Class: Nugrant::Helper::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/nugrant/helper/env.rb

Constant Summary collapse

@@DEFAULT_SCRIPT_PATH =
"./nugrant2env.sh"

Class Method Summary collapse

Class Method Details

.commands(type, bag, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/nugrant/helper/env.rb', line 51

def self.commands(type, bag, options = {})
  # TODO: Replace by a map type => function name

  case
  when type == :export
    export_commands(bag, options)
  when type == :unset
    unset_commands(bag, options)
  end
end

.default_namer(char = "_") ⇒ Object

Returns the default namer, which join segments together using a character and upcase the result.

Parameters:

  • `char`

    The character used to join segments together, default to ‘“_”`.

Returns:

  • A lambda that will simply joins segment using the ‘char` argument and upcase the result.



29
30
31
32
33
# File 'lib/nugrant/helper/env.rb', line 29

def self.default_namer(char = "_")
  lambda do |segments|
    segments.join(char).upcase()
  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



143
144
145
146
147
148
149
# File 'lib/nugrant/helper/env.rb', line 143

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

.export_commands(bag, options = {}) ⇒ Object

Generate the list of export commands that must be executed so each bag variables is export to an environment variables

Options:

* :escape_value (true) => If true, escape the value to export.

* :namer (nil) => A block taking as options the full path of
                  an export variable key and return what
                  the name the should be exported.
  • :override (true) => If true, an export command will be put

    in the list even if it already exist in
    the ENV array.
    

Parameters:

  • bag

    The bag to export to environment variables

Returns:

  • A list of commands that can be used to export the bag to environment variables.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nugrant/helper/env.rb', line 82

def self.export_commands(bag, options = {})
  namer = options[:namer] || default_namer()
  override = options.fetch(:override, true)

  commands = []
  walk_bag(bag) do |segments, key, value|
    key = namer.call(segments)

    commands << export_command(key, value, options) if override or not ENV[key]
  end

  commands
end

.prefix_namer(prefix, delegate_namer) ⇒ Object

Returns the prefix namer, which add a prefix to segments and delegate its work to another namer.

Parameters:

  • prefix

    The prefix to add to segments.

  • delegate_namer

    A namer that will be used to transform the prefixed segments.

Returns:

  • A lambda that will simply add prefix to segments and will call the delegate_namer with those new segments.



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

def self.prefix_namer(prefix, delegate_namer)
  lambda do |segments|
    delegate_namer.call([prefix] + segments)
  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



161
162
163
164
# File 'lib/nugrant/helper/env.rb', line 161

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

  "unset #{key}"
end

.unset_commands(bag, options = {}) ⇒ Object

Generate the list of unset commands that must be executed so each bag variables is unset from the environment variables

Options:

* :namer (nil) => A block taking as options the full path of
                  an export variable key and return what
                  the name the should be exported.
  • :override (true) => If true, an export command will be put

    in the list even if it already exist in
    the ENV array.
    

Parameters:

  • bag

    The bag to unset environment variables

Returns:

  • A list of commands that can be used to unset the bag from environment variables.



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nugrant/helper/env.rb', line 115

def self.unset_commands(bag, options = {})
  namer = options[:namer] || default_namer()
  override = options.fetch(:override, true)

  commands = []
  walk_bag(bag) do |segments, key, value|
    key = namer.call(segments)

    commands << unset_command(key, value, options) if override or not ENV[key]
  end

  commands
end

.write_commands(bag, options = {}, io = $stdout) ⇒ 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.

The

Options:

* :type => The type of command, default to :export
* See commands, export_commands and unset_commands for further options.

Parameters:

  • bag

    The bag to create the script for.

  • io (defaults to: $stdout)

    The io where to output the commands, defaults to $stdout.

Returns:

  • (side-effect)

    Outputs to io the commands generated.



209
210
211
212
213
214
215
# File 'lib/nugrant/helper/env.rb', line 209

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

  commands.each do |command|
    io.puts(command)
  end
end

.write_script(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:

* :type => The type of command, default to :export
* :script_path => The path where to write the script, defaults to `./nugrant2env.sh`.
* See commands, export_commands and unset_commands for further options.

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.



182
183
184
185
186
187
188
189
190
191
# File 'lib/nugrant/helper/env.rb', line 182

def self.write_script(bag, options = {})
  file = File.open(File.expand_path(options[:script_path] || @@DEFAULT_SCRIPT_PATH), "w")

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

  write_commands(bag, options, file)
ensure
  file.close() if file
end