nugrant 1.4.2 → 2.0.0.dev1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +1 -2
  3. data/.travis.yml +0 -6
  4. data/CHANGELOG.md +3 -79
  5. data/Gemfile +8 -8
  6. data/README.md +10 -146
  7. data/lib/nugrant.rb +4 -19
  8. data/lib/nugrant/bag.rb +8 -12
  9. data/lib/nugrant/config.rb +55 -24
  10. data/lib/nugrant/helper/bag.rb +19 -17
  11. data/lib/nugrant/parameters.rb +37 -9
  12. data/lib/nugrant/vagrant/v1/command/parameters.rb +13 -13
  13. data/lib/nugrant/vagrant/v1/command/root.rb +2 -7
  14. data/lib/nugrant/vagrant/v1/config/user.rb +1 -10
  15. data/lib/nugrant/vagrant/v2/command/parameters.rb +1 -1
  16. data/lib/nugrant/vagrant/v2/command/root.rb +2 -7
  17. data/lib/nugrant/vagrant/v2/config/user.rb +1 -10
  18. data/lib/nugrant/version.rb +1 -1
  19. data/nugrant.gemspec +1 -4
  20. data/test/lib/nugrant/test_bag.rb +19 -33
  21. data/test/lib/nugrant/test_config.rb +92 -100
  22. data/test/lib/nugrant/test_parameters.rb +186 -203
  23. data/test/resources/README.md +11 -11
  24. data/test/resources/{vagrantfiles/v1.empty → Vagrantfile.v1.empty} +0 -0
  25. data/test/resources/{vagrantfiles/v1.fake → Vagrantfile.v1.fake} +0 -0
  26. data/test/resources/{vagrantfiles/v1.real → Vagrantfile.v1.real} +0 -0
  27. data/test/resources/{vagrantfiles/v2.empty → Vagrantfile.v2.empty} +0 -0
  28. data/test/resources/{vagrantfiles/v2.fake → Vagrantfile.v2.fake} +1 -4
  29. data/test/resources/{vagrantfiles/v2.real → Vagrantfile.v2.real} +0 -0
  30. data/test/resources/json/params_current_1.json +6 -0
  31. data/test/resources/json/params_current_2.json +29 -0
  32. data/test/resources/{yml → yaml}/params_array.yml +0 -0
  33. data/test/resources/{yml → yaml}/params_boolean.yml +0 -0
  34. data/test/resources/{yml → yaml}/params_combinations.yml +0 -0
  35. data/test/resources/yaml/params_current_1.yml +4 -0
  36. data/test/resources/yaml/params_current_2.yml +23 -0
  37. data/test/resources/{yml → yaml}/params_defaults_at_root.yml +0 -0
  38. data/test/resources/{yml → yaml}/params_defaults_not_at_root.yml +0 -0
  39. data/test/resources/{yml → yaml}/params_empty.yml +0 -0
  40. data/test/resources/{yml → yaml}/params_list.yml +0 -0
  41. data/test/resources/{yml → yaml}/params_simple.yml +0 -0
  42. data/test/resources/{yml → yaml}/params_system_1.yml +0 -0
  43. data/test/resources/{yml → yaml}/params_system_2.yml +0 -0
  44. data/test/resources/{yml → yaml}/params_unix_eol.yml +0 -0
  45. data/test/resources/{yml → yaml}/params_user_1.yml +0 -0
  46. data/test/resources/{yml → yaml}/params_user_2.yml +0 -0
  47. data/test/resources/{yml → yaml}/params_windows_eol.yml +0 -0
  48. metadata +71 -95
  49. data/lib/nugrant/helper/env/exporter.rb +0 -208
  50. data/lib/nugrant/helper/env/namer.rb +0 -47
  51. data/lib/nugrant/helper/stack.rb +0 -86
  52. data/lib/nugrant/vagrant/errors.rb +0 -27
  53. data/lib/nugrant/vagrant/v1/command/env.rb +0 -107
  54. data/lib/nugrant/vagrant/v2/command/env.rb +0 -107
  55. data/locales/en.yml +0 -13
  56. data/test/lib/nugrant/helper/env/test_exporter.rb +0 -238
  57. data/test/lib/nugrant/helper/test_stack.rb +0 -149
  58. data/test/resources/json/params_project_1.json +0 -6
  59. data/test/resources/json/params_project_2.json +0 -29
  60. data/test/resources/json/params_user_nil_values.json +0 -9
  61. data/test/resources/vagrantfiles/v1.defaults_mixed_string_symbols +0 -18
  62. data/test/resources/vagrantfiles/v1.defaults_using_string +0 -18
  63. data/test/resources/vagrantfiles/v1.defaults_using_symbol +0 -18
  64. data/test/resources/vagrantfiles/v1.missing_parameter +0 -3
  65. data/test/resources/vagrantfiles/v2.defaults_mixed_string_symbols +0 -20
  66. data/test/resources/vagrantfiles/v2.defaults_null_values_in_vagrantuser +0 -25
  67. data/test/resources/vagrantfiles/v2.defaults_using_string +0 -20
  68. data/test/resources/vagrantfiles/v2.defaults_using_symbol +0 -20
  69. data/test/resources/vagrantfiles/v2.missing_parameter +0 -5
  70. data/test/resources/yml/params_project_1.yml +0 -4
  71. data/test/resources/yml/params_project_2.yml +0 -23
  72. data/test/resources/yml/params_user_nil_values.yml +0 -5
