Class: Foobara::CommandConnector

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.0.110/projects/command_connectors/src/authenticator.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/request.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/response.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/commands/ping.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/unknown_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/not_found_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/commands/describe.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/not_allowed_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/no_type_found_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/invalid_context_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/unauthenticated_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/commands/list_commands.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/no_command_found_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/command_connector_error.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/commands/query_git_commit_info.rb,
foobara-0.0.110/projects/command_connectors/src/command_connector/no_command_or_type_found_error.rb

Defined Under Namespace

Modules: Commands Classes: Authenticator, 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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 106

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

#authenticatorObject

Returns the value of attribute authenticator.



104
105
106
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 104

def authenticator
  @authenticator
end

#capture_unknown_errorObject

Returns the value of attribute capture_unknown_error.



104
105
106
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 104

def capture_unknown_error
  @capture_unknown_error
end

#command_registryObject

Returns the value of attribute command_registry.



104
105
106
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 104

def command_registry
  @command_registry
end

Class Method Details

.allowed_rules_to_registerObject



10
11
12
13
14
15
16
17
18
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 10

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_registryObject



24
25
26
27
28
29
30
31
32
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 24

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



6
7
8
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 6

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



20
21
22
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 20

def register_allowed_rule(*rule_args)
  allowed_rules_to_register << rule_args
end

