/
command.rb
434 lines (348 loc) · 11 KB
/
command.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
require 'fileutils'
require 'net/ftp'
require 'set'
require 'fig/at_exit'
require 'fig/command/action'
require 'fig/command/options'
require 'fig/command/package_applier'
require 'fig/command/package_loader'
require 'fig/figrc'
require 'fig/logging'
require 'fig/operating_system'
require 'fig/package'
require 'fig/parser'
require 'fig/repository'
require 'fig/repository_error'
require 'fig/runtime_environment'
require 'fig/statement/configuration'
require 'fig/update_lock'
require 'fig/user_input_error'
require 'fig/working_directory_maintainer'
module Fig; end
# Main program
class Fig::Command
def run_fig(argv, options = nil)
begin
@options = options || Fig::Command::Options.new()
@options.process_command_line(argv)
rescue Fig::UserInputError => error
$stderr.puts error.to_s # Logging isn't set up yet.
return Fig::Command::Action::EXIT_FAILURE
end
if not @options.exit_code.nil?
return @options.exit_code
end
actions = @options.actions()
if actions.empty?
$stderr.puts "Nothing to do.\n\n"
$stderr.puts %q<Run "fig --help" for a full list of commands.>
return Fig::Command::Action::EXIT_FAILURE
end
actions.each do
|action|
if action.execute_immediately_after_command_line_parse?
# Note that the action doesn't get an execution context.
return action.execute()
end
end
Fig::Logging.initialize_pre_configuration(@options.log_level())
@descriptor = @options.descriptor
check_descriptor_requirement()
if actions.any? {|action| not action.allow_both_descriptor_and_file? }
ensure_descriptor_and_file_were_not_both_specified()
end
check_package_content_options()
configure()
set_up_base_package()
invoke_post_set_up_actions()
context = ExecutionContext.new(
@base_package,
base_config(),
@environment,
@repository,
@operating_system,
@package_source_description
)
actions.each do
|action|
action.execution_context = context
exit_code = action.execute
if exit_code != Fig::Command::Action::EXIT_SUCCESS
return exit_code
end
end
return Fig::Command::Action::EXIT_SUCCESS
end
def run_with_exception_handling(argv, options = nil)
begin
return run_fig(argv, options)
rescue Fig::URLAccessError => error
urls = error.urls.join(', ')
$stderr.puts \
"Access to #{urls} in #{error.package}/#{error.version} not allowed."
rescue Fig::UserInputError => error
log_error_message(error)
end
return Fig::Command::Action::EXIT_FAILURE
end
# Extension mechanism for customizing Fig.
def add_post_set_up_action(action)
@post_set_up_actions << action
return
end
def add_publish_listener(listener)
@publish_listeners << listener
return
end
def initialize()
@post_set_up_actions = []
@publish_listeners = []
end
private
ExecutionContext =
Struct.new(
:base_package,
:base_config,
:environment,
:repository,
:operating_system,
:package_source_description
)
def check_include_statements_versions?()
return false if @options.suppress_warning_include_statement_missing_version?
suppressed_warnings = @application_configuration['suppress warnings']
return true if not suppressed_warnings
return ! suppressed_warnings.include?('include statement missing version')
end
def configure()
set_up_update_lock()
set_up_application_configuration()
Fig::Logging.initialize_post_configuration(
@options.log_config() || @application_configuration['log configuration'],
@options.log_level()
)
@operating_system = Fig::OperatingSystem.new(@options.login?)
prepare_repository()
prepare_environment()
end
def set_up_update_lock()
return if not @options.update_packages
update_lock_response = @options.update_lock_response
return if update_lock_response == :ignore
@update_lock = Fig::UpdateLock.new(@options.home, update_lock_response)
# *sigh* Ruby 1.8 doesn't support close_on_exec(), so we've got to ensure
# this stuff on our own.
Fig::AtExit.add { @update_lock.close }
return
end
def set_up_application_configuration()
@application_configuration = Fig::FigRC.find(
@options.figrc(),
ENV['FIG_REMOTE_URL'],
@options.login?,
@options.home(),
@options.no_figrc?
)
if \
remote_operation_necessary? \
&& @application_configuration.remote_repository_url.nil?
raise Fig::UserInputError.new(
'Please define the FIG_REMOTE_URL environment variable.'
)
end
return
end
def prepare_repository()
@repository = Fig::Repository.new(
@operating_system,
@options.home(),
@application_configuration,
@publish_listeners,
check_include_statements_versions?
)
case @options.update_packages
when :unconditionally
@repository.update_unconditionally
when :if_missing
@repository.update_if_missing
end
return
end
def prepare_environment()
working_directory_maintainer = Fig::WorkingDirectoryMaintainer.new('.')
Fig::AtExit.add do
working_directory_maintainer.prepare_for_shutdown(
@base_package && retrieves_should_happen?
)
end
environment_variables = nil
if reset_environment?
environment_variables = Fig::OperatingSystem.get_environment_variables({})
end
@environment = Fig::RuntimeEnvironment.new(
@repository, environment_variables, working_directory_maintainer
)
Fig::AtExit.add { @environment.check_unused_retrieves() }
return
end
def set_up_base_package()
return if ! load_base_package?
# We get these before loading the package so that we detect conflicts
# between actions.
retrieves_should_happen = retrieves_should_happen?
register_base_package = register_base_package?
apply_config = apply_config?
apply_base_config = apply_config ? apply_base_config? : nil
package_loader = new_package_loader()
if @options.actions.all? {|action| action.base_package_can_come_from_descriptor?}
@base_package = package_loader.load_package_object()
else
@base_package = package_loader.load_package_object_from_file()
end
@package_source_description = package_loader.package_source_description()
applier = new_package_applier()
if retrieves_should_happen
applier.activate_retrieves()
end
if register_base_package
applier.register_package_with_environment()
end
if apply_config
applier.apply_config_to_environment(! apply_base_config)
end
return
end
def invoke_post_set_up_actions()
@post_set_up_actions.each do
|action|
action.set_up_finished(@application_configuration)
end
return
end
def base_config()
return @options.config() ||
@descriptor && @descriptor.config ||
Fig::Package::DEFAULT_CONFIG
end
def new_package_loader()
return Fig::Command::PackageLoader.new(
@application_configuration,
@descriptor,
@options.package_definition_file,
base_config(),
@repository
)
end
def new_package_applier()
return Fig::Command::PackageApplier.new(
@base_package,
@environment,
@options,
@descriptor,
base_config(),
@package_source_description
)
end
# If the user has specified a descriptor and we are not publishing, than any
# package.fig or --file option is ignored. Thus, in order to avoid confusing
# the user, we make specifying both an error.
def ensure_descriptor_and_file_were_not_both_specified()
file = @options.package_definition_file()
# If the user specified --no-file, even though it's kind of superfluous,
# we'll let it slide because the user doesn't think that any file will be
# processed.
file_specified = ! file.nil? && file != :none
if @descriptor && file_specified
raise Fig::UserInputError.new(
%Q<Cannot specify both a package descriptor (#{@descriptor.original_string}) and the --file option (#{file}).>
)
end
return
end
def check_descriptor_requirement()
@options.actions.each do
|action|
case action.descriptor_requirement()
when :required
if not @descriptor
raise Fig::UserInputError.new(
"Need to specify a descriptor for #{action.primary_option()}."
)
end
when :warn
if @descriptor
Fig::Logging.warn(
%Q<Ignored descriptor "#{@descriptor.to_string}".>
)
end
end
end
return
end
def check_package_content_options()
statements = @options.package_contents_statements
return if statements.empty?
return if @options.actions.any? \
{|action| action.cares_about_package_content_options?}
statements.each do
|statement|
Fig::Logging.warn(
"Ignored #{statement.source_description} for #{statement.url}."
)
end
return
end
def remote_operation_necessary?()
return @options.actions.any? {|action| action.remote_operation_necessary?}
end
def load_base_package?()
return should_perform?(
@options.actions, %Q<the base package should be loaded>
) {|action| action.load_base_package?}
end
def retrieves_should_happen?()
return @options.actions.any? {|action| action.retrieves_should_happen?}
end
def register_base_package?()
return should_perform?(
@options.actions, %Q<the base package should be in the starting set of packages>
) {|action| action.register_base_package?}
end
def apply_config?()
return should_perform?(
@options.actions, %Q<any config should be applied>
) {|action| action.apply_config?}
end
def apply_base_config?()
actions_wanting_application =
@options.actions.select {|action| action.apply_config?}
return should_perform?(
actions_wanting_application, %Q<the base config should be applied>
) {|action| action.apply_base_config?}
end
def should_perform?(actions, failure_description, &predicate)
yes_actions, no_actions = actions.partition &predicate
# Filter out the "don't care" actions".
no_actions = no_actions.select { |action| ! predicate.call(action).nil? }
return false if yes_actions.empty?
return true if no_actions.empty?
action_strings = actions.map {|action| action.options.join}
action_string = action_strings.join %q<", ">
raise Fig::UserInputError.new(
%Q<Cannot use "#{action_string}" together because they disagree on whether #{failure_description}.>
)
end
def reset_environment?()
return @options.actions.any? {|action| action.reset_environment?}
end
def log_error_message(error)
# If there's no message, we assume that the cause has already been logged.
if error_has_message?(error)
Fig::Logging.fatal error.to_s
end
end
def error_has_message?(error)
class_name = error.class.name
return error.message != class_name
end
end