@@ -4,12 +4,12 @@ that test merge possibilities.
4
4
  Naming convetions
5
5
  -----------------
6
6
 
7
- The filename used a specific convetion:
7
+ The filename uses a specific convention:
8
8
 
9
9
  params_*kind*_*level*.[yml|json]
10
10
 
11
- The kind is one of: [`project`|`user`|`system`] and defines which
12
- responsability they will hold. The order is `project` overrides
11
+ The kind is one of: [`current`|`user`|`system`] and defines which
12
+ responsability they will hold. The order is `current` overrides
13
13
  `user` overrides `system`.
14
14
 
15
15
  Inside file, keys have special meaning. They define how
@@ -19,12 +19,12 @@ to represent each possibilities.
19
19
  key = "1.1.1"
20
20
 
21
21
  Each element represent a specific kind. Read from left to
22
- right, they are assigned to `project`, `user` and `system`
22
+ right, they are assigned to `current`, `user` and `system`
23
23
  respectively.
24
24
 
25
25
  So,
26
26
 
27
- `project` `user` `system`
27
+ `current` `user` `system`
28
28
  1 . 1 . 1
29
29
 
30
30
  A 1 signify that the file kind *column* will have a key "1.1.1" set
@@ -32,8 +32,8 @@ to value *kind*. A 0 means the key is not set. With this in mind,
32
32
  it is easy to reason about the value that will need to be asserted
33
33
  for key "1.1.1" on level file *level*.
34
34
 
35
- # params_project_1.yml file
36
- "1.1.1": "project"
35
+ # params_current_1.yml file
36
+ "1.1.1": "current"
37
37
 
38
38
  # params_user_1.yml file
39
39
  "1.1.1": "user"
@@ -42,11 +42,11 @@ for key "1.1.1" on level file *level*.
42
42
  "1.1.1": "system"
43
43
 
44
44
  # Value to assert
45
- assert("project", parameters.get("1.1.1"))
45
+ assert("current", parameters.get("1.1.1"))
46
46
 
47
47
  With the help of params_combinations, it is easy to create test file
48
48
  either for other formats or for more level. Copy the all keys expect
49
- "0.0.0" to the file. Say the file is of kind `project`, then for
50
- column `project`, each time a one is there, replace `base` by the
51
- kind (here `project`). The line that still have value base as the
49
+ "0.0.0" to the file. Say the file is of kind `current`, then for
50
+ column `current`, each time a one is there, replace `base` by the
51
+ kind (here `current`). The line that still have value base as the
52
52
  value must be deleted.
@@ -7,11 +7,8 @@ Vagrant.configure("2") do |config|
7
7
  'second' => "value2"
8
8
  }
9
9
  }
10
-
10
+
11
11
  config.user.local.each do |name, value|
12
12
  puts "Parameters #{name}: #{value}"
13
13
  end