.register_authenticator(*authenticatorish_args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 34

def register_authenticator(*authenticatorish_args)
  authenticator = to_authenticator(*authenticatorish_args)

  unless authenticator.symbol
    # :nocov:
    raise ArgumentError, "Expected authenticator to have a symbol"
    # :nocov:
  end

  authenticator_registry[authenticator.symbol] = authenticator
end

.to_authenticator(*args) ⇒ Object

TODO: relocate to Authenticator



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
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 47

def to_authenticator(*args)
  symbol, object = case args.size
                   when 1
                     [nil, args.first]
                   when 2
                     args
                   else
                     # :nocov:
                     raise ArgumentError, "Expected 1 or 2 arguments, got #{args.size}"
                     # :nocov:
                   end

  case object
  when Class
    if object < Authenticator
      object.new
    else
      # :nocov:
      raise ArgumentError, "Expected a class that inherits from Authenticator"
      # :nocov:
    end
  when Authenticator, nil
    object
  when ::String
    if symbol
      # :nocov:
      raise ArgumentError, "Was not expecting a symbol and a string"
      # :nocov:
    end

    to_authenticator(object.to_sym)
  when ::Symbol
    authenticator = authenticator_registry[object]

    unless authenticator
      # :nocov:
      raise "No authenticator found for #{object}"
      # :nocov:
    end

    authenticator
  else
    if object.respond_to?(:call)
      Authenticator.new(symbol:, &object)
    else
      # :nocov:
      raise "Not sure how to convert #{object} into an AllowedRule object"
      # :nocov:
    end
  end.tap do |resolved_authenticator|
    if resolved_authenticator
      resolved_authenticator.symbol ||= symbol
    end
  end
end

Instance Method Details

#all_exposed_commandsObject



598
599
600
601
602
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 598

def all_exposed_commands
  process_delayed_connections

  command_registry.foobara_all_command
end

#authenticate(request) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 400

def authenticate(request)
  request_command = request.command
  auth_block = request_command.authenticator || authenticator

  request_command.after_load_records do |command:, **|
    authenticated_user = request.instance_exec(&auth_block)

    request_command.authenticated_user = authenticated_user

    unless authenticated_user
      request_command.outcome = Outcome.error(CommandConnector::UnauthenticatedError.new)

      command.state_machine.error!
      command.halt!
    end
  end
end

#build_command(request) ⇒ Object



418
419
420
421
422
423
424
425
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 418

def build_command(request)
  unless request.error
    command = request_to_command(request)
    request.command = command
  end

  command
end

#build_requestObject



356
357
358
359
360
361
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 356

def build_request(...)
  self.class::Request.new(...).tap do |request|
    # TODO: feels like a smell
    request.command_connector = self
  end
end

#build_response(request) ⇒ Object



427
428
429
430
431
432
433
434
435
436
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 427

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(registerable, authenticator: nil) ⇒ Object



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
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 319

def connect(registerable, *, authenticator: nil, **)
  if authenticator
    authenticator = self.class.to_authenticator(authenticator)
  end

  case registerable
  when Class
    unless registerable < Command
      # :nocov:
      raise "Don't know how to register #{registerable} (#{registerable.class})"
      # :nocov:
    end

    command_registry.register(registerable, *, authenticator:, **)
  when Module
    if registerable.foobara_organization?
      registerable.foobara_domains.map do |domain|
        connect(domain, *, authenticator:, **)
      end.flatten
    elsif registerable.foobara_domain?
      registerable.foobara_all_command(mode: Namespace::LookupMode::DIRECT).map do |command_class|
        Util.array(connect(command_class, *, authenticator:, **))
      end.flatten
    else
      # :nocov:
      raise "Don't know how to register #{registerable} (#{registerable.class})"
      # :nocov:
    end
  when Symbol, String
    connect_delayed(registerable, *, authenticator:, **)
  else
    # :nocov:
    raise "Don't know how to register #{registerable} (#{registerable.class})"
    # :nocov:
  end
end

#connect_delayed(registerable_name, *args, **opts) ⇒ Object



299
300
301
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 299

def connect_delayed(registerable_name, *args, **opts)
  delayed_connections[registerable_name] = { args:, opts: }
end

#delayed_connectionsObject



303
304
305
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 303

def delayed_connections
  @delayed_connections ||= {}
end

#find_builtin_command_class(command_class_name) ⇒ Object



122
123
124
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 122

def find_builtin_command_class(command_class_name)
  self.class.find_builtin_command_class(command_class_name)
end

#foobara_manifestObject



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 442

def foobara_manifest
  process_delayed_connections

  to_include = Set.new

  to_include << command_registry.global_organization
  to_include << command_registry.global_domain

  # ABSOLUTE lets us get all of the children but not include dependent domains (GlobalDomain)
  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?
              # :nocov:
              raise UnexpectedSensitiveTypeInManifestError,
                    "Unexpected sensitive type in manifest: #{o.scoped_full_path}. " \
                    "Make sure these are not included."
            # :nocov:
            else
              domain_name = o.foobara_domain.scoped_full_name

              unless command_registry.foobara_registered?(domain_name)
                command_registry.build_and_register_exposed_domain(domain_name)

                # Since we don't know which other domains/orgs creating this domain might have created,
                # we will just add them all to be included just in case
                command_registry.foobara_all_domain.each do |exposed_domain|
                  additional_to_include << exposed_domain
                end

                command_registry.foobara_all_organization.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
        # :nocov:
        raise "no category symbol for #{object}"
        # :nocov:
      end

      namespace = if object.is_a?(Types::Type)
                    object.created_in_namespace
                  else
                    Foobara::Namespace.current
                  end

      cat = h[category_symbol] ||= {}
      # TODO: do we really need to enter the namespace here for this?
      cat[manifest_reference] = Foobara::Namespace.use namespace do
        object.foobara_manifest
      end

      included << object
    end
  end

  h[:domain].each_value do |domain_manifest|
    # TODO: hack, we need to trim types down to what is actually included in this 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



140
141
142
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 140

def lookup_command(name)
  command_registry.foobara_lookup_command(name)
end

#mutate_response(response) ⇒ Object



283
284
285
286
287
288
289
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 283

def mutate_response(response)
  command = response.command

  if command.respond_to?(:mutate_response)
    command.mutate_response(response)
  end
end

#normalize_manifest(manifest_hash) ⇒ Object



560
561
562
563
564
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 560

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



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
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 566

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_connectionsObject



307
308
309
310
311
312
313
314
315
316
317
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 307

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(request) ⇒ Object

TODO: maybe instead connect commands with a shortcut_only: “describe” option in order to make this easier to extend and manage.



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
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 146

def request_to_command(request)
  action = request.action
  inputs = nil
  full_command_name = request.full_command_name

  case action
  when "run"
    transformed_command_class = transformed_command_from_name(full_command_name)

    unless transformed_command_class
      # :nocov:
      raise NoCommandFoundError.new(message: "Could not find command registered for #{full_command_name}")
      # :nocov:
    end

    request.command_class = transformed_command_class

    inputs = request.inputs
  when "describe"
    manifestable = transformed_command_from_name(full_command_name) || type_from_name(full_command_name)

    unless manifestable
      # :nocov:
      raise NoCommandOrTypeFoundError.new(
        message: "Could not find command or type registered for #{full_command_name}"
      )
      # :nocov:
    end

    command_class = find_builtin_command_class("Describe")
    full_command_name = command_class.full_command_name

    inputs = request.inputs.merge(manifestable:, request:)

    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "describe_command"
    transformed_command_class = transformed_command_from_name(full_command_name)

    unless transformed_command_class
      # :nocov:
      raise NoCommandFoundError.new(message: "Could not find command registered for #{full_command_name}")
      # :nocov:
    end

    command_class = find_builtin_command_class("Describe")
    full_command_name = command_class.full_command_name

    inputs = request.inputs.merge(manifestable: transformed_command_class, request:)
    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "describe_type"
    type = type_from_name(full_command_name)

    unless type
      # :nocov:
      raise NoTypeFoundError.new(message: "Could not find type registered for #{full_command_name}")
      # :nocov:
    end

    command_class = find_builtin_command_class("Describe")
    full_command_name = command_class.full_command_name

    inputs = request.inputs.merge(manifestable: type, request:)
    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "manifest"
    command_class = find_builtin_command_class("Describe")
    full_command_name = command_class.full_command_name

    inputs = request.inputs.merge(manifestable: self, request:)
    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "ping"
    command_class = find_builtin_command_class("Ping")
    full_command_name = command_class.full_command_name

    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "query_git_commit_info"
    # TODO: this feels out of control... should just accomplish this through run I think instead. Same with ping.
    command_class = find_builtin_command_class("QueryGitCommitInfo")
    full_command_name = command_class.full_command_name

    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "help"
    command_class = find_builtin_command_class("Help")
    full_command_name = command_class.full_command_name

    inputs = { request: }
    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  when "list"
    command_class = find_builtin_command_class("ListCommands")

    full_command_name = command_class.full_command_name

    request.command_class = command_class
    inputs = request.inputs.merge(request:)

    transformed_command_class = transformed_command_from_name(full_command_name) ||
                                transform_command_class(command_class)
  else
    # :nocov:
    raise InvalidContextError.new(message: "Not sure what to do with #{action}")
    # :nocov:
  end

  if inputs && !inputs.empty?
    transformed_command_class.new(inputs)
  else
    transformed_command_class.new
  end
end

#request_to_response(request) ⇒ Object



268
269
270
271
272
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 268

def request_to_response(request)
  response = self.class::Response.new(request:)
  request.response = response
  response
end

#runObject

TODO: maybe introduce a Runner interface?



364
365
366
367
368
369
370
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 364

def run(...)
  process_delayed_connections

  request = build_request(...)

  run_request(request)
end

#run_command(request) ⇒ Object



390
391
392
393
394
395
396
397
398
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 390

def run_command(request)
  request.command.run
rescue => e
  if capture_unknown_error
    request.error = CommandConnector::UnknownError.for(e)
  else
    raise
  end
end

#run_request(request) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 372

def run_request(request)
  command = build_command(request)

  if command.respond_to?(:requires_authentication?) && command.requires_authentication?
    authenticate(request)
  end

  if command
    run_command(request)
    # :nocov:
  elsif !request.error
    raise "No command returned from #request_to_command"
    # :nocov:
  end

  build_response(request)
end

#serialize_response_body(response) ⇒ Object



291
292
293
294
295
296
297
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 291

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



278
279
280
281
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 278

def set_response_body(response)
  outcome = response.request.outcome
  response.body = outcome.success? ? outcome.result : outcome.error_collection
end

#set_response_status(response) ⇒ Object



274
275
276
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 274

def set_response_status(response)
  response.status = response.success? ? 0 : 1
end

#transform_command_class(klass) ⇒ Object

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



264
265
266
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 264

def transform_command_class(klass)
  command_registry.create_exposed_command_without_domain(klass).transformed_command_class
end

#type_from_name(name) ⇒ Object



438
439
440
# File 'foobara-0.0.110/projects/command_connectors/src/command_connector.rb', line 438

def type_from_name(name)
  Foobara.foobara_lookup_type(name, mode: Namespace::LookupMode::RELAXED)
end