nugrant 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +10 -0
- data/README.md +26 -7
- data/lib/nugrant/helper/env/exporter.rb +208 -0
- data/lib/nugrant/helper/env/namer.rb +47 -0
- data/lib/nugrant/vagrant/v1/command/env.rb +39 -5
- data/lib/nugrant/vagrant/v2/command/env.rb +38 -4
- data/lib/nugrant/version.rb +1 -1
- data/test/lib/nugrant/helper/env/test_exporter.rb +238 -0
- metadata +6 -5
- data/lib/nugrant/helper/env.rb +0 -232
- data/test/lib/nugrant/helper/test_env.rb +0 -150
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmY5YWM2MTBjZTAwOTUzY2NhMjMxZTdiZTA5ODBmZTI4NmE4MzllOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzE5NTViNDE0Y2E0YzQyNTM5NWIwYmZmMzU2YTUyMjVjMmIyZjc0Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDU4ZWQxY2ZiZmZjOTNjNWEwYzAzNzBkNTAxMmI5ZDk2OGExMWNkMGQyYjJi
|
10
|
+
Y2IwNjA2YWQyZDcxMjYyMzQ5ZWY0NzJhZGI0OGI5Y2M3MGIzNmEwMDE0OGIx
|
11
|
+
MzZjMzUzNjYwZWMyMWFmM2U1NmRmODY3M2Y0ZTY2YzYxNzAwMzA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTQxNDYzYWNiMWExYzM4N2RlOThmMjYzNjEzNGJhNDYxMDliOWU4MmFjNWYx
|
14
|
+
NjFlNDk1M2E3ZTE4ZWY1YzM5MWM0YzExMDlkZGYyMjAyYmRkZDc2OTdhYjA0
|
15
|
+
ZmVkOTVkYTBhZTc5NjFkNjk1NWNlMjQ4Y2QxN2E4N2EwY2UxMmU=
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 1.4.0 (November 28th, 2013)
|
2
|
+
|
3
|
+
* Adding support to export to an [autoenv](https://github.com/kennethreitz/autoenv)
|
4
|
+
file. See [GH-13](https://github.com/maoueh/nugrant/issues/13).
|
5
|
+
|
6
|
+
* Deprecated usage of `-s, --script` option for command
|
7
|
+
`vagrant user env`. This was replaced by the more generic
|
8
|
+
and extensible `-f, --format FORMAT` option. The
|
9
|
+
`-s, --script` option will be removed in 2.0.
|
10
|
+
|
1
11
|
# 1.3.0 (November 19th, 2013)
|
2
12
|
|
3
13
|
* Now using [minitest](https://github.com/seattlerb/minitest) as our
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ If you would like to use Nugrant as a library, simply reference
|
|
27
27
|
it as a dependency of your application. Probably by adding it to
|
28
28
|
your `Gemfile` or your `.gemspec` file.
|
29
29
|
|
30
|
-
"nugrant", "~> 1.
|
30
|
+
"nugrant", "~> 1.4"
|
31
31
|
|
32
32
|
### Vagrant
|
33
33
|
|
@@ -316,7 +316,7 @@ Sometimes, you would like to have acces to the different values
|
|
316
316
|
stored in your `.vagrantuser` from environment variables. This
|
317
317
|
command is meant is exactly for this.
|
318
318
|
|
319
|
-
By using one of the
|
319
|
+
By using one of the three methods below, you will be able to export
|
320
320
|
(but also unset) environment variables from your current
|
321
321
|
parameters as seen by Nugrant.
|
322
322
|
|
@@ -359,11 +359,9 @@ by sourcing the script.
|
|
359
359
|
Create a file named `nugrant2env` somewhere accessible from
|
360
360
|
the `$PATH` variable with the following content:
|
361
361
|
|
362
|
-
```bash
|
363
362
|
#!/bin/env sh
|
364
363
|
|
365
364
|
$(vagrant user env "$@")
|
366
|
-
```
|
367
365
|
|
368
366
|
This script will simply delegates to the `vagrant user env`
|
369
367
|
command and pass all arguments it receives to it. The
|
@@ -391,17 +389,38 @@ to:
|
|
391
389
|
Use the command to generate a base script in the current
|
392
390
|
directory that you will then source:
|
393
391
|
|
394
|
-
vagrant user env
|
392
|
+
vagrant user env --format script
|
395
393
|
|
396
394
|
This will generate a script called `nugrant2env.sh` into the
|
397
395
|
current directory. You then simply source this script:
|
398
396
|
|
399
397
|
. nugrant2env.sh
|
400
398
|
|
401
|
-
Using vagrant user env -
|
402
|
-
script that will unset the
|
399
|
+
Using `vagrant user env -u --format script` will instead generate the bash
|
400
|
+
script that will unset the environment variables. Don't forget
|
403
401
|
to source it to unset variables.
|
404
402
|
|
403
|
+
##### Method #3
|
404
|
+
|
405
|
+
Use the command to generate an [autoenv](https://github.com/kennethreitz/autoenv)
|
406
|
+
file in the current directory. By using the [autoenv] project, anytime you
|
407
|
+
will enter the project directory via the `cd` command, variables
|
408
|
+
exported found in the `.env` file generated will be exported to
|
409
|
+
your environment.
|
410
|
+
|
411
|
+
vagrant user env --format autoenv
|
412
|
+
|
413
|
+
This will generate a file called `.env` in the
|
414
|
+
current directory. You then simply change to the directory
|
415
|
+
where the `.env` file was generated to made exported variables
|
416
|
+
available in your environment.
|
417
|
+
|
418
|
+
cd ..
|
419
|
+
cd <project_dir>
|
420
|
+
|
421
|
+
Using `vagrant user env -u --format autoenv` will instead generate
|
422
|
+
the autoenv file that will unset the environment variables.
|
423
|
+
|
405
424
|
## Contributing
|
406
425
|
|
407
426
|
You can contribute by filling issues when something goes
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
require 'nugrant/bag'
|
4
|
+
require 'nugrant/helper/env/namer'
|
5
|
+
|
6
|
+
module Nugrant
|
7
|
+
module Helper
|
8
|
+
module Env
|
9
|
+
module Exporter
|
10
|
+
@@DEFAULT_AUTOENV_PATH = "./.env"
|
11
|
+
@@DEFAULT_SCRIPT_PATH = "./nugrant2env.sh"
|
12
|
+
|
13
|
+
@@VALID_EXPORTERS = [:autoenv, :script, :terminal]
|
14
|
+
|
15
|
+
##
|
16
|
+
# Returns true if the exporter name received is a valid
|
17
|
+
# valid export, false otherwise.
|
18
|
+
#
|
19
|
+
# @param exporter The exporter name to check validity
|
20
|
+
#
|
21
|
+
# @return true if exporter is valid, false otherwise.
|
22
|
+
def self.valid?(exporter)
|
23
|
+
@@VALID_EXPORTERS.include?(exporter)
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Creates an autoenv script containing the commands that are required
|
28
|
+
# to export or unset a bunch of environment variables taken from the
|
29
|
+
# bag.
|
30
|
+
#
|
31
|
+
# @param bag The bag to create the script for.
|
32
|
+
#
|
33
|
+
# @return (side-effect) Creates a script file containing commands
|
34
|
+
# to export or unset environment variables for
|
35
|
+
# bag.
|
36
|
+
#
|
37
|
+
# Options:
|
38
|
+
# * :autoenv_path => The path where to write the script, defaults to `./.env`.
|
39
|
+
# * :escape_value => If true, escape the value to export (or unset), default to true.
|
40
|
+
# * :io => The io where the command should be written, default to nil which create the autoenv on disk.
|
41
|
+
# * :namer => The namer used to transform bag segments into variable name, default to Namer::default().
|
42
|
+
# * :override => If true, variable a exported even when the override an existing env key, default to true.
|
43
|
+
# * :type => The type of command, default to :export.
|
44
|
+
#
|
45
|
+
def self.autoenv_exporter(bag, options = {})
|
46
|
+
io = options[:io] || (File.open(File.expand_path(options[:autoenv_path] || @@DEFAULT_AUTOENV_PATH), "w"))
|
47
|
+
|
48
|
+
terminal_exporter(bag, options.merge({:io => io}))
|
49
|
+
ensure
|
50
|
+
io.close() if io
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Creates a bash script containing the commands that are required
|
55
|
+
# to export or unset a bunch of environment variables taken from the
|
56
|
+
# bag.
|
57
|
+
#
|
58
|
+
# @param bag The bag to create the script for.
|
59
|
+
#
|
60
|
+
# @return (side-effect) Creates a script file containing commands
|
61
|
+
# to export or unset environment variables for
|
62
|
+
# bag.
|
63
|
+
#
|
64
|
+
# Options:
|
65
|
+
# * :escape_value => If true, escape the value to export (or unset), default to true.
|
66
|
+
# * :io => The io where the command should be written, default to nil which create the script on disk.
|
67
|
+
# * :namer => The namer used to transform bag segments into variable name, default to Namer::default().
|
68
|
+
# * :override => If true, variable a exported even when the override an existing env key, default to true.
|
69
|
+
# * :script_path => The path where to write the script, defaults to `./nugrant2env.sh`.
|
70
|
+
# * :type => The type of command, default to :export.
|
71
|
+
#
|
72
|
+
def self.script_exporter(bag, options = {})
|
73
|
+
io = options[:io] || (File.open(File.expand_path(options[:script_path] || @@DEFAULT_SCRIPT_PATH), "w"))
|
74
|
+
|
75
|
+
io.puts("#!/bin/env sh")
|
76
|
+
io.puts()
|
77
|
+
|
78
|
+
terminal_exporter(bag, options.merge({:io => io}))
|
79
|
+
ensure
|
80
|
+
io.close() if io
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Export to terminal the commands that are required
|
85
|
+
# to export or unset a bunch of environment variables taken from the
|
86
|
+
# bag.
|
87
|
+
#
|
88
|
+
# @param bag The bag to create the script for.
|
89
|
+
#
|
90
|
+
# @return (side-effect) Outputs to io the commands generated.
|
91
|
+
#
|
92
|
+
# Options:
|
93
|
+
# * :escape_value => If true, escape the value to export (or unset), default to true.
|
94
|
+
# * :io => The io where the command should be displayed, default to $stdout.
|
95
|
+
# * :namer => The namer used to transform bag segments into variable name, default to Namer::default().
|
96
|
+
# * :override => If true, variable a exported even when the override an existing env key, default to true.
|
97
|
+
# * :type => The type of command, default to :export.
|
98
|
+
#
|
99
|
+
def self.terminal_exporter(bag, options = {})
|
100
|
+
io = options[:io] || $stdout
|
101
|
+
type = options[:type] || :export
|
102
|
+
|
103
|
+
export(bag, options) do |key, value|
|
104
|
+
io.puts(command(type, key, value, options))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Generic function to export a bag. This walk the bag,
|
110
|
+
# for each element, it creates the key using the namer
|
111
|
+
# and then forward the key and value to the block if
|
112
|
+
# the variable does not override an existing environment
|
113
|
+
# variable or if options :override is set to true.
|
114
|
+
#
|
115
|
+
# @param bag The bag to export.
|
116
|
+
#
|
117
|
+
# @return (side-effect) Yields each key and value to a block
|
118
|
+
#
|
119
|
+
# Options:
|
120
|
+
# * :namer => The namer used to transform bag segments into variable name, default to Namer::default().
|
121
|
+
# * :override => If true, variable a exported even when the override an existing env key, default to true.
|
122
|
+
#
|
123
|
+
def self.export(bag, options = {})
|
124
|
+
namer = options[:namer] || Env::Namer.default()
|
125
|
+
override = options.fetch(:override, true)
|
126
|
+
|
127
|
+
variables = {}
|
128
|
+
walk_bag(bag) do |segments, key, value|
|
129
|
+
key = namer.call(segments)
|
130
|
+
|
131
|
+
variables[key] = value if override or not ENV[key]
|
132
|
+
end
|
133
|
+
|
134
|
+
variables.sort().each do |key, value|
|
135
|
+
yield key, value
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# Given a key and a value, return a string representation
|
141
|
+
# of the command type requested. Available types:
|
142
|
+
#
|
143
|
+
# * :export => A bash compatible export command
|
144
|
+
# * :unset => A bash compatible export command
|
145
|
+
#
|
146
|
+
def self.command(type, key, value, options = {})
|
147
|
+
# TODO: Replace by a map type => function name
|
148
|
+
case
|
149
|
+
when type == :export
|
150
|
+
export_command(key, value, options)
|
151
|
+
when type == :unset
|
152
|
+
unset_command(key, value, options)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
##
|
157
|
+
# Returns a string representation of the command
|
158
|
+
# that needs to be used on the current platform
|
159
|
+
# to export an environment variable.
|
160
|
+
#
|
161
|
+
# @param key The key of the environment variable to export.
|
162
|
+
# It cannot be nil.
|
163
|
+
# @param value The value of the environment variable to export
|
164
|
+
#
|
165
|
+
# @return The export command, as a string
|
166
|
+
#
|
167
|
+
# Options:
|
168
|
+
# * :escape_value (true) => If true, escape the value to export.
|
169
|
+
#
|
170
|
+
def self.export_command(key, value, options = {})
|
171
|
+
value = value.to_s()
|
172
|
+
value = Shellwords.escape(value) if options[:escape_value] == nil || options[:escape_value]
|
173
|
+
|
174
|
+
# TODO: Handle platform differently
|
175
|
+
"export #{key}=#{value}"
|
176
|
+
end
|
177
|
+
|
178
|
+
##
|
179
|
+
# Returns a string representation of the command
|
180
|
+
# that needs to be used on the current platform
|
181
|
+
# to unset an environment variable.
|
182
|
+
#
|
183
|
+
# @param key The key of the environment variable to export.
|
184
|
+
# It cannot be nil.
|
185
|
+
#
|
186
|
+
# @return The unset command, as a string
|
187
|
+
#
|
188
|
+
def self.unset_command(key, value, options = {})
|
189
|
+
# TODO: Handle platform differently
|
190
|
+
"unset #{key}"
|
191
|
+
end
|
192
|
+
|
193
|
+
# FIXME: Move this directly into bag class
|
194
|
+
def self.walk_bag(bag, parents = [], &block)
|
195
|
+
commands = []
|
196
|
+
|
197
|
+
bag.each do |key, value|
|
198
|
+
segments = parents + [key]
|
199
|
+
nested_bag = value.kind_of?(Nugrant::Bag)
|
200
|
+
|
201
|
+
walk_bag(value, segments, &block) if nested_bag
|
202
|
+
yield segments, key, value if not nested_bag
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Nugrant
|
2
|
+
module Helper
|
3
|
+
module Env
|
4
|
+
##
|
5
|
+
# A namer is a lambda taking as argument an array of segments
|
6
|
+
# that should return a string representation of those segments.
|
7
|
+
# How the segments are transformed to a string is up to the
|
8
|
+
# namer. By using various namer, we can change how a bag key
|
9
|
+
# is transformed into and environment variable name. This is
|
10
|
+
# like the strategy pattern.
|
11
|
+
#
|
12
|
+
module Namer
|
13
|
+
|
14
|
+
##
|
15
|
+
# Returns the default namer, which join segments together
|
16
|
+
# using a character and upcase the result.
|
17
|
+
#
|
18
|
+
# @param `char` The character used to join segments together, default to `"_"`.
|
19
|
+
#
|
20
|
+
# @return A lambda that will simply joins segment using the `char` argument
|
21
|
+
# and upcase the result.
|
22
|
+
#
|
23
|
+
def self.default(char = "_")
|
24
|
+
lambda do |segments|
|
25
|
+
segments.join(char).upcase()
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Returns the prefix namer, which add a prefix to segments
|
31
|
+
# and delegate its work to another namer.
|
32
|
+
#
|
33
|
+
# @param prefix The prefix to add to segments.
|
34
|
+
# @param delegate_namer A namer that will be used to transform the prefixed segments.
|
35
|
+
#
|
36
|
+
# @return A lambda that will simply add prefix to segments and will call
|
37
|
+
# the delegate_namer with those new segments.
|
38
|
+
#
|
39
|
+
def self.prefix(prefix, delegate_namer)
|
40
|
+
lambda do |segments|
|
41
|
+
delegate_namer.call([prefix] + segments)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'nugrant'
|
2
|
-
require 'nugrant/helper/env'
|
2
|
+
require 'nugrant/helper/env/exporter'
|
3
|
+
require 'nugrant/parameters'
|
3
4
|
|
4
5
|
module Nugrant
|
5
6
|
module Vagrant
|
@@ -11,6 +12,7 @@ module Nugrant
|
|
11
12
|
|
12
13
|
@unset = false
|
13
14
|
@script = false
|
15
|
+
@format = :terminal
|
14
16
|
@show_help = false
|
15
17
|
end
|
16
18
|
|
@@ -21,7 +23,19 @@ module Nugrant
|
|
21
23
|
|
22
24
|
parser.separator "Outputs the commands that should be executed to export\n" +
|
23
25
|
"the various parameter as environment variables. By default,\n" +
|
24
|
-
"existing ones are overridden."
|
26
|
+
"existing ones are overridden. The --format argument can be used\n" +
|
27
|
+
"to choose in which format the variables should be displayed.\n" +
|
28
|
+
"Changing the format will also change where they are displayed.\n"
|
29
|
+
parser.separator ""
|
30
|
+
parser.separator "The `-s, --script` option is deprecated and will be removed in\n" +
|
31
|
+
"version 2.0. Use `--format script` instead."
|
32
|
+
parser.separator ""
|
33
|
+
|
34
|
+
parser.separator "Available formats:"
|
35
|
+
parser.separator " autoenv => Write commands to a file named `.env` in the current directory.\n" +
|
36
|
+
" See https://github.com/kennethreitz/autoenv for more info."
|
37
|
+
parser.separator " terminal => Display commands to terminal so they can be sourced."
|
38
|
+
parser.separator " script => Write commands to a bash script named `nugrant2env.sh` so it can be sourced."
|
25
39
|
parser.separator ""
|
26
40
|
|
27
41
|
parser.separator "Available options:"
|
@@ -32,7 +46,11 @@ module Nugrant
|
|
32
46
|
end
|
33
47
|
|
34
48
|
parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
|
35
|
-
|
49
|
+
@script = script
|
50
|
+
end
|
51
|
+
|
52
|
+
parser.on("-f", "--format FORMAT", "Determines in what format variables are outputted, default to terminal") do |format|
|
53
|
+
@format = format.to_sym()
|
36
54
|
end
|
37
55
|
|
38
56
|
parser.on("-h", "--help", "Print this help") do
|
@@ -41,6 +59,15 @@ module Nugrant
|
|
41
59
|
end
|
42
60
|
end
|
43
61
|
|
62
|
+
def error(message, parser)
|
63
|
+
@env.ui.info("ERROR: #{message}", :prefix => false)
|
64
|
+
@env.ui.info("", :prefix => false)
|
65
|
+
|
66
|
+
help(parser)
|
67
|
+
|
68
|
+
return 1
|
69
|
+
end
|
70
|
+
|
44
71
|
def help(parser)
|
45
72
|
@env.ui.info(parser.help, :prefix => false)
|
46
73
|
end
|
@@ -49,6 +76,7 @@ module Nugrant
|
|
49
76
|
parser = create_parser()
|
50
77
|
arguments = parse_options(parser)
|
51
78
|
|
79
|
+
return error("Invalid format value '#{@format}'", parser) if not Helper::Env::Exporter.valid?(@format)
|
52
80
|
return help(parser) if @show_help
|
53
81
|
|
54
82
|
@logger.debug("Nugrant 'Env'")
|
@@ -59,8 +87,14 @@ module Nugrant
|
|
59
87
|
|
60
88
|
options = {:type => @unset ? :unset : :export}
|
61
89
|
|
62
|
-
|
63
|
-
|
90
|
+
case
|
91
|
+
when @script || @format == :script
|
92
|
+
Helper::Env::Exporter.script_exporter(bag, options)
|
93
|
+
when @format == :autoenv
|
94
|
+
Helper::Env::Exporter.autoenv_exporter(bag, options)
|
95
|
+
else
|
96
|
+
Helper::Env::Exporter.terminal_exporter(bag, options)
|
97
|
+
end
|
64
98
|
|
65
99
|
# No need to execute for the other VMs
|
66
100
|
return 0
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'nugrant'
|
2
|
-
require 'nugrant/helper/env'
|
2
|
+
require 'nugrant/helper/env/exporter'
|
3
|
+
require 'nugrant/parameters'
|
3
4
|
|
4
5
|
module Nugrant
|
5
6
|
module Vagrant
|
@@ -11,6 +12,7 @@ module Nugrant
|
|
11
12
|
|
12
13
|
@unset = false
|
13
14
|
@script = false
|
15
|
+
@format = :terminal
|
14
16
|
@show_help = false
|
15
17
|
end
|
16
18
|
|
@@ -21,7 +23,19 @@ module Nugrant
|
|
21
23
|
|
22
24
|
parser.separator "Outputs the commands that should be executed to export\n" +
|
23
25
|
"the various parameter as environment variables. By default,\n" +
|
24
|
-
"existing ones are overridden."
|
26
|
+
"existing ones are overridden. The --format argument can be used\n" +
|
27
|
+
"to choose in which format the variables should be displayed.\n" +
|
28
|
+
"Changing the format will also change where they are displayed.\n"
|
29
|
+
parser.separator ""
|
30
|
+
parser.separator "The `-s, --script` option is deprecated and will be removed in\n" +
|
31
|
+
"version 2.0. Use `--format script` instead."
|
32
|
+
parser.separator ""
|
33
|
+
|
34
|
+
parser.separator "Available formats:"
|
35
|
+
parser.separator " autoenv => Write commands to a file named `.env` in the current directory.\n" +
|
36
|
+
" See https://github.com/kennethreitz/autoenv for more info."
|
37
|
+
parser.separator " terminal => Display commands to terminal so they can be sourced."
|
38
|
+
parser.separator " script => Write commands to a bash script named `nugrant2env.sh` so it can be sourced."
|
25
39
|
parser.separator ""
|
26
40
|
|
27
41
|
parser.separator "Available options:"
|
@@ -35,12 +49,25 @@ module Nugrant
|
|
35
49
|
@script = script
|
36
50
|
end
|
37
51
|
|
52
|
+
parser.on("-f", "--format FORMAT", "Determines in what format variables are outputted, default to terminal") do |format|
|
53
|
+
@format = format.to_sym()
|
54
|
+
end
|
55
|
+
|
38
56
|
parser.on("-h", "--help", "Print this help") do
|
39
57
|
@show_help = true
|
40
58
|
end
|
41
59
|
end
|
42
60
|
end
|
43
61
|
|
62
|
+
def error(message, parser)
|
63
|
+
@env.ui.info("ERROR: #{message}", :prefix => false)
|
64
|
+
@env.ui.info("", :prefix => false)
|
65
|
+
|
66
|
+
help(parser)
|
67
|
+
|
68
|
+
return 1
|
69
|
+
end
|
70
|
+
|
44
71
|
def help(parser)
|
45
72
|
@env.ui.info(parser.help, :prefix => false)
|
46
73
|
end
|
@@ -49,6 +76,7 @@ module Nugrant
|
|
49
76
|
parser = create_parser()
|
50
77
|
arguments = parse_options(parser)
|
51
78
|
|
79
|
+
return error("Invalid format value '#{@format}'", parser) if not Helper::Env::Exporter.valid?(@format)
|
52
80
|
return help(parser) if @show_help
|
53
81
|
|
54
82
|
@logger.debug("Nugrant 'Env'")
|
@@ -59,8 +87,14 @@ module Nugrant
|
|
59
87
|
|
60
88
|
options = {:type => @unset ? :unset : :export}
|
61
89
|
|
62
|
-
|
63
|
-
|
90
|
+
case
|
91
|
+
when @script || @format == :script
|
92
|
+
Helper::Env::Exporter.script_exporter(bag, options)
|
93
|
+
when @format == :autoenv
|
94
|
+
Helper::Env::Exporter.autoenv_exporter(bag, options)
|
95
|
+
else
|
96
|
+
Helper::Env::Exporter.terminal_exporter(bag, options)
|
97
|
+
end
|
64
98
|
|
65
99
|
# No need to execute for the other VMs
|
66
100
|
return 0
|
data/lib/nugrant/version.rb
CHANGED
@@ -0,0 +1,238 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
require 'nugrant/bag'
|
4
|
+
require 'nugrant/helper/env/exporter'
|
5
|
+
|
6
|
+
module Nugrant
|
7
|
+
module Helper
|
8
|
+
module Env
|
9
|
+
class TestExporter < ::Minitest::Test
|
10
|
+
def create_bag(parameters)
|
11
|
+
return Nugrant::Bag.new(parameters)
|
12
|
+
end
|
13
|
+
|
14
|
+
def assert_export(expected, key, value, options = {})
|
15
|
+
actual = Env::Exporter.command(:export, key, value, options)
|
16
|
+
|
17
|
+
assert_equal(expected, actual)
|
18
|
+
end
|
19
|
+
|
20
|
+
def assert_unset(expected, key, options = {})
|
21
|
+
actual = Env::Exporter.command(:unset, key, options)
|
22
|
+
|
23
|
+
assert_equal(expected, actual)
|
24
|
+
end
|
25
|
+
|
26
|
+
def assert_autoenv_exporter(expected, bag, options = {})
|
27
|
+
io = StringIO.new()
|
28
|
+
Env::Exporter.autoenv_exporter(bag, options.merge({:io => io}))
|
29
|
+
|
30
|
+
actual = io.string().split(/\r?\n/)
|
31
|
+
|
32
|
+
assert_equal(expected, actual)
|
33
|
+
end
|
34
|
+
|
35
|
+
def assert_script_exporter(expected, bag, options = {})
|
36
|
+
io = StringIO.new()
|
37
|
+
Env::Exporter.script_exporter(bag, options.merge({:io => io}))
|
38
|
+
|
39
|
+
actual = io.string().split(/\r?\n/)
|
40
|
+
|
41
|
+
assert_equal(expected, actual)
|
42
|
+
end
|
43
|
+
|
44
|
+
def assert_terminal_exporter(expected, bag, options = {})
|
45
|
+
io = StringIO.new()
|
46
|
+
Env::Exporter.terminal_exporter(bag, options.merge({:io => io}))
|
47
|
+
|
48
|
+
actual = io.string().split(/\r?\n/)
|
49
|
+
|
50
|
+
assert_equal(expected, actual)
|
51
|
+
end
|
52
|
+
|
53
|
+
def assert_unset_commands(expected, bag, options = {})
|
54
|
+
actual = Env::Exporter.unset_commands(bag, options)
|
55
|
+
|
56
|
+
assert_equal(expected, actual)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_valid_exporter
|
60
|
+
assert_equal(true, Env::Exporter.valid?(:autoenv))
|
61
|
+
assert_equal(true, Env::Exporter.valid?(:script))
|
62
|
+
assert_equal(true, Env::Exporter.valid?(:terminal))
|
63
|
+
assert_equal(false, Env::Exporter.valid?(:foreman))
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_export_command
|
67
|
+
assert_export("export TEST=\\\"running\\ with\\ space\\\"", "TEST", "\"running with space\"")
|
68
|
+
assert_export("export TEST=running with space", "TEST", "running with space", :escape_value => false)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_unset_command
|
72
|
+
assert_unset("unset TEST", "TEST")
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_terminal_exporter_export
|
76
|
+
bag = create_bag({
|
77
|
+
:level1 => {
|
78
|
+
:level2 => {
|
79
|
+
:first => "first with space",
|
80
|
+
:second => "\"second\\"
|
81
|
+
},
|
82
|
+
:third => "third"
|
83
|
+
},
|
84
|
+
:existing => "downcase",
|
85
|
+
})
|
86
|
+
|
87
|
+
stub_env(:existing => "exist", :EXISTING => "exist") do
|
88
|
+
assert_terminal_exporter([
|
89
|
+
"export EXISTING=downcase",
|
90
|
+
"export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
|
91
|
+
"export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
|
92
|
+
"export LEVEL1_THIRD=third",
|
93
|
+
], bag)
|
94
|
+
|
95
|
+
assert_terminal_exporter([
|
96
|
+
"export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
|
97
|
+
"export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
|
98
|
+
"export LEVEL1_THIRD=third",
|
99
|
+
], bag, :override => false)
|
100
|
+
|
101
|
+
assert_terminal_exporter([
|
102
|
+
"export EXISTING=downcase",
|
103
|
+
"export LEVEL1_LEVEL2_FIRST=first with space",
|
104
|
+
"export LEVEL1_LEVEL2_SECOND=\"second\\",
|
105
|
+
"export LEVEL1_THIRD=third",
|
106
|
+
], bag, :type => :export, :override => true, :escape_value => false)
|
107
|
+
|
108
|
+
default_namer = Env::Namer.default(".")
|
109
|
+
prefix_namer = Env::Namer.prefix("CONFIG", default_namer)
|
110
|
+
|
111
|
+
assert_terminal_exporter([
|
112
|
+
"export CONFIG.EXISTING=downcase",
|
113
|
+
"export CONFIG.LEVEL1.LEVEL2.FIRST=first with space",
|
114
|
+
"export CONFIG.LEVEL1.LEVEL2.SECOND=\"second\\",
|
115
|
+
"export CONFIG.LEVEL1.THIRD=third",
|
116
|
+
], bag, :override => true, :escape_value => false, :namer => prefix_namer)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_terminal_exporter_unset
|
121
|
+
bag = create_bag({
|
122
|
+
:level1 => {
|
123
|
+
:level2 => {
|
124
|
+
:first => "first",
|
125
|
+
:second => "second"
|
126
|
+
},
|
127
|
+
:third => "third"
|
128
|
+
},
|
129
|
+
:existing => "downcase",
|
130
|
+
})
|
131
|
+
|
132
|
+
stub_env(:existing => "exist", :EXISTING => "exist") do
|
133
|
+
assert_terminal_exporter([
|
134
|
+
"unset EXISTING",
|
135
|
+
"unset LEVEL1_LEVEL2_FIRST",
|
136
|
+
"unset LEVEL1_LEVEL2_SECOND",
|
137
|
+
"unset LEVEL1_THIRD",
|
138
|
+
], bag, :type => :unset)
|
139
|
+
|
140
|
+
assert_terminal_exporter([
|
141
|
+
"unset LEVEL1_LEVEL2_FIRST",
|
142
|
+
"unset LEVEL1_LEVEL2_SECOND",
|
143
|
+
"unset LEVEL1_THIRD",
|
144
|
+
], bag, :override => false, :type => :unset)
|
145
|
+
|
146
|
+
default_namer = Env::Namer.default(".")
|
147
|
+
prefix_namer = Env::Namer.prefix("CONFIG", default_namer)
|
148
|
+
|
149
|
+
assert_terminal_exporter([
|
150
|
+
"unset CONFIG.EXISTING",
|
151
|
+
"unset CONFIG.LEVEL1.LEVEL2.FIRST",
|
152
|
+
"unset CONFIG.LEVEL1.LEVEL2.SECOND",
|
153
|
+
"unset CONFIG.LEVEL1.THIRD",
|
154
|
+
], bag, :override => true, :namer => prefix_namer, :type => :unset)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_autoenv_exporter
|
159
|
+
bag = create_bag({
|
160
|
+
:level1 => {
|
161
|
+
:level2 => {
|
162
|
+
:first => "first",
|
163
|
+
:second => "second"
|
164
|
+
},
|
165
|
+
:third => "third"
|
166
|
+
},
|
167
|
+
:existing => "downcase",
|
168
|
+
})
|
169
|
+
|
170
|
+
assert_autoenv_exporter([
|
171
|
+
"export EXISTING=downcase",
|
172
|
+
"export LEVEL1_LEVEL2_FIRST=first",
|
173
|
+
"export LEVEL1_LEVEL2_SECOND=second",
|
174
|
+
"export LEVEL1_THIRD=third",
|
175
|
+
], bag, :type => :export)
|
176
|
+
|
177
|
+
assert_autoenv_exporter([
|
178
|
+
"unset EXISTING",
|
179
|
+
"unset LEVEL1_LEVEL2_FIRST",
|
180
|
+
"unset LEVEL1_LEVEL2_SECOND",
|
181
|
+
"unset LEVEL1_THIRD",
|
182
|
+
], bag, :type => :unset)
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_script_exporter
|
186
|
+
bag = create_bag({
|
187
|
+
:level1 => {
|
188
|
+
:level2 => {
|
189
|
+
:first => "first",
|
190
|
+
:second => "second"
|
191
|
+
},
|
192
|
+
:third => "third"
|
193
|
+
},
|
194
|
+
:existing => "downcase",
|
195
|
+
})
|
196
|
+
|
197
|
+
assert_script_exporter([
|
198
|
+
"#!/bin/env sh",
|
199
|
+
"",
|
200
|
+
"export EXISTING=downcase",
|
201
|
+
"export LEVEL1_LEVEL2_FIRST=first",
|
202
|
+
"export LEVEL1_LEVEL2_SECOND=second",
|
203
|
+
"export LEVEL1_THIRD=third",
|
204
|
+
], bag, :type => :export)
|
205
|
+
|
206
|
+
assert_script_exporter([
|
207
|
+
"#!/bin/env sh",
|
208
|
+
"",
|
209
|
+
"unset EXISTING",
|
210
|
+
"unset LEVEL1_LEVEL2_FIRST",
|
211
|
+
"unset LEVEL1_LEVEL2_SECOND",
|
212
|
+
"unset LEVEL1_THIRD",
|
213
|
+
], bag, :type => :unset)
|
214
|
+
end
|
215
|
+
|
216
|
+
def replace_env(variables)
|
217
|
+
ENV.clear()
|
218
|
+
|
219
|
+
variables = Hash[variables.map do |name, value|
|
220
|
+
[name.to_s, value]
|
221
|
+
end]
|
222
|
+
|
223
|
+
ENV.update(variables)
|
224
|
+
end
|
225
|
+
|
226
|
+
def stub_env(new = {})
|
227
|
+
old = ENV.to_hash()
|
228
|
+
|
229
|
+
replace_env(new)
|
230
|
+
yield
|
231
|
+
|
232
|
+
ensure
|
233
|
+
replace_env(old)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nugrant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthieu Vachon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -80,7 +80,8 @@ files:
|
|
80
80
|
- lib/nugrant/bag.rb
|
81
81
|
- lib/nugrant/config.rb
|
82
82
|
- lib/nugrant/helper/bag.rb
|
83
|
-
- lib/nugrant/helper/env.rb
|
83
|
+
- lib/nugrant/helper/env/exporter.rb
|
84
|
+
- lib/nugrant/helper/env/namer.rb
|
84
85
|
- lib/nugrant/helper/stack.rb
|
85
86
|
- lib/nugrant/helper/yaml.rb
|
86
87
|
- lib/nugrant/parameters.rb
|
@@ -99,7 +100,7 @@ files:
|
|
99
100
|
- lib/vagrant_init.rb
|
100
101
|
- locales/en.yml
|
101
102
|
- nugrant.gemspec
|
102
|
-
- test/lib/nugrant/helper/
|
103
|
+
- test/lib/nugrant/helper/env/test_exporter.rb
|
103
104
|
- test/lib/nugrant/helper/test_stack.rb
|
104
105
|
- test/lib/nugrant/test_bag.rb
|
105
106
|
- test/lib/nugrant/test_config.rb
|
@@ -179,7 +180,7 @@ signing_key:
|
|
179
180
|
specification_version: 4
|
180
181
|
summary: Library to handle user specific parameters from various location.
|
181
182
|
test_files:
|
182
|
-
- test/lib/nugrant/helper/
|
183
|
+
- test/lib/nugrant/helper/env/test_exporter.rb
|
183
184
|
- test/lib/nugrant/helper/test_stack.rb
|
184
185
|
- test/lib/nugrant/test_bag.rb
|
185
186
|
- test/lib/nugrant/test_config.rb
|
data/lib/nugrant/helper/env.rb
DELETED
@@ -1,232 +0,0 @@
|
|
1
|
-
require 'shellwords'
|
2
|
-
|
3
|
-
module Nugrant
|
4
|
-
module Helper
|
5
|
-
class Env
|
6
|
-
|
7
|
-
@@DEFAULT_SCRIPT_PATH = "./nugrant2env.sh"
|
8
|
-
|
9
|
-
##
|
10
|
-
# Notes on `namer`
|
11
|
-
#
|
12
|
-
# A namer is a lambda taking as argument an array of segments
|
13
|
-
# that should return a string representation of those segments.
|
14
|
-
# How the segments are transformed to a string is up to the
|
15
|
-
# namer. By using various namer, we can change how a bag key
|
16
|
-
# is transformed into and environment variable name. This is
|
17
|
-
# like the strategy pattern.
|
18
|
-
#
|
19
|
-
|
20
|
-
##
|
21
|
-
# Returns the default namer, which join segments together
|
22
|
-
# using a character and upcase the result.
|
23
|
-
#
|
24
|
-
# @param `char` The character used to join segments together, default to `"_"`.
|
25
|
-
#
|
26
|
-
# @return A lambda that will simply joins segment using the `char` argument
|
27
|
-
# and upcase the result.
|
28
|
-
#
|
29
|
-
def self.default_namer(char = "_")
|
30
|
-
lambda do |segments|
|
31
|
-
segments.join(char).upcase()
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# Returns the prefix namer, which add a prefix to segments
|
37
|
-
# and delegate its work to another namer.
|
38
|
-
#
|
39
|
-
# @param prefix The prefix to add to segments.
|
40
|
-
# @param delegate_namer A namer that will be used to transform the prefixed segments.
|
41
|
-
#
|
42
|
-
# @return A lambda that will simply add prefix to segments and will call
|
43
|
-
# the delegate_namer with those new segments.
|
44
|
-
#
|
45
|
-
def self.prefix_namer(prefix, delegate_namer)
|
46
|
-
lambda do |segments|
|
47
|
-
delegate_namer.call([prefix] + segments)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.commands(type, bag, options = {})
|
52
|
-
# TODO: Replace by a map type => function name
|
53
|
-
case
|
54
|
-
when type == :export
|
55
|
-
export_commands(bag, options)
|
56
|
-
when type == :unset
|
57
|
-
unset_commands(bag, options)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
##
|
62
|
-
# Generate the list of export commands that must be
|
63
|
-
# executed so each bag variables is export to an
|
64
|
-
# environment variables
|
65
|
-
#
|
66
|
-
# @param bag The bag to export to environment variables
|
67
|
-
#
|
68
|
-
# @return A list of commands that can be used to
|
69
|
-
# export the bag to environment variables.
|
70
|
-
#
|
71
|
-
# Options:
|
72
|
-
# * :escape_value (true) => If true, escape the value to export.
|
73
|
-
#
|
74
|
-
# * :namer (nil) => A block taking as options the full path of
|
75
|
-
# an export variable key and return what
|
76
|
-
# the name the should be exported.
|
77
|
-
#
|
78
|
-
# * :override (true) => If true, an export command will be put
|
79
|
-
# in the list even if it already exist in
|
80
|
-
# the ENV array.
|
81
|
-
#
|
82
|
-
def self.export_commands(bag, options = {})
|
83
|
-
namer = options[:namer] || default_namer()
|
84
|
-
override = options.fetch(:override, true)
|
85
|
-
|
86
|
-
commands = []
|
87
|
-
walk_bag(bag) do |segments, key, value|
|
88
|
-
key = namer.call(segments)
|
89
|
-
|
90
|
-
commands << export_command(key, value, options) if override or not ENV[key]
|
91
|
-
end
|
92
|
-
|
93
|
-
commands
|
94
|
-
end
|
95
|
-
|
96
|
-
##
|
97
|
-
# Generate the list of unset commands that must be
|
98
|
-
# executed so each bag variables is unset from the
|
99
|
-
# environment variables
|
100
|
-
#
|
101
|
-
# @param bag The bag to unset environment variables
|
102
|
-
#
|
103
|
-
# @return A list of commands that can be used to
|
104
|
-
# unset the bag from environment variables.
|
105
|
-
#
|
106
|
-
# Options:
|
107
|
-
# * :namer (nil) => A block taking as options the full path of
|
108
|
-
# an export variable key and return what
|
109
|
-
# the name the should be exported.
|
110
|
-
#
|
111
|
-
# * :override (true) => If true, an export command will be put
|
112
|
-
# in the list even if it already exist in
|
113
|
-
# the ENV array.
|
114
|
-
#
|
115
|
-
def self.unset_commands(bag, options = {})
|
116
|
-
namer = options[:namer] || default_namer()
|
117
|
-
override = options.fetch(:override, true)
|
118
|
-
|
119
|
-
commands = []
|
120
|
-
walk_bag(bag) do |segments, key, value|
|
121
|
-
key = namer.call(segments)
|
122
|
-
|
123
|
-
commands << unset_command(key, value, options) if override or not ENV[key]
|
124
|
-
end
|
125
|
-
|
126
|
-
commands
|
127
|
-
end
|
128
|
-
|
129
|
-
##
|
130
|
-
# Returns a string representation of the command
|
131
|
-
# that needs to be used on the current platform
|
132
|
-
# to export an environment variable.
|
133
|
-
#
|
134
|
-
# @param key The key of the environment variable to export.
|
135
|
-
# It cannot be nil.
|
136
|
-
# @param value The value of the environment variable to export
|
137
|
-
#
|
138
|
-
# @return The export command, as a string
|
139
|
-
#
|
140
|
-
# Options:
|
141
|
-
# * :escape_value (true) => If true, escape the value to export.
|
142
|
-
#
|
143
|
-
def self.export_command(key, value, options = {})
|
144
|
-
value = value.to_s()
|
145
|
-
value = Shellwords.escape(value) if options[:escape_value] == nil || options[:escape_value]
|
146
|
-
|
147
|
-
# TODO: Handle platform differently
|
148
|
-
"export #{key}=#{value}"
|
149
|
-
end
|
150
|
-
|
151
|
-
##
|
152
|
-
# Returns a string representation of the command
|
153
|
-
# that needs to be used on the current platform
|
154
|
-
# to unset an environment variable.
|
155
|
-
#
|
156
|
-
# @param key The key of the environment variable to export.
|
157
|
-
# It cannot be nil.
|
158
|
-
#
|
159
|
-
# @return The unset command, as a string
|
160
|
-
#
|
161
|
-
def self.unset_command(key, value, options = {})
|
162
|
-
# TODO: Handle platform differently
|
163
|
-
"unset #{key}"
|
164
|
-
end
|
165
|
-
|
166
|
-
##
|
167
|
-
# Creates a bash script containing the commands that are required
|
168
|
-
# to export or unset a bunch of environment variables taken from the
|
169
|
-
# bag.
|
170
|
-
#
|
171
|
-
# @param bag The bag to create the script for.
|
172
|
-
#
|
173
|
-
# @return (side-effect) Creates a script file containing commands
|
174
|
-
# to export or unset environment variables for
|
175
|
-
# bag.
|
176
|
-
#
|
177
|
-
# Options:
|
178
|
-
# * :type => The type of command, default to :export
|
179
|
-
# * :script_path => The path where to write the script, defaults to `./nugrant2env.sh`.
|
180
|
-
# * See commands, export_commands and unset_commands for further options.
|
181
|
-
#
|
182
|
-
def self.write_script(bag, options = {})
|
183
|
-
file = File.open(File.expand_path(options[:script_path] || @@DEFAULT_SCRIPT_PATH), "w")
|
184
|
-
|
185
|
-
file.puts("#!/bin/env sh")
|
186
|
-
file.puts()
|
187
|
-
|
188
|
-
write_commands(bag, options, file)
|
189
|
-
ensure
|
190
|
-
file.close() if file
|
191
|
-
end
|
192
|
-
|
193
|
-
##
|
194
|
-
# Creates a bash script containing the commands that are required
|
195
|
-
# to export or unset a bunch of environment variables taken from the
|
196
|
-
# bag.
|
197
|
-
#
|
198
|
-
# The
|
199
|
-
#
|
200
|
-
# @param bag The bag to create the script for.
|
201
|
-
# @param io The io where to output the commands, defaults to $stdout.
|
202
|
-
#
|
203
|
-
# @return (side-effect) Outputs to io the commands generated.
|
204
|
-
#
|
205
|
-
# Options:
|
206
|
-
# * :type => The type of command, default to :export
|
207
|
-
# * See commands, export_commands and unset_commands for further options.
|
208
|
-
#
|
209
|
-
def self.write_commands(bag, options = {}, io = $stdout)
|
210
|
-
commands = commands(options[:type] || :export, bag, options)
|
211
|
-
|
212
|
-
commands.each do |command|
|
213
|
-
io.puts(command)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
private
|
218
|
-
|
219
|
-
def self.walk_bag(bag, parents = [], &block)
|
220
|
-
commands = []
|
221
|
-
|
222
|
-
bag.each do |key, value|
|
223
|
-
segments = parents + [key]
|
224
|
-
nested_bag = value.kind_of?(Nugrant::Bag)
|
225
|
-
|
226
|
-
walk_bag(value, segments, &block) if nested_bag
|
227
|
-
yield segments, key, value if not nested_bag
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
@@ -1,150 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
|
3
|
-
require 'nugrant/bag'
|
4
|
-
require 'nugrant/helper/env'
|
5
|
-
|
6
|
-
module Nugrant
|
7
|
-
module Helper
|
8
|
-
class TestEnv < ::Minitest::Test
|
9
|
-
def create_bag(parameters)
|
10
|
-
return Nugrant::Bag.new(parameters)
|
11
|
-
end
|
12
|
-
|
13
|
-
def assert_export(expected, key, value, options = {})
|
14
|
-
actual = Helper::Env.export_command(key, value, options)
|
15
|
-
|
16
|
-
assert_equal(expected, actual)
|
17
|
-
end
|
18
|
-
|
19
|
-
def assert_unset(expected, key, options = {})
|
20
|
-
actual = Helper::Env.unset_command(key, options)
|
21
|
-
|
22
|
-
assert_equal(expected, actual)
|
23
|
-
end
|
24
|
-
|
25
|
-
def assert_export_commands(expected, bag, options = {})
|
26
|
-
actual = Helper::Env.export_commands(bag, options)
|
27
|
-
|
28
|
-
assert_equal(expected.sort!(), actual.sort!())
|
29
|
-
end
|
30
|
-
|
31
|
-
def assert_unset_commands(expected, bag, options = {})
|
32
|
-
actual = Helper::Env.unset_commands(bag, options)
|
33
|
-
|
34
|
-
assert_equal(expected, actual)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_export_command
|
38
|
-
assert_export("export TEST=\\\"running\\ with\\ space\\\"", "TEST", "\"running with space\"")
|
39
|
-
assert_export("export TEST=running with space", "TEST", "running with space", :escape_value => false)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_export_commands
|
43
|
-
bag = create_bag({
|
44
|
-
:existing => "downcase",
|
45
|
-
:level1 => {
|
46
|
-
:level2 => {
|
47
|
-
:first => "first with space",
|
48
|
-
:second => "\"second\\"
|
49
|
-
},
|
50
|
-
:third => "third"
|
51
|
-
}
|
52
|
-
})
|
53
|
-
|
54
|
-
stub_env(:existing => "exist", :EXISTING => "exist") do
|
55
|
-
assert_export_commands([
|
56
|
-
"export EXISTING=downcase",
|
57
|
-
"export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
|
58
|
-
"export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
|
59
|
-
"export LEVEL1_THIRD=third",
|
60
|
-
], bag)
|
61
|
-
|
62
|
-
assert_export_commands([
|
63
|
-
"export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
|
64
|
-
"export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
|
65
|
-
"export LEVEL1_THIRD=third",
|
66
|
-
], bag, :override => false)
|
67
|
-
|
68
|
-
assert_export_commands([
|
69
|
-
"export EXISTING=downcase",
|
70
|
-
"export LEVEL1_LEVEL2_FIRST=first with space",
|
71
|
-
"export LEVEL1_LEVEL2_SECOND=\"second\\",
|
72
|
-
"export LEVEL1_THIRD=third",
|
73
|
-
], bag, :override => true, :escape_value => false)
|
74
|
-
|
75
|
-
default_namer = Helper::Env.default_namer(".")
|
76
|
-
prefix_namer = Helper::Env.prefix_namer("CONFIG", default_namer)
|
77
|
-
|
78
|
-
assert_export_commands([
|
79
|
-
"export CONFIG.EXISTING=downcase",
|
80
|
-
"export CONFIG.LEVEL1.LEVEL2.FIRST=first with space",
|
81
|
-
"export CONFIG.LEVEL1.LEVEL2.SECOND=\"second\\",
|
82
|
-
"export CONFIG.LEVEL1.THIRD=third",
|
83
|
-
], bag, :override => true, :escape_value => false, :namer => prefix_namer)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_unset_command
|
88
|
-
assert_unset("unset TEST", "TEST")
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_unset_commands
|
92
|
-
bag = create_bag({
|
93
|
-
:existing => "downcase",
|
94
|
-
:level1 => {
|
95
|
-
:level2 => {
|
96
|
-
:first => "first",
|
97
|
-
:second => "second"
|
98
|
-
},
|
99
|
-
:third => "third"
|
100
|
-
}
|
101
|
-
})
|
102
|
-
|
103
|
-
stub_env(:existing => "exist", :EXISTING => "exist") do
|
104
|
-
assert_unset_commands([
|
105
|
-
"unset EXISTING",
|
106
|
-
"unset LEVEL1_LEVEL2_FIRST",
|
107
|
-
"unset LEVEL1_LEVEL2_SECOND",
|
108
|
-
"unset LEVEL1_THIRD",
|
109
|
-
], bag)
|
110
|
-
|
111
|
-
assert_unset_commands([
|
112
|
-
"unset LEVEL1_LEVEL2_FIRST",
|
113
|
-
"unset LEVEL1_LEVEL2_SECOND",
|
114
|
-
"unset LEVEL1_THIRD",
|
115
|
-
], bag, :override => false)
|
116
|
-
|
117
|
-
default_namer = Helper::Env.default_namer(".")
|
118
|
-
prefix_namer = Helper::Env.prefix_namer("CONFIG", default_namer)
|
119
|
-
|
120
|
-
assert_unset_commands([
|
121
|
-
"unset CONFIG.EXISTING",
|
122
|
-
"unset CONFIG.LEVEL1.LEVEL2.FIRST",
|
123
|
-
"unset CONFIG.LEVEL1.LEVEL2.SECOND",
|
124
|
-
"unset CONFIG.LEVEL1.THIRD",
|
125
|
-
], bag, :override => true, :namer => prefix_namer)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def replace_env(variables)
|
130
|
-
ENV.clear()
|
131
|
-
|
132
|
-
variables = Hash[variables.map do |name, value|
|
133
|
-
[name.to_s, value]
|
134
|
-
end]
|
135
|
-
|
136
|
-
ENV.update(variables)
|
137
|
-
end
|
138
|
-
|
139
|
-
def stub_env(new = {})
|
140
|
-
old = ENV.to_hash()
|
141
|
-
|
142
|
-
replace_env(new)
|
143
|
-
yield
|
144
|
-
|
145
|
-
ensure
|
146
|
-
replace_env(old)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|