14
-
15
- puts "User has local (symbol)" if config.user.has?(:local)
16
- puts "User has local (string)" if config.user.has?("local")
17
14
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "1.1.1": "current",
3
+ "1.1.0": "current",
4
+ "1.0.1": "current",
5
+ "1.0.0": "current"
6
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "1.1.1": {
3
+ "1.1.1": "current",
4
+ "1.1.0": "current",
5
+ "1.0.1": "current",
6
+ "1.0.0": "current"
7
+ },
8
+
9
+ "1.1.0": {
10
+ "1.1.1": "current",
11
+ "1.1.0": "current",
12
+ "1.0.1": "current",
13
+ "1.0.0": "current"
14
+ },
15
+
16
+ "1.0.1": {
17
+ "1.1.1": "current",
18
+ "1.1.0": "current",
19
+ "1.0.1": "current",
20
+ "1.0.0": "current"
21
+ },
22
+
23
+ "1.0.0": {
24
+ "1.1.1": "current",
25
+ "1.1.0": "current",
26
+ "1.0.1": "current",
27
+ "1.0.0": "current"
28
+ }
29
+ }
@@ -0,0 +1,4 @@
1
+ "1.1.1": "current"
2
+ "1.1.0": "current"
3
+ "1.0.1": "current"
4
+ "1.0.0": "current"
@@ -0,0 +1,23 @@
1
+ "1.1.1":
2
+ "1.1.1": "current"
3
+ "1.1.0": "current"
4
+ "1.0.1": "current"
5
+ "1.0.0": "current"
6
+
7
+ "1.1.0":
8
+ "1.1.1": "current"
9
+ "1.1.0": "current"
10
+ "1.0.1": "current"
11
+ "1.0.0": "current"
12
+
13
+ "1.0.1":
14
+ "1.1.1": "current"
15
+ "1.1.0": "current"
16
+ "1.0.1": "current"
17
+ "1.0.0": "current"
18
+
19
+ "1.0.0":
20
+ "1.1.1": "current"
21
+ "1.1.0": "current"
22
+ "1.0.1": "current"
23
+ "1.0.0": "current"
metadata CHANGED
@@ -1,36 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nugrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
5
- prerelease:
4
+ version: 2.0.0.dev1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Matthieu Vachon
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-01-11 00:00:00.000000000 Z
11
+ date: 2013-07-12 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: multi_json
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.8'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '1.8'
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: rake
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
17
  - - ! '>='
36
18
  - !ruby/object:Gem::Version
@@ -38,27 +20,10 @@ dependencies:
38
20
  type: :development
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
24
  - - ! '>='
44
25
  - !ruby/object:Gem::Version
45
26
  version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: minitest
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 5.0.8
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 5.0.8
62
27
  description: ! " Nugrant is a library to easily handle parameters that need to
63
28
  be\n injected into an application via different sources (system, user,\n project,
64
29
  defaults).\n\n Nugrant can also be directly used as a Vagrant plugin. By activating\n
@@ -87,115 +52,126 @@ files:
87
52
  - lib/nugrant/bag.rb
88
53
  - lib/nugrant/config.rb
89
54
  - lib/nugrant/helper/bag.rb
90
- - lib/nugrant/helper/env/exporter.rb
91
- - lib/nugrant/helper/env/namer.rb
92
- - lib/nugrant/helper/stack.rb
93
55
  - lib/nugrant/helper/yaml.rb
94
56
  - lib/nugrant/parameters.rb
95
- - lib/nugrant/vagrant/errors.rb
96
- - lib/nugrant/vagrant/v1/command/env.rb
97
57
  - lib/nugrant/vagrant/v1/command/parameters.rb
98
58
  - lib/nugrant/vagrant/v1/command/root.rb
99
59
  - lib/nugrant/vagrant/v1/config/user.rb
100
60
  - lib/nugrant/vagrant/v1/plugin.rb
101
- - lib/nugrant/vagrant/v2/command/env.rb
102
61
  - lib/nugrant/vagrant/v2/command/parameters.rb
103
62
  - lib/nugrant/vagrant/v2/command/root.rb
104
63
  - lib/nugrant/vagrant/v2/config/user.rb
105
64
  - lib/nugrant/vagrant/v2/plugin.rb
106
65
  - lib/nugrant/version.rb
107
66
  - lib/vagrant_init.rb
108
- - locales/en.yml
109
67
  - nugrant.gemspec
