Class: Foobara::CommandConnector
- Inherits:
-
Object
- Object
- Foobara::CommandConnector
show all
- Includes:
- Concerns::Desugarizers
- Defined in:
- foobara-0.0.130/projects/command_connectors/src/authenticator.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector.rb,
foobara-0.0.130/projects/command_connectors/src/authenticator_selector.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/request.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/response.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/commands/ping.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/unknown_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/not_found_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/commands/describe.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/not_allowed_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/no_type_found_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/concerns/desugarizers.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/invalid_context_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/unauthenticated_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/commands/list_commands.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/no_command_found_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/command_connector_error.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/commands/query_git_commit_info.rb,
foobara-0.0.130/projects/command_connectors/src/command_connector/no_command_or_type_found_error.rb
Defined Under Namespace
Modules: Commands, Concerns
Classes: Authenticator, AuthenticatorSelector, CommandConnectorError, InvalidContextError, NoCommandFoundError, NoCommandOrTypeFoundError, NoTypeFoundError, NotAllowedError, NotFoundError, Request, Response, UnauthenticatedError, UnexpectedSensitiveTypeInManifestError, UnknownError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(authenticator: nil, capture_unknown_error: nil, default_serializers: nil) ⇒ CommandConnector
Returns a new instance of CommandConnector.
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 121
def initialize(authenticator: nil, capture_unknown_error: nil, default_serializers: nil)
authenticator = self.class.to_authenticator(authenticator)
self.authenticator = authenticator
self.command_registry = CommandRegistry.new(authenticator:)
self.capture_unknown_error = capture_unknown_error
Util.array(default_serializers).each do |serializer|
add_default_serializer(serializer)
end
self.class.allowed_rules_to_register.each do |ruleish_args|
command_registry.allowed_rule(*ruleish_args)
end
end
|
Instance Attribute Details
#authenticator ⇒ Object
Returns the value of attribute authenticator.
119
120
121
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 119
def authenticator
@authenticator
end
|
#capture_unknown_error ⇒ Object
Returns the value of attribute capture_unknown_error.
119
120
121
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 119
def capture_unknown_error
@capture_unknown_error
end
|
#command_registry ⇒ Object
Returns the value of attribute command_registry.
119
120
121
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 119
def command_registry
@command_registry
end
|
Class Method Details
.allowed_rules_to_register ⇒ Object
12
13
14
15
16
17
18
19
20
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 12
def allowed_rules_to_register
return @allowed_rules_to_register if defined?(@allowed_rules_to_register)
@allowed_rules_to_register = if superclass == Object
[]
else
superclass.allowed_rules_to_register.dup
end
end
|
.authenticator_registry ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 26
def authenticator_registry
return @authenticator_registry if defined?(@authenticator_registry)
@authenticator_registry = if superclass == Object
{}
else
superclass.authenticator_registry.dup
end
end
|
.find_builtin_command_class(command_class_name) ⇒ Object
8
9
10
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 8
def find_builtin_command_class(command_class_name)
Util.find_constant_through_class_hierarchy(self, "Commands::#{command_class_name}")
end
|
.register_allowed_rule(*rule_args) ⇒ Object
22
23
24
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 22
def register_allowed_rule(*rule_args)
allowed_rules_to_register << rule_args
end
|
.register_authenticator(*authenticatorish_args) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 36
def register_authenticator(*authenticatorish_args)
authenticator = to_authenticator(*authenticatorish_args)
unless authenticator.symbol
raise ArgumentError, "Expected authenticator to have a symbol"
end
authenticator_registry[authenticator.symbol] = authenticator
end
|
.to_authenticator(*args) ⇒ Object
TODO: relocate to Authenticator
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
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 49
def to_authenticator(*args)
symbol, object = case args.size
when 1
[nil, args.first]
when 2
args
else
raise ArgumentError, "Expected 1 or 2 arguments, got #{args.size}"
end
case object
when Class
if object < Authenticator
object.new
else
raise ArgumentError, "Expected a class that inherits from Authenticator"
end
when Authenticator, nil
object
when ::String
if symbol
raise ArgumentError, "Was not expecting a symbol and a string"
end
to_authenticator(object.to_sym)
when ::Symbol
authenticator = authenticator_registry[object]
unless authenticator
raise "No authenticator found for #{object}"
end
authenticator
when ::Array
case object.size
when 0
nil
when 1
to_authenticator(object.first)
else
authenticators = object.map { |authenticatorish| to_authenticator(authenticatorish) }
AuthenticatorSelector.new(authenticators:, symbol:)
end
else
if object.respond_to?(:call)
Authenticator.new(symbol:, &object)
else
raise "Not sure how to convert #{object} into an AllowedRule object"
end
end.tap do |resolved_authenticator|
if resolved_authenticator
resolved_authenticator.symbol ||= symbol
end
end
end
|
Instance Method Details
#all_exposed_commands ⇒ Object
642
643
644
645
646
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 642
def all_exposed_commands
process_delayed_connections
command_registry.foobara_all_command(mode: Namespace::LookupMode::ABSOLUTE)
end
|
#all_exposed_type_names ⇒ Object
648
649
650
651
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 648
def all_exposed_type_names
foobara_manifest[:type].keys.sort.map(&:to_s)
end
|
#build_command_instance(request) ⇒ Object
442
443
444
445
446
447
448
449
450
451
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 442
def build_command_instance(request)
command = request_to_command_instance(request)
request.command = command
if command.is_a?(TransformedCommand)
command.request = request
end
command
end
|
#build_request ⇒ Object
372
373
374
375
376
377
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 372
def build_request(...)
self.class::Request.new(...).tap do |request|
request.command_connector = self
end
end
|
#build_response(request) ⇒ Object
460
461
462
463
464
465
466
467
468
469
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 460
def build_response(request)
response = request_to_response(request)
set_response_status(response)
set_response_body(response)
mutate_response(response)
serialize_response_body(response)
response
end
|
#connect(*args, **opts) ⇒ Object
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
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 318
def connect(*args, **opts)
args, opts = desugarize_connect_args(args, opts)
registerable = args.first
if opts.key?(:authenticator)
authenticator = opts[:authenticator]
authenticator = self.class.to_authenticator(authenticator)
opts = opts.merge(authenticator:)
end
case registerable
when Class
unless registerable < Command
raise "Don't know how to register #{registerable} (#{registerable.class})"
end
command_registry.register(*args, **opts)
when Module
if registerable.foobara_organization?
registerable.foobara_domains.map do |domain|
connect(domain, *args[1..], **opts)
end.flatten
elsif registerable.foobara_domain?
registerable.foobara_all_command(mode: Namespace::LookupMode::DIRECT).map do |command_class|
Util.array(connect(command_class, *args[1..], **opts))
end.flatten
else
raise "Don't know how to register #{registerable} (#{registerable.class})"
end
when Symbol, String
connect_delayed(*args, **opts)
else
raise "Don't know how to register #{registerable} (#{registerable.class})"
end
end
|
#connect_delayed(registerable_name, *args, **opts) ⇒ Object
298
299
300
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 298
def connect_delayed(registerable_name, *args, **opts)
delayed_connections[registerable_name] = { args:, opts: }
end
|
#delayed_connections ⇒ Object
302
303
304
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 302
def delayed_connections
@delayed_connections ||= {}
end
|
#desugarize_connect_args(args, opts) ⇒ Object
361
362
363
364
365
366
367
368
369
370
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 361
def desugarize_connect_args(args, opts)
if self.class.desugarizer
self.class.desugarizer.process_value!([args, opts])
else
[args, opts]
end
end
|
#determine_command_class(request) ⇒ Object
453
454
455
456
457
458
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 453
def determine_command_class(request)
unless request.error
command_class = request_to_command_class(request)
request.command_class = command_class
end
end
|
#find_builtin_command_class(command_class_name) ⇒ Object
137
138
139
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 137
def find_builtin_command_class(command_class_name)
self.class.find_builtin_command_class(command_class_name)
end
|
#foobara_manifest ⇒ Object
475
476
477
478
479
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 475
def foobara_manifest
Namespace.use command_registry do
foobara_manifest_in_current_namespace
end
end
|
#foobara_manifest_in_current_namespace ⇒ Object
TODO: try to break this giant method up
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 482
def foobara_manifest_in_current_namespace
process_delayed_connections
to_include = Set.new
to_include << command_registry.global_organization
to_include << command_registry.global_domain
command_registry.foobara_each(mode: Namespace::LookupMode::ABSOLUTE) do |exposed_whatever|
to_include << exposed_whatever
end
included = Set.new
additional_to_include = Set.new
h = {
organization: {},
domain: {},
type: {},
command: {},
error: {},
processor: {},
processor_class: {}
}
TypeDeclarations.with_manifest_context(to_include: additional_to_include, remove_sensitive: true) do
until to_include.empty? && additional_to_include.empty?
object = nil
if to_include.empty?
until additional_to_include.empty?
o = additional_to_include.first
additional_to_include.delete(o)
if o.is_a?(::Module)
if o.foobara_domain? || o.foobara_organization?
unless o.foobara_root_namespace == command_registry
next
end
elsif o.is_a?(::Class) && o < Foobara::Command
next
end
elsif o.is_a?(Types::Type)
if o.sensitive?
raise UnexpectedSensitiveTypeInManifestError,
"Unexpected sensitive type in manifest: #{o.scoped_full_path}. " \
"Make sure these are not included."
else
domain_name = o.foobara_domain.scoped_full_name
unless command_registry.foobara_registered?(domain_name, mode: Namespace::LookupMode::ABSOLUTE)
command_registry.build_and_register_exposed_domain(domain_name)
command_registry.foobara_all_domain(
mode: Namespace::LookupMode::ABSOLUTE
).each do |exposed_domain|
additional_to_include << exposed_domain
end
command_registry.foobara_all_organization(
mode: Namespace::LookupMode::ABSOLUTE
).each do |exposed_organization|
additional_to_include << exposed_organization
end
end
end
end
object = o
break
end
else
object = to_include.first
to_include.delete(object)
end
break unless object
next if included.include?(object)
manifest_reference = object.foobara_manifest_reference.to_sym
category_symbol = command_registry.foobara_category_symbol_for(object)
unless category_symbol
raise "no category symbol for #{object}"
end
namespace = if object.is_a?(Types::Type)
object.created_in_namespace
else
Foobara::Namespace.current
end
cat = h[category_symbol] ||= {}
cat[manifest_reference] = Foobara::Namespace.use namespace do
object.foobara_manifest
end
included << object
end
end
h[:domain].each_value do |domain_manifest|
domain_manifest[:types] = domain_manifest[:types].select do |type_name|
h[:type].key?(type_name.to_sym)
end
end
h = normalize_manifest(h)
patch_up_broken_parents_for_errors_with_missing_command_parents(h)
end
|
#lookup_command(name) ⇒ Object
155
156
157
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 155
def lookup_command(name)
command_registry.foobara_lookup_command(name)
end
|
#mutate_response(response) ⇒ Object
281
282
283
284
285
286
287
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 281
def mutate_response(response)
command = response.command
if command.respond_to?(:mutate_response)
command.mutate_response(response)
end
end
|
#normalize_manifest(manifest_hash) ⇒ Object
604
605
606
607
608
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 604
def normalize_manifest(manifest_hash)
manifest_hash.map do |key, entries|
[key, entries.sort.to_h]
end.sort.to_h
end
|
#patch_up_broken_parents_for_errors_with_missing_command_parents(manifest_hash) ⇒ Object
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 610
def patch_up_broken_parents_for_errors_with_missing_command_parents(manifest_hash)
root_manifest = Manifest::RootManifest.new(manifest_hash)
error_category = {}
root_manifest.errors.each do |error|
error_manifest = if error.parent_category == :command &&
!root_manifest.contains?(error.parent_name, error.parent_category)
domain = error.domain
index = domain.scoped_full_path.size
fixed_scoped_path = error.scoped_full_path[index..]
fixed_scoped_name = fixed_scoped_path.join("::")
fixed_scoped_prefix = fixed_scoped_path[..-2]
fixed_parent = [:domain, domain.full_domain_name]
error.relevant_manifest.merge(
parent: fixed_parent,
scoped_path: fixed_scoped_path,
scoped_name: fixed_scoped_name,
scoped_prefix: fixed_scoped_prefix
)
else
error.relevant_manifest
end
error_category[error.scoped_full_name.to_sym] = error_manifest
end
manifest_hash.merge(error: error_category)
end
|
#process_delayed_connections ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 306
def process_delayed_connections
delayed_connections.each_pair do |registerable_name, arg_hash|
args = arg_hash[:args]
opts = arg_hash[:opts]
const = Object.const_get(registerable_name)
connect(const, *args, **opts)
end
delayed_connections.clear
end
|
#request_to_command_class(request) ⇒ Object
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
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 159
def request_to_command_class(request)
action = request.action
full_command_name = request.full_command_name
if action == "run"
transformed_command_class = transformed_command_from_name(full_command_name)
unless transformed_command_class
raise NoCommandFoundError.new(message: "Could not find command registered for #{full_command_name}")
end
transformed_command_class
else
action = case action
when "describe_type", "manifest", "describe_command"
"describe"
when "describe", "ping", "query_git_commit_info", "help"
action
when "list"
"list_commands"
else
raise InvalidContextError.new(message: "Not sure what to do with #{action}")
end
command_name = Util.classify(action)
command_class = find_builtin_command_class(command_name)
full_command_name = command_class.full_command_name
transformed_command_from_name(full_command_name) || transform_command_class(command_class)
end
end
|
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
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 195
def request_to_command_inputs(request)
action = request.action
full_command_name = request.full_command_name
case action
when "run"
request.inputs
when "describe"
manifestable = transformed_command_from_name(full_command_name) || type_from_name(full_command_name)
unless manifestable
raise NoCommandOrTypeFoundError.new(
message: "Could not find command or type registered for #{full_command_name}"
)
end
request.inputs.merge(manifestable:, request:)
when "describe_command"
transformed_command_class = transformed_command_from_name(full_command_name)
unless transformed_command_class
raise NoCommandFoundError.new(message: "Could not find command registered for #{full_command_name}")
end
request.inputs.merge(manifestable: transformed_command_class, request:)
when "describe_type"
type = type_from_name(full_command_name)
unless type
raise NoTypeFoundError.new(message: "Could not find type registered for #{full_command_name}")
end
request.inputs.merge(manifestable: type, request:)
when "manifest"
request.inputs.merge(manifestable: self, request:)
when "ping", "query_git_commit_info"
nil
when "help"
{ request: }
when "list"
request.inputs.merge(request:)
else
raise InvalidContextError.new(message: "Not sure what to do with #{action}")
end
end
|
#request_to_command_instance(request) ⇒ Object
249
250
251
252
253
254
255
256
257
258
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 249
def request_to_command_instance(request)
command_class = request.command_class
inputs = request.inputs
if inputs && !inputs.empty?
command_class.new(inputs)
else
command_class.new
end
end
|
#request_to_response(request) ⇒ Object
266
267
268
269
270
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 266
def request_to_response(request)
response = self.class::Response.new(request:)
request.response = response
response
end
|
#run ⇒ Object
TODO: maybe introduce a Runner interface?
380
381
382
383
384
385
386
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 380
def run(...)
process_delayed_connections
request = build_request(...)
run_request(request)
end
|
#run_command(request) ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 428
def run_command(request)
command = request.command
unless command.outcome
command.run
end
rescue => e
if capture_unknown_error
request.error = CommandConnector::UnknownError.for(e)
else
raise
end
end
|
#run_request(request) ⇒ Object
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
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 388
def run_request(request)
command_class = determine_command_class(request)
request.command_class = command_class
return build_response(request) unless command_class
begin
request.open_transaction
request.use_transaction do
request.authenticate
request.mutate_request
inputs = request_to_command_inputs(request)
request.inputs = inputs
command = build_command_instance(request)
request.command = command
unless request.error
if command
run_command(request)
else
raise "No command returned from #request_to_command"
end
end
end
ensure
request.use_transaction do
if (request.response || request).outcome&.success?
request.commit_transaction_if_open
else
request.rollback_transaction
end
end
end
build_response(request)
end
|
#serialize_response_body(response) ⇒ Object
289
290
291
292
293
294
295
296
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 289
def serialize_response_body(response)
command = response.command
if command.respond_to?(:serialize_result)
response.body = command.serialize_result(response.body)
end
end
|
#set_response_body(response) ⇒ Object
276
277
278
279
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 276
def set_response_body(response)
outcome = response.request.outcome
response.body = outcome.success? ? outcome.result : outcome.error_collection
end
|
#set_response_status(response) ⇒ Object
272
273
274
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 272
def set_response_status(response)
response.status = response.success? ? 0 : 1
end
|
Feels like we should just register these if we’re going to make use of them via “actions”… TODO: figure out how to kill this
262
263
264
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 262
def transform_command_class(klass)
command_registry.create_exposed_command_without_domain(klass).transformed_command_class
end
|
#type_from_name(name) ⇒ Object
471
472
473
|
# File 'foobara-0.0.130/projects/command_connectors/src/command_connector.rb', line 471
def type_from_name(name)
Foobara.foobara_lookup_type(name, mode: Namespace::LookupMode::RELAXED)
end
|