Class: Foobara::CommandConnector
- Inherits:
-
Object
- Object
- Foobara::CommandConnector
show all
- Includes:
- Concerns::Desugarizers
- Defined in:
- foobara-0.2.6/projects/command_connectors/src/authenticator.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector.rb,
foobara-0.2.6/projects/command_connectors/src/authenticator_selector.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/request.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/response.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/commands/ping.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/unknown_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/not_found_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/commands/describe.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/not_allowed_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/no_type_found_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/concerns/desugarizers.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/invalid_context_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/unauthenticated_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/commands/list_commands.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/no_command_found_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/command_connector_error.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/commands/query_git_commit_info.rb,
foobara-0.2.6/projects/command_connectors/src/command_connector/no_command_or_type_found_error.rb
Defined Under Namespace
Modules: Commands, Concerns
Classes: AlreadyConnectedError, 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(name: nil, authenticator: nil, capture_unknown_error: nil, default_serializers: nil, default_pre_commit_transformers: nil) ⇒ CommandConnector
Returns a new instance of CommandConnector.
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'projects/command_connectors/src/command_connector.rb', line 122
def initialize(name: nil,
authenticator: nil,
capture_unknown_error: nil,
default_serializers: nil,
default_pre_commit_transformers: nil)
authenticator = self.class.to_authenticator(authenticator)
self.authenticator = authenticator
self.command_registry = CommandRegistry.new(authenticator:)
self.capture_unknown_error = capture_unknown_error
self.name = name
Util.array(default_serializers).each do |serializer|
add_default_serializer(serializer)
end
Util.array(default_pre_commit_transformers).each do |pre_commit_transformer|
add_default_pre_commit_transformer(pre_commit_transformer)
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.
120
121
122
|
# File 'projects/command_connectors/src/command_connector.rb', line 120
def authenticator
@authenticator
end
|
#capture_unknown_error ⇒ Object
Returns the value of attribute capture_unknown_error.
120
121
122
|
# File 'projects/command_connectors/src/command_connector.rb', line 120
def capture_unknown_error
@capture_unknown_error
end
|
#command_registry ⇒ Object
Returns the value of attribute command_registry.
120
121
122
|
# File 'projects/command_connectors/src/command_connector.rb', line 120
def command_registry
@command_registry
end
|
#name ⇒ Object
Returns the value of attribute name.
120
121
122
|
# File 'projects/command_connectors/src/command_connector.rb', line 120
def name
@name
end
|
Class Method Details
.allowed_rules_to_register ⇒ Object
13
14
15
16
17
18
19
20
21
|
# File 'projects/command_connectors/src/command_connector.rb', line 13
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
27
28
29
30
31
32
33
34
35
|
# File 'projects/command_connectors/src/command_connector.rb', line 27
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
9
10
11
|
# File 'projects/command_connectors/src/command_connector.rb', line 9
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
23
24
25
|
# File 'projects/command_connectors/src/command_connector.rb', line 23
def register_allowed_rule(*rule_args)
allowed_rules_to_register << rule_args
end
|
.register_authenticator(*authenticatorish_args) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'projects/command_connectors/src/command_connector.rb', line 37
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
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
|
# File 'projects/command_connectors/src/command_connector.rb', line 50
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_command_names ⇒ Object
679
680
681
|
# File 'projects/command_connectors/src/command_connector.rb', line 679
def all_exposed_command_names
all_exposed_commands.map(&:full_command_name)
end
|
#all_exposed_commands ⇒ Object
673
674
675
676
677
|
# File 'projects/command_connectors/src/command_connector.rb', line 673
def all_exposed_commands
process_delayed_connections
command_registry.foobara_all_command(mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE)
end
|
#all_exposed_type_names ⇒ Object
683
684
685
686
|
# File 'projects/command_connectors/src/command_connector.rb', line 683
def all_exposed_type_names
foobara_manifest[:type].keys.sort.map(&:to_s)
end
|
#build_command_instance(request) ⇒ Object
472
473
474
475
476
477
478
479
480
481
|
# File 'projects/command_connectors/src/command_connector.rb', line 472
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
402
403
404
405
406
407
|
# File 'projects/command_connectors/src/command_connector.rb', line 402
def build_request(...)
self.class::Request.new(...).tap do |request|
request.command_connector = self
end
end
|
#build_response(request) ⇒ Object
490
491
492
493
494
495
496
497
498
499
|
# File 'projects/command_connectors/src/command_connector.rb', line 490
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
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
|
# File 'projects/command_connectors/src/command_connector.rb', line 338
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?
args = args[1..]
registerable.foobara_domains.map do |domain|
connect(domain, *args, **opts)
end.flatten
elsif registerable.foobara_domain?
args = args[1..]
connected = []
registerable = registerable.foobara_all_command(mode: Namespace::LookupMode::DIRECT)
registerable.each do |command_class|
unless command_class.abstract?
connected << connect(command_class, *args, **opts)
end
end
connected.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
308
309
310
311
312
313
314
315
316
317
318
|
# File 'projects/command_connectors/src/command_connector.rb', line 308
def connect_delayed(registerable_name, *args, **opts)
key = registerable_name.to_s
if delayed_connections.key?(key)
raise AlreadyConnectedError, "Already connected #{key}"
else
delayed_connections[key] = { args:, opts: }
end
end
|
#delayed_connections ⇒ Object
320
321
322
|
# File 'projects/command_connectors/src/command_connector.rb', line 320
def delayed_connections
@delayed_connections ||= {}
end
|
#desugarize_connect_args(args, opts) ⇒ Object
391
392
393
394
395
396
397
398
399
400
|
# File 'projects/command_connectors/src/command_connector.rb', line 391
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
483
484
485
486
487
488
|
# File 'projects/command_connectors/src/command_connector.rb', line 483
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
147
148
149
|
# File 'projects/command_connectors/src/command_connector.rb', line 147
def find_builtin_command_class(command_class_name)
self.class.find_builtin_command_class(command_class_name)
end
|
#foobara_manifest ⇒ Object
505
506
507
508
509
|
# File 'projects/command_connectors/src/command_connector.rb', line 505
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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
# File 'projects/command_connectors/src/command_connector.rb', line 512
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_command(mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE) do |exposed_command|
to_include << exposed_command
end
included = Set.new
additional_to_include = Set.new
h = {
organization: {},
domain: {},
type: {},
command: {},
error: {}
}
if TypeDeclarations.include_processors?
h.merge!(
processor: {},
processor_class: {}
)
end
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
mode = Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE
domain_name = o.foobara_domain.scoped_full_name
exposed_domain = command_registry.foobara_lookup_domain(domain_name, mode:)
exposed_domain ||= command_registry.build_and_register_exposed_domain(domain_name)
command_registry.foobara_all_domain(mode:).each do |exposed_domain|
additional_to_include << exposed_domain
end
command_registry.foobara_all_organization(mode:).each do |exposed_organization|
additional_to_include << exposed_organization
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
h[category_symbol][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
165
166
167
|
# File 'projects/command_connectors/src/command_connector.rb', line 165
def lookup_command(name)
command_registry.foobara_lookup_command(name)
end
|
#mutate_response(response) ⇒ Object
291
292
293
294
295
296
297
|
# File 'projects/command_connectors/src/command_connector.rb', line 291
def mutate_response(response)
command = response.command
if command.respond_to?(:mutate_response)
command.mutate_response(response)
end
end
|
#normalize_manifest(manifest_hash) ⇒ Object
635
636
637
638
639
|
# File 'projects/command_connectors/src/command_connector.rb', line 635
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
# File 'projects/command_connectors/src/command_connector.rb', line 641
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.reference]
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
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'projects/command_connectors/src/command_connector.rb', line 324
def process_delayed_connections
return if delayed_connections.empty?
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
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
|
# File 'projects/command_connectors/src/command_connector.rb', line 169
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
|
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
|
# File 'projects/command_connectors/src/command_connector.rb', line 205
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
259
260
261
262
263
264
265
266
267
268
|
# File 'projects/command_connectors/src/command_connector.rb', line 259
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
276
277
278
279
280
|
# File 'projects/command_connectors/src/command_connector.rb', line 276
def request_to_response(request)
response = self.class::Response.new(request:)
request.response = response
response
end
|
#run ⇒ Object
TODO: maybe introduce a Runner interface?
410
411
412
413
414
415
416
|
# File 'projects/command_connectors/src/command_connector.rb', line 410
def run(...)
process_delayed_connections
request = build_request(...)
run_request(request)
end
|
#run_command(request) ⇒ Object
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'projects/command_connectors/src/command_connector.rb', line 458
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
# File 'projects/command_connectors/src/command_connector.rb', line 418
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
299
300
301
302
303
304
305
306
|
# File 'projects/command_connectors/src/command_connector.rb', line 299
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
286
287
288
289
|
# File 'projects/command_connectors/src/command_connector.rb', line 286
def set_response_body(response)
outcome = response.request.outcome
response.body = outcome.success? ? outcome.result : outcome.error_collection
end
|
#set_response_status(response) ⇒ Object
282
283
284
|
# File 'projects/command_connectors/src/command_connector.rb', line 282
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
272
273
274
|
# File 'projects/command_connectors/src/command_connector.rb', line 272
def transform_command_class(klass)
command_registry.create_exposed_command_without_domain(klass).transformed_command_class
end
|
#type_from_name(name) ⇒ Object
501
502
503
|
# File 'projects/command_connectors/src/command_connector.rb', line 501
def type_from_name(name)
Foobara.foobara_lookup_type(name, mode: Namespace::LookupMode::RELAXED)
end
|