110
- - test/lib/nugrant/helper/env/test_exporter.rb
111
- - test/lib/nugrant/helper/test_stack.rb
112
68
  - test/lib/nugrant/test_bag.rb
113
69
  - test/lib/nugrant/test_config.rb
114
70
  - test/lib/nugrant/test_parameters.rb
115
71
  - test/resources/.vagrantuser
116
72
  - test/resources/README.md
73
+ - test/resources/Vagrantfile.v1.empty
74
+ - test/resources/Vagrantfile.v1.fake
75
+ - test/resources/Vagrantfile.v1.real
76
+ - test/resources/Vagrantfile.v2.empty
77
+ - test/resources/Vagrantfile.v2.fake
78
+ - test/resources/Vagrantfile.v2.real
117
79
  - test/resources/json/params_array.json
118
80
  - test/resources/json/params_boolean.json
119
81
  - test/resources/json/params_combinations.json
82
+ - test/resources/json/params_current_1.json
83
+ - test/resources/json/params_current_2.json
120
84
  - test/resources/json/params_defaults_at_root.json
121
85
  - test/resources/json/params_defaults_not_at_root.json
122
86
  - test/resources/json/params_empty.json
123
87
  - test/resources/json/params_list.json
124
- - test/resources/json/params_project_1.json
125
- - test/resources/json/params_project_2.json
126
88
  - test/resources/json/params_simple.json
127
89
  - test/resources/json/params_system_1.json
128
90
  - test/resources/json/params_system_2.json
129
91
  - test/resources/json/params_unix_eol.json
130
92
  - test/resources/json/params_user_1.json
131
93
  - test/resources/json/params_user_2.json
132
- - test/resources/json/params_user_nil_values.json
133
94
  - test/resources/json/params_windows_eol.json
134
- - test/resources/vagrantfiles/v1.defaults_mixed_string_symbols
135
- - test/resources/vagrantfiles/v1.defaults_using_string
136
- - test/resources/vagrantfiles/v1.defaults_using_symbol
137
- - test/resources/vagrantfiles/v1.empty
138
- - test/resources/vagrantfiles/v1.fake
139
- - test/resources/vagrantfiles/v1.missing_parameter
140
- - test/resources/vagrantfiles/v1.real
141
- - test/resources/vagrantfiles/v2.defaults_mixed_string_symbols
142
- - test/resources/vagrantfiles/v2.defaults_null_values_in_vagrantuser
143
- - test/resources/vagrantfiles/v2.defaults_using_string
144
- - test/resources/vagrantfiles/v2.defaults_using_symbol
145
- - test/resources/vagrantfiles/v2.empty
146
- - test/resources/vagrantfiles/v2.fake
147
- - test/resources/vagrantfiles/v2.missing_parameter
148
- - test/resources/vagrantfiles/v2.real
149
- - test/resources/yml/params_array.yml
150
- - test/resources/yml/params_boolean.yml
151
- - test/resources/yml/params_combinations.yml
152
- - test/resources/yml/params_defaults_at_root.yml
153
- - test/resources/yml/params_defaults_not_at_root.yml
154
- - test/resources/yml/params_empty.yml
155
- - test/resources/yml/params_list.yml
156
- - test/resources/yml/params_project_1.yml
157
- - test/resources/yml/params_project_2.yml
158
- - test/resources/yml/params_simple.yml
159
- - test/resources/yml/params_system_1.yml
160
- - test/resources/yml/params_system_2.yml
161
- - test/resources/yml/params_unix_eol.yml
162
- - test/resources/yml/params_user_1.yml
163
- - test/resources/yml/params_user_2.yml
164
- - test/resources/yml/params_user_nil_values.yml
165
- - test/resources/yml/params_windows_eol.yml
95
+ - test/resources/yaml/params_array.yml
96
+ - test/resources/yaml/params_boolean.yml
97
+ - test/resources/yaml/params_combinations.yml
98
+ - test/resources/yaml/params_current_1.yml
99
+ - test/resources/yaml/params_current_2.yml
100
+ - test/resources/yaml/params_defaults_at_root.yml
101
+ - test/resources/yaml/params_defaults_not_at_root.yml
102
+ - test/resources/yaml/params_empty.yml
103
+ - test/resources/yaml/params_list.yml
104
+ - test/resources/yaml/params_simple.yml
105
+ - test/resources/yaml/params_system_1.yml
106
+ - test/resources/yaml/params_system_2.yml
107
+ - test/resources/yaml/params_unix_eol.yml
108
+ - test/resources/yaml/params_user_1.yml
109
+ - test/resources/yaml/params_user_2.yml
110
+ - test/resources/yaml/params_windows_eol.yml
166
111
  homepage: https://github.com/maoueh/nugrant
