cide 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,307 +0,0 @@
1
- module CIDE
2
- module Build
3
- class ConfigLoader
4
- class Path
5
- attr_reader :to_s
6
- def initialize(str)
7
- @to_s = str.to_s
8
- end
9
-
10
- def append(value)
11
- self.class.new(
12
- @to_s + (value.is_a?(Integer) ? "[#{value}]" : ".#{value}"),
13
- )
14
- end
15
- end
16
-
17
- def initialize(config)
18
- @config = config
19
- end
20
-
21
- def load(data)
22
- data.each_pair do |key, value|
23
- key = key.to_s
24
- path = Path.new(key)
25
- case key
26
- when 'from', 'image' then
27
- wanted_key(path, 'from', key)
28
- @config.from = expect_string(path, value)
29
- when 'as_root' then
30
- @config.as_root = maybe_step(path, value)
31
- when 'use_ssh' then
32
- @config.use_ssh = expect_boolean(path, value)
33
- when 'before' then
34
- @config.before = maybe_step(path, value)
35
- when 'env', 'forward_env' then
36
- wanted_key(path, 'env', key)
37
- @config.env = expect_env_hash(path, value)
38
- when 'export_dir' then
39
- @config.export_dir = maybe_string(path, value)
40
- when 'link', 'links' then
41
- @config.links = expect_links(path, value)
42
- when 'run', 'command' then
43
- wanted_key(path, 'run', key)
44
- @config.run = expect_run(path, value)
45
- else
46
- unknown_key(path)
47
- end
48
- end
49
- @config
50
- end
51
-
52
- protected
53
-
54
- def warn(message)
55
- @config.warnings << message
56
- end
57
-
58
- def error(message)
59
- @config.errors << message
60
- end
61
-
62
- def wanted_key(path, wanted_key, key)
63
- return if key == wanted_key
64
- warn "#{path} is deprecated. use '#{wanted_key}' instead."
65
- end
66
-
67
- def unknown_key(path)
68
- warn "Unknown key #{path}"
69
- end
70
-
71
- def type_error(path, wanted_type, value)
72
- error "expected #{path} to be a #{wanted_type} but got a #{value.class}"
73
- end
74
-
75
- def expect_string(path, value)
76
- case value
77
- when String, Symbol, Integer
78
- value.to_s
79
- else
80
- type_error(path, 'string', value)
81
- ''
82
- end
83
- end
84
-
85
- def maybe_string(path, value)
86
- case value
87
- when String, Symbol, Integer
88
- value.to_s
89
- when nil
90
- nil
91
- else
92
- type_error(path, 'string or nil', value)
93
- nil
94
- end
95
- end
96
-
97
- def maybe_step(path, value)
98
- case value
99
- when String, Symbol, Integer, Array then
100
- load_step(path, run: value)
101
- when Hash then
102
- load_step(path, value)
103
- when nil then
104
- nil
105
- else
106
- type_error(path, 'string, array, hash or nil', value)
107
- nil
108
- end
109
- end
110
-
111
- def load_step(path, data)
112
- step = Config::Step.new
113
- data.each_pair do |key, value|
114
- key = key.to_s
115
- path_ = path.append(key)
116
- case key
117
- when 'run' then
118
- step.run = expect_array(path_, value)
119
- when 'env', 'forward_env' then
120
- wanted_key(path_, 'env', key)
121
- step.env = expect_env_hash(path_, value)
122
- when 'add' then
123
- step.add = expect_adds(path_, value)
124
- else
125
- unknown_key(path_)
126
- end
127
- end
128
- step
129
- end
130
-
131
- def expect_links(path, value)
132
- array = []
133
- case value
134
- when String, Symbol, Integer, Hash then
135
- array << expect_link(path, value)
136
- when Array then
137
- value.compact.each_with_index do |value_, i|
138
- array << expect_link(path.append(i), value_)
139
- end
140
- when nil then
141
- # nothing to do
142
- else
143
- type_error(path, 'string, array of links, hash or nil', value)
144
- end
145
- array.compact
146
- end
147
-
148
- def expect_link(path, value)
149
- case value
150
- when String, Symbol, Integer then
151
- load_link(path, image: value)
152
- when Hash
153
- load_link(path, value)
154
- else
155
- type_error(path, 'string or hash expected', value)
156
- end
157
- end
158
-
159
- def load_link(path, data)
160
- link = Config::Link.new
161
- data.each_pair do |key, value|
162
- key = key.to_s
163
- path_ = path.append(key)
164
- case key
165
- when 'name' then
166
- link.name = expect_string(path_, value)
167
- when 'image', 'from' then
168
- wanted_key(path_, 'image', key)
169
- link.image = expect_string(path_, value)
170
- when 'env' then
171
- link.env = expect_env_hash(path_, value)
172
- when 'run' then
173
- link.run = maybe_string(path_, value)
174
- else
175
- unknown_key(path_)
176
- end
177
- end
178
- link.name ||= link.image.split(':').first.split('/').last if link.image
179
- link.image ||= link.name
180
- if link.name.nil?
181
- type_error(
182
- path,
183
- 'expected hash to either declare the name or image',
184
- data,
185
- )
186
- return nil
187
- end
188
- link
189
- end
190
-
191
- def expect_boolean(path, value)
192
- case value
193
- when true then
194
- true
195
- when false then
196
- false
197
- else
198
- type_error(path, 'boolean', value)
199
- false
200
- end
201
- end
202
-
203
- def expect_array(path, value)
204
- array = []
205
- case value
206
- when Array then
207
- value.compact.each_with_index do |value_, i|
208
- array << expect_string(path.append(i), value_)
209
- end
210
- when String, Symbol, Integer then
211
- array << value.to_s
212
- when nil then
213
- # nothing to do
214
- else
215
- type_error(path, 'array of string, string or nil', value)
216
- end
217
- array.compact
218
- end
219
-
220
- def expect_adds(path, value)
221
- array = []
222
- case value
223
- when Array then
224
- value.compact.each_with_index do |value_, i|
225
- str = expect_string(path.append(i), value_)
226
- array << load_add_str(str)
227
- end
228
- when Hash then
229
- value.each_pair do |key_, value_|
230
- src = expect_array(path.append(key_), value_)
231
- array << Config::FileAdd.new(src: src, dest: key_.to_s)
232
- end
233
- when String, Symbol, Integer then
234
- array << load_add_str(value.to_s)
235
- when nil then
236
- # nothing to do
237
- else
238
- type_error(path, 'arrays of string, hash, string or nil', value)
239
- end
240
- array.compact
241
- end
242
-
243
- def load_add_str(str)
244
- Config::FileAdd.new(
245
- src: [str],
246
- dest: str,
247
- )
248
- end
249
-
250
- def expect_env(path, key)
251
- str = expect_string(path, key)
252
- return nil if str == ''
253
- value = ENV[str]
254
- error "Missing environment variable #{key} in #{path}" if value.nil?
255
- value
256
- end
257
-
258
- def expect_env_hash(path, value)
259
- hash = {}
260
- case value
261
- when String, Symbol, Integer
262
- key1 = value
263
- value1 = expect_env(path, key1)
264
- hash[key1] = value1 if value1
265
- when Array then
266
- value.compact.each_with_index do |key, i|
267
- value_ = expect_env(path.append(i), key)
268
- hash[key.to_s] = value_ if value_
269
- end
270
- when Hash then
271
- value.each_pair do |key, value_|
272
- key = key.to_s
273
- path_ = path.append(key)
274
- if value_.nil?
275
- value_ = expect_env(path_, key)
276
- else
277
- value_ = expect_string(path_, value_)
278
- end
279
- hash[key.to_s] = value_ if value_
280
- end
281
- else
282
- type_error(path, 'hash or array of keys or just a string', value)
283
- end
284
- hash
285
- end
286
-
287
- def expect_run(path, value)
288
- array = []
289
- has_error = false
290
- case value
291
- when Array
292
- value.compact.each_with_index do |key, i|
293
- array << expect_string(path.append(i), key)
294
- end
295
- when String, Symbol, Integer
296
- array.push('sh', '-e', '-c', value.to_s)
297
- when nil then
298
- else
299
- has_error = true
300
- type_error(path, 'string or array of string', value)
301
- end
302
- error("#{path} shouldn't be empty") if array.empty? && !has_error
303
- array
304
- end
305
- end
306
- end
307
- end