nugrant 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +23 -23
  3. data/.travis.yml +15 -10
  4. data/CHANGELOG.md +347 -338
  5. data/CONTRIBUTORS.md +7 -5
  6. data/Gemfile +13 -13
  7. data/README.md +601 -601
  8. data/lib/nugrant/bag.rb +264 -264
  9. data/lib/nugrant/config.rb +201 -201
  10. data/lib/nugrant/helper/bag.rb +38 -38
  11. data/lib/nugrant/helper/env/exporter.rb +195 -195
  12. data/lib/nugrant/helper/env/namer.rb +47 -47
  13. data/lib/nugrant/helper/parameters.rb +12 -12
  14. data/lib/nugrant/helper/stack.rb +86 -86
  15. data/lib/nugrant/mixin/parameters.rb +178 -178
  16. data/lib/nugrant/parameters.rb +29 -29
  17. data/lib/nugrant/vagrant/errors.rb +35 -35
  18. data/lib/nugrant/vagrant/v2/action/auto_export.rb +45 -45
  19. data/lib/nugrant/vagrant/v2/action.rb +17 -17
  20. data/lib/nugrant/vagrant/v2/command/env.rb +118 -118
  21. data/lib/nugrant/vagrant/v2/command/parameters.rb +153 -153
  22. data/lib/nugrant/vagrant/v2/command/restricted_keys.rb +64 -64
  23. data/lib/nugrant/vagrant/v2/command/root.rb +95 -95
  24. data/lib/nugrant/vagrant/v2/config/user.rb +29 -29
  25. data/lib/nugrant/vagrant/v2/helper.rb +96 -96
  26. data/lib/nugrant/vagrant/v2/plugin.rb +34 -34
  27. data/lib/nugrant/version.rb +3 -3
  28. data/lib/nugrant.rb +31 -31
  29. data/locales/en.yml +24 -24
  30. data/locales/fr.yml +24 -24
  31. data/nugrant.gemspec +32 -32
  32. data/test/lib/nugrant/helper/env/test_exporter.rb +238 -238
  33. data/test/lib/nugrant/helper/test_bag.rb +16 -16
  34. data/test/lib/nugrant/helper/test_parameters.rb +17 -17
  35. data/test/lib/nugrant/helper/test_stack.rb +152 -152
  36. data/test/lib/nugrant/test_bag.rb +450 -450
  37. data/test/lib/nugrant/test_config.rb +201 -201
  38. data/test/lib/nugrant/test_parameters.rb +438 -438
  39. data/test/lib/test_helper.rb +3 -3
  40. data/test/resources/README.md +52 -52
  41. data/test/resources/json/params_current_1.json +6 -6
  42. data/test/resources/json/params_current_2.json +29 -29
  43. data/test/resources/json/params_user_nil_values.json +9 -9
  44. data/test/resources/vagrantfiles/v2.auto_export +13 -13
  45. data/test/resources/vagrantfiles/v2.bag_inside_array +15 -15
  46. data/test/resources/vagrantfiles/v2.defaults_mixed_string_symbols +18 -18
  47. data/test/resources/vagrantfiles/v2.defaults_null_values_in_vagrantuser +23 -23
  48. data/test/resources/vagrantfiles/v2.defaults_using_string +18 -18
  49. data/test/resources/vagrantfiles/v2.defaults_using_symbol +18 -18
  50. data/test/resources/vagrantfiles/v2.empty +2 -2
  51. data/test/resources/vagrantfiles/v2.fake +29 -29
  52. data/test/resources/vagrantfiles/v2.missing_parameter +3 -3
  53. data/test/resources/vagrantfiles/v2.real +22 -22
  54. data/test/resources/yaml/params_array.yml +5 -5
  55. data/test/resources/yaml/params_boolean.yml +1 -1
  56. data/test/resources/yaml/params_combinations.yml +72 -72
  57. data/test/resources/yaml/params_current_1.yml +4 -4
  58. data/test/resources/yaml/params_current_2.yml +23 -23
  59. data/test/resources/yaml/params_defaults_at_root.yml +1 -1
  60. data/test/resources/yaml/params_defaults_not_at_root.yml +2 -2
  61. data/test/resources/yaml/params_list.yml +2 -2
  62. data/test/resources/yaml/params_numeric_key.yml +3 -3
  63. data/test/resources/yaml/params_simple.yml +1 -1
  64. data/test/resources/yaml/params_system_1.yml +4 -4
  65. data/test/resources/yaml/params_system_2.yml +25 -25
  66. data/test/resources/yaml/params_unix_eol.yml +3 -3
  67. data/test/resources/yaml/params_user_1.yml +4 -4
  68. data/test/resources/yaml/params_user_2.yml +23 -23
  69. data/test/resources/yaml/params_user_nil_values.yml +5 -5
  70. data/test/resources/yaml/params_windows_eol.yml +3 -3
  71. metadata +12 -13
@@ -1,195 +1,195 @@
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 parents 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
- bag.walk do |path, key, value|
129
- key = namer.call(path)
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
- end
193
- end
194
- end
195
- end
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), "wb"))
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), "wb"))
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 parents 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
+ bag.walk do |path, key, value|
129
+ key = namer.call(path)
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
+ end
193
+ end
194
+ end
195
+ end
@@ -1,47 +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
+ 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,12 +1,12 @@
1
- require 'nugrant/parameters'
2
-
3
- module Nugrant
4
- module Helper
5
- module Parameters
6
- def self.restricted_keys()
7
- methods = Nugrant::Parameters.instance_methods() + Nugrant::Bag.instance_methods()
8
- methods.uniq!
9
- end
10
- end
11
- end
12
- end
1
+ require 'nugrant/parameters'
2
+
3
+ module Nugrant
4
+ module Helper
5
+ module Parameters
6
+ def self.restricted_keys()
7
+ methods = Nugrant::Parameters.instance_methods() + Nugrant::Bag.instance_methods()
8
+ methods.uniq!
9
+ end
10
+ end
11
+ end
12
+ end