167
112
  licenses: []
113
+ metadata: {}
168
114
  post_install_message:
169
115
  rdoc_options: []
170
116
  require_paths:
171
117
  - lib
172
118
  required_ruby_version: !ruby/object:Gem::Requirement
173
- none: false
174
119
  requirements:
175
120
  - - ! '>='
176
121
  - !ruby/object:Gem::Version
177
122
  version: '0'
178
- segments:
179
- - 0
180
- hash: -348904095
181
123
  required_rubygems_version: !ruby/object:Gem::Requirement
182
- none: false
183
124
  requirements:
184
- - - ! '>='
125
+ - - ! '>'
185
126
  - !ruby/object:Gem::Version
186
- version: '0'
187
- segments:
188
- - 0
189
- hash: -348904095
127
+ version: 1.3.1
190
128
  requirements: []
191
129
  rubyforge_project:
192
- rubygems_version: 1.8.28
130
+ rubygems_version: 2.0.3
193
131
  signing_key:
194
- specification_version: 3
132
+ specification_version: 4
195
133
  summary: Library to handle user specific parameters from various location.
196
134
  test_files:
197
- - test/lib/nugrant/helper/env/test_exporter.rb
198
- - test/lib/nugrant/helper/test_stack.rb
199
135
  - test/lib/nugrant/test_bag.rb
200
136
  - test/lib/nugrant/test_config.rb
201
137
  - test/lib/nugrant/test_parameters.rb
138
+ - test/resources/.vagrantuser
139
+ - test/resources/README.md
140
+ - test/resources/Vagrantfile.v1.empty
141
+ - test/resources/Vagrantfile.v1.fake
142
+ - test/resources/Vagrantfile.v1.real
143
+ - test/resources/Vagrantfile.v2.empty
144
+ - test/resources/Vagrantfile.v2.fake
145
+ - test/resources/Vagrantfile.v2.real
146
+ - test/resources/json/params_array.json
147
+ - test/resources/json/params_boolean.json
148
+ - test/resources/json/params_combinations.json
149
+ - test/resources/json/params_current_1.json
150
+ - test/resources/json/params_current_2.json
151
+ - test/resources/json/params_defaults_at_root.json
152
+ - test/resources/json/params_defaults_not_at_root.json
153
+ - test/resources/json/params_empty.json
154
+ - test/resources/json/params_list.json
155
+ - test/resources/json/params_simple.json
156
+ - test/resources/json/params_system_1.json
157
+ - test/resources/json/params_system_2.json
158
+ - test/resources/json/params_unix_eol.json
159
+ - test/resources/json/params_user_1.json
160
+ - test/resources/json/params_user_2.json
161
+ - test/resources/json/params_windows_eol.json
162
+ - test/resources/yaml/params_array.yml
163
+ - test/resources/yaml/params_boolean.yml
164
+ - test/resources/yaml/params_combinations.yml
165
+ - test/resources/yaml/params_current_1.yml
166
+ - test/resources/yaml/params_current_2.yml
167
+ - test/resources/yaml/params_defaults_at_root.yml
168
+ - test/resources/yaml/params_defaults_not_at_root.yml
169
+ - test/resources/yaml/params_empty.yml
170
+ - test/resources/yaml/params_list.yml
171
+ - test/resources/yaml/params_simple.yml
172
+ - test/resources/yaml/params_system_1.yml
173
+ - test/resources/yaml/params_system_2.yml
174
+ - test/resources/yaml/params_unix_eol.yml
175
+ - test/resources/yaml/params_user_1.yml
176
+ - test/resources/yaml/params_user_2.yml
177
+ - test/resources/yaml/params_windows_eol.yml
@@ -1,208 +0,0 @@
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