aspera-cli 4.29.1 → 4.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +4 -4
- data/CHANGELOG.md +119 -0
- data/CONTRIBUTING.md +8 -1
- data/README.md +2 -0
- data/TODO.md +4 -5
- data/bin/asession +2 -2
- data/docs/README.md +616 -86
- data/docs/test-mcp-with-ai.md +4 -0
- data/lib/aspera/agent/base.rb +62 -6
- data/lib/aspera/agent/factory.rb +127 -20
- data/lib/aspera/ascp/data_repository.rb +71 -0
- data/lib/aspera/ascp/installation.rb +16 -11
- data/lib/aspera/ascp/log_analyzer/lines.rb +4 -4
- data/lib/aspera/ascp/log_analyzer/log_file.rb +5 -3
- data/lib/aspera/ascp/log_analyzer/records.rb +11 -6
- data/lib/aspera/ascp/log_analyzer.rb +43 -18
- data/lib/aspera/ascp/log_locations.rb +2 -2
- data/lib/aspera/assert.rb +8 -0
- data/lib/aspera/bundle_registry.rb +528 -0
- data/lib/aspera/cli/argument.rb +110 -0
- data/lib/aspera/cli/bool_value.rb +41 -0
- data/lib/aspera/cli/bootstrapper.rb +31 -24
- data/lib/aspera/cli/command/mount.rb +53 -0
- data/lib/aspera/cli/command/registry.rb +434 -0
- data/lib/aspera/cli/command.rb +95 -0
- data/lib/aspera/cli/command_line.rb +91 -91
- data/lib/aspera/cli/complete.rb +22 -0
- data/lib/aspera/cli/completion_actions.rb +28 -26
- data/lib/aspera/cli/context.rb +29 -6
- data/lib/aspera/cli/error.rb +39 -0
- data/lib/aspera/cli/extended_value.rb +25 -4
- data/lib/aspera/cli/formatter.rb +8 -8
- data/lib/aspera/cli/hints.rb +17 -52
- data/lib/aspera/cli/http.rb +17 -12
- data/lib/aspera/cli/mailer.rb +7 -24
- data/lib/aspera/cli/mcp/tool.rb +10 -2
- data/lib/aspera/cli/option/declarator.rb +80 -0
- data/lib/aspera/cli/option/registry.rb +74 -0
- data/lib/aspera/cli/option/source.rb +38 -0
- data/lib/aspera/cli/option/type.rb +37 -0
- data/lib/aspera/cli/option/value.rb +376 -0
- data/lib/aspera/cli/option.rb +78 -0
- data/lib/aspera/cli/options.schema.yaml +28 -783
- data/lib/aspera/cli/parser.rb +73 -41
- data/lib/aspera/cli/plugins/base.rb +122 -42
- data/lib/aspera/cli/plugins/basic_auth.rb +13 -2
- data/lib/aspera/cli/plugins/config.rb +101 -167
- data/lib/aspera/cli/plugins/factory.rb +139 -43
- data/lib/aspera/cli/plugins/mcp.rb +61 -54
- data/lib/aspera/cli/plugins/oauth.rb +19 -6
- data/lib/aspera/cli/poll.rb +14 -20
- data/lib/aspera/cli/preset_actions.rb +11 -3
- data/lib/aspera/cli/preset_manager.rb +16 -6
- data/lib/aspera/cli/result.rb +2 -2
- data/lib/aspera/cli/runner.rb +71 -99
- data/lib/aspera/cli/secret_finder.rb +1 -1
- data/lib/aspera/cli/security.rb +102 -4
- data/lib/aspera/{preview/terminal.rb → cli/terminal_image.rb} +93 -94
- data/lib/aspera/cli/vault_manager.rb +3 -3
- data/lib/aspera/cli/version.rb +1 -1
- data/lib/aspera/cli/wizard.rb +9 -3
- data/lib/aspera/coverage.rb +1 -2
- data/lib/aspera/line_logger.rb +35 -6
- data/lib/aspera/log.rb +3 -2
- data/lib/aspera/oauth/client/base.rb +19 -1
- data/lib/aspera/oauth/client/factory.rb +1 -7
- data/lib/aspera/plugin.schema.yaml +178 -0
- data/lib/aspera/plugins/alee/api.rb +25 -0
- data/lib/aspera/{cli/plugins/alee.rb → plugins/alee/commands.rb} +10 -8
- data/lib/aspera/plugins/alee/plugin.yaml +3 -0
- data/lib/aspera/plugins/aoc/admin_resources.rb +148 -0
- data/lib/aspera/plugins/aoc/api.rb +783 -0
- data/lib/aspera/{cli/plugins/aoc.rb → plugins/aoc/commands.rb} +404 -330
- data/lib/aspera/plugins/aoc/graphql.rb +45 -0
- data/lib/aspera/plugins/aoc/plugin.yaml +31 -0
- data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +15042 -7076
- data/lib/aspera/plugins/aoc/schemas/options.yaml +47 -0
- data/lib/aspera/plugins/ats/api.rb +51 -0
- data/lib/aspera/{cli/plugins/ats.rb → plugins/ats/commands.rb} +33 -33
- data/lib/aspera/plugins/ats/plugin.yaml +6 -0
- data/lib/aspera/plugins/connect/agent.rb +199 -0
- data/lib/aspera/plugins/connect/plugin.yaml +11 -0
- data/lib/aspera/plugins/connect/schemas/agent_options.yaml +6 -0
- data/lib/aspera/{cli/plugins/console.rb → plugins/console/commands.rb} +41 -33
- data/lib/aspera/plugins/console/plugin.yaml +8 -0
- data/lib/aspera/plugins/cos/api.rb +114 -0
- data/lib/aspera/{cli/plugins/cos.rb → plugins/cos/commands.rb} +19 -16
- data/lib/aspera/plugins/cos/plugin.yaml +9 -0
- data/lib/aspera/plugins/cos/schemas/options.yaml +30 -0
- data/lib/aspera/plugins/desktop/agent.rb +165 -0
- data/lib/aspera/plugins/desktop/plugin.yaml +11 -0
- data/lib/aspera/plugins/desktop/schemas/agent_options.yaml +6 -0
- data/lib/aspera/plugins/direct/agent.rb +518 -0
- data/lib/aspera/plugins/direct/plugin.yaml +11 -0
- data/lib/aspera/plugins/direct/schemas/agent_options.yaml +86 -0
- data/lib/aspera/plugins/faspex4/gateway_options.rb +18 -0
- data/lib/aspera/plugins/faspex4/gw_servlet.rb +122 -0
- data/lib/aspera/plugins/faspex4/plugin.yaml +7 -0
- data/lib/aspera/plugins/faspex4/schemas/options.yaml +57 -0
- data/lib/aspera/plugins/faspex5/api.rb +269 -0
- data/lib/aspera/{cli/plugins/faspex5.rb → plugins/faspex5/commands.rb} +152 -114
- data/lib/aspera/plugins/faspex5/faspex4_post_proc_servlet.rb +241 -0
- data/lib/aspera/plugins/faspex5/plugin.yaml +14 -0
- data/lib/aspera/{schema → plugins/faspex5/schemas}/IBM Aspera Faspex API-5.0-enhanced.yaml +279 -76
- data/lib/aspera/plugins/faspex5/schemas/options.yaml +87 -0
- data/lib/aspera/{cli/plugins/faspio.rb → plugins/faspio/commands.rb} +11 -7
- data/lib/aspera/plugins/faspio/plugin.yaml +8 -0
- data/lib/aspera/{schema → plugins/faspio/schemas}/IBM Aspera faspio Gateway API-1.0.0-enhanced.yaml +4 -2
- data/lib/aspera/plugins/httpgw/agent.rb +126 -0
- data/lib/aspera/plugins/httpgw/api.rb +411 -0
- data/lib/aspera/{cli/plugins/httpgw.rb → plugins/httpgw/commands.rb} +9 -9
- data/lib/aspera/plugins/httpgw/plugin.yaml +12 -0
- data/lib/aspera/plugins/httpgw/schemas/agent_options.yaml +24 -0
- data/lib/aspera/plugins/node/agent.rb +184 -0
- data/lib/aspera/plugins/node/api.rb +699 -0
- data/lib/aspera/{cli/plugins/node.rb → plugins/node/commands.rb} +166 -141
- data/lib/aspera/plugins/node/emulator.rb +774 -0
- data/lib/aspera/plugins/node/plugin.yaml +20 -0
- data/lib/aspera/plugins/node/schemas/agent_options.yaml +20 -0
- data/lib/aspera/plugins/node/schemas/options.yaml +190 -0
- data/lib/aspera/{cli/plugins/orchestrator.rb → plugins/orchestrator/commands.rb} +139 -110
- data/lib/aspera/plugins/orchestrator/plugin.yaml +34 -0
- data/lib/aspera/{schema → plugins/orchestrator/schemas}/IBM Aspera Orchestrator API-v1-enhanced.yaml +486 -16
- data/lib/aspera/plugins/orchestrator/schemas/options.yaml +143 -0
- data/lib/aspera/plugins/orchestrator/workflow_checker.rb +692 -0
- data/lib/aspera/{cli/plugins/preview.rb → plugins/preview/commands.rb} +53 -51
- data/lib/aspera/plugins/preview/file_types.rb +121 -0
- data/lib/aspera/plugins/preview/generator.rb +350 -0
- data/lib/aspera/plugins/preview/plugin.yaml +12 -0
- data/lib/aspera/{preview/options.schema.yaml → plugins/preview/schemas/options.yaml} +1 -1
- data/lib/aspera/plugins/preview/utils.rb +213 -0
- data/lib/aspera/plugins/server/ascmd.rb +374 -0
- data/lib/aspera/{cli/plugins/server.rb → plugins/server/commands.rb} +48 -32
- data/lib/aspera/plugins/server/plugin.yaml +9 -0
- data/lib/aspera/plugins/server/schemas/options.yaml +30 -0
- data/lib/aspera/{cli/plugins/shares.rb → plugins/shares/commands.rb} +29 -21
- data/lib/aspera/plugins/shares/plugin.yaml +10 -0
- data/lib/aspera/plugins/shares/shares_web.rb +97 -0
- data/lib/aspera/plugins/transferd/agent.rb +258 -0
- data/lib/aspera/plugins/transferd/api.rb +180 -0
- data/lib/aspera/plugins/transferd/client.rb +67 -0
- data/lib/aspera/{cli/plugins/transferd.rb → plugins/transferd/commands.rb} +43 -42
- data/lib/aspera/plugins/transferd/config_commands.rb +38 -0
- data/lib/aspera/plugins/transferd/plugin.yaml +27 -0
- data/lib/aspera/plugins/transferd/product.rb +146 -0
- data/lib/aspera/plugins/transferd/schemas/agent_options.yaml +31 -0
- data/lib/aspera/plugins/transferd/schemas/options.yaml +80 -0
- data/lib/aspera/plugins/transferd/service.rb +617 -0
- data/lib/aspera/plugins/xfer/ascp_actions.rb +131 -0
- data/lib/aspera/plugins/xfer/ascp_logs_actions.rb +198 -0
- data/lib/aspera/plugins/xfer/async_transfer_store.rb +131 -0
- data/lib/aspera/plugins/xfer/config_commands.rb +119 -0
- data/lib/aspera/plugins/xfer/helper.rb +19 -0
- data/lib/aspera/plugins/xfer/plugin.yaml +42 -0
- data/lib/aspera/plugins/xfer/schemas/agent_extension.schema.yaml +26 -0
- data/lib/aspera/plugins/xfer/schemas/options.yaml +125 -0
- data/lib/aspera/plugins/xfer/sync_actions.rb +253 -0
- data/lib/aspera/plugins/xfer/transfer_actions.rb +176 -0
- data/lib/aspera/plugins/xfer/transfer_agent.rb +505 -0
- data/lib/aspera/products/cli_v3.rb +34 -0
- data/lib/aspera/products/connect.rb +1 -1
- data/lib/aspera/products/drive.rb +29 -0
- data/lib/aspera/products/hsts.rb +34 -0
- data/lib/aspera/products/transferd.rb +25 -3
- data/lib/aspera/products.rb +54 -0
- data/lib/aspera/prompt.rb +184 -0
- data/lib/aspera/proxy_auto_config.rb +3 -3
- data/lib/aspera/rest/aspera_errors.rb +4 -38
- data/lib/aspera/rest/error_analyzer.rb +6 -2
- data/lib/aspera/rest/list.rb +4 -5
- data/lib/aspera/schema/reader.rb +15 -0
- data/lib/aspera/schema/registry.rb +239 -45
- data/lib/aspera/schema/validator.rb +63 -17
- data/lib/aspera/sealed_hash.rb +4 -0
- data/lib/aspera/secret/hider.rb +159 -24
- data/lib/aspera/secret/reference.rb +8 -0
- data/lib/aspera/secret/resolver.rb +8 -4
- data/lib/aspera/secret/store/base.rb +10 -9
- data/lib/aspera/secret/store/encrypted_hash.rb +6 -6
- data/lib/aspera/secret/store/hashicorp_vault.rb +3 -3
- data/lib/aspera/secret/store/keeper.rb +2 -2
- data/lib/aspera/secret/store/macos_security.rb +2 -3
- data/lib/aspera/secret/store/one_password_api.rb +2 -2
- data/lib/aspera/secret/store/one_password_cli.rb +3 -3
- data/lib/aspera/secret/store/secret_service.rb +2 -2
- data/lib/aspera/ssl.rb +3 -3
- data/lib/aspera/standard_input.rb +20 -0
- data/lib/aspera/{schema → sync}/async_tables.yaml +1 -1
- data/lib/aspera/sync/database.rb +7 -0
- data/lib/aspera/sync/log_parser.rb +138 -0
- data/lib/aspera/sync/operations.rb +59 -34
- data/lib/aspera/transfer/command_line_builder.rb +248 -0
- data/lib/aspera/transfer/command_line_converter.rb +37 -0
- data/lib/aspera/transfer/faux_file.rb +2 -2
- data/lib/aspera/transfer/parameters.rb +9 -10
- data/lib/aspera/transfer/spec.rb +28 -1
- data/lib/aspera/transfer/spec.schema.yaml +30 -5
- data/lib/aspera/units/decimal.rb +1 -1
- data/lib/aspera/units/timestamp.rb +84 -0
- data/lib/aspera/uri_reader.rb +2 -2
- data/lib/aspera/web_auth/paste.rb +2 -2
- data/lib/aspera/web_server_simple.rb +10 -4
- data.tar.gz.sig +0 -0
- metadata +137 -80
- metadata.gz.sig +0 -0
- data/lib/aspera/agent/connect.rb +0 -188
- data/lib/aspera/agent/desktop.rb +0 -154
- data/lib/aspera/agent/direct.rb +0 -496
- data/lib/aspera/agent/httpgw.rb +0 -100
- data/lib/aspera/agent/node.rb +0 -166
- data/lib/aspera/agent/transferd.rb +0 -341
- data/lib/aspera/api/alee.rb +0 -19
- data/lib/aspera/api/aoc.rb +0 -776
- data/lib/aspera/api/ats.rb +0 -49
- data/lib/aspera/api/cos_node.rb +0 -107
- data/lib/aspera/api/faspex.rb +0 -258
- data/lib/aspera/api/httpgw.rb +0 -409
- data/lib/aspera/api/node.rb +0 -684
- data/lib/aspera/ascmd.rb +0 -248
- data/lib/aspera/cli/ascp_actions.rb +0 -113
- data/lib/aspera/cli/ascp_logs_actions.rb +0 -188
- data/lib/aspera/cli/async_transfer_store.rb +0 -129
- data/lib/aspera/cli/command_registry.rb +0 -371
- data/lib/aspera/cli/command_spec.rb +0 -273
- data/lib/aspera/cli/option_declarator.rb +0 -74
- data/lib/aspera/cli/option_registry.rb +0 -72
- data/lib/aspera/cli/option_types.rb +0 -120
- data/lib/aspera/cli/option_value.rb +0 -345
- data/lib/aspera/cli/prompt.rb +0 -186
- data/lib/aspera/cli/sync_actions.rb +0 -181
- data/lib/aspera/cli/transfer_actions.rb +0 -98
- data/lib/aspera/cli/transfer_agent.rb +0 -348
- data/lib/aspera/command_line_builder.rb +0 -244
- data/lib/aspera/command_line_converter.rb +0 -35
- data/lib/aspera/data_repository.rb +0 -69
- data/lib/aspera/faspex_gw.rb +0 -172
- data/lib/aspera/faspex_postproc.rb +0 -237
- data/lib/aspera/graphql.rb +0 -41
- data/lib/aspera/node_emulator.rb +0 -758
- data/lib/aspera/oauth/client/shares_web.rb +0 -94
- data/lib/aspera/orchestrator_workflow_checker.rb +0 -688
- data/lib/aspera/preview/file_types.rb +0 -119
- data/lib/aspera/preview/generator.rb +0 -345
- data/lib/aspera/preview/utils.rb +0 -211
- data/lib/aspera/products/other.rb +0 -92
- data/lib/aspera/products/transferd_service.rb +0 -610
- /data/lib/aspera/{data → ascp/data}/1 +0 -0
- /data/lib/aspera/{data → ascp/data}/2 +0 -0
- /data/lib/aspera/{data → ascp/data}/3 +0 -0
- /data/lib/aspera/{data → ascp/data}/4 +0 -0
- /data/lib/aspera/{data → ascp/data}/5 +0 -0
- /data/lib/aspera/{data → ascp/data}/6 +0 -0
- /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_account.graphql +0 -0
- /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_usage.graphql +0 -0
- /data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +0 -0
- /data/lib/aspera/{schema → plugins/console/schemas}/IBM Aspera Console-enhanced.yaml +0 -0
- /data/lib/aspera/{schema → plugins/node/schemas}/IBM Aspera Node API-4.4.6.yaml +0 -0
- /data/lib/aspera/{preview → plugins/preview}/image_error.png +0 -0
- /data/lib/aspera/{preview → plugins/preview}/video_error.png +0 -0
- /data/lib/aspera/{schema → plugins/shares/schemas}/IBM_Aspera_Shares.yaml +0 -0
- /data/lib/{transferd_pb.rb → aspera/plugins/transferd/grpc/transferd_pb.rb} +0 -0
- /data/lib/{transferd_services_pb.rb → aspera/plugins/transferd/grpc/transferd_services_pb.rb} +0 -0
- /data/lib/aspera/{schema → plugins/transferd/schemas}/transferd-config.schema.json +0 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: IBM Corp.
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require_relative '../command'
|
|
7
|
+
require_relative '../complete'
|
|
8
|
+
|
|
9
|
+
module Aspera
|
|
10
|
+
module Cli
|
|
11
|
+
class Command
|
|
12
|
+
# Stores Command objects indexed by their full path (Array<Symbol>).
|
|
13
|
+
# Each plugin class gets its own instance (not shared across the inheritance chain).
|
|
14
|
+
#
|
|
15
|
+
# Public API:
|
|
16
|
+
# register(spec) - store a Command; raises on duplicate full_path
|
|
17
|
+
# register_option(spec) - store an Option by name
|
|
18
|
+
# option_specs - Hash{Symbol => Option} of all registered options
|
|
19
|
+
# [](path) - retrieve a Command by full path (follows mounts)
|
|
20
|
+
# children_of(path) - Hash{Symbol => Command} of direct children (follows mounts)
|
|
21
|
+
# resolve(path) - [registry, path] owning the spec at path (follows mounts)
|
|
22
|
+
# local?(path) - true if path is owned by this registry (not reached through a mount)
|
|
23
|
+
# mount_of(path) - Command::Mount of the local node at path, if any
|
|
24
|
+
# mount_at(path) - Command::Mount of the node at path, if any (follows mounts)
|
|
25
|
+
# own_children_of(path) - Hash{Symbol => Command} of direct children declared on the node, without mounted ones
|
|
26
|
+
# arguments_at(path) - Array of Argument read by the node at path (mount arguments first)
|
|
27
|
+
# leaf_paths - Array of all leaf paths (follows mounts, or stops at mount nodes)
|
|
28
|
+
# command_path(words) - command path designated by command line words (aliases, prefixes, arguments)
|
|
29
|
+
# command_position(words) - command path and number of its arguments, as read by the dispatcher
|
|
30
|
+
# all_paths - Array of all locally registered full paths
|
|
31
|
+
# any? - true if at least one spec has been registered
|
|
32
|
+
# validate! - cross-spec consistency checks; raises on violation
|
|
33
|
+
#
|
|
34
|
+
# Paths are always expressed in this registry's namespace: a path going through a
|
|
35
|
+
# mounted node (see Command::Mount) is translated to the target registry transparently.
|
|
36
|
+
# Specs returned for mounted paths are the target's specs (their full_path is in the
|
|
37
|
+
# target namespace).
|
|
38
|
+
class Registry
|
|
39
|
+
# @param path [Array<Symbol>] full path to look up
|
|
40
|
+
# @return [Command, nil]
|
|
41
|
+
def [](path)
|
|
42
|
+
registry, local_path = resolve(path)
|
|
43
|
+
registry.equal?(self) ? @specs[local_path] : registry[local_path]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Find the registry owning `path`, following mounts.
|
|
47
|
+
# A host child always takes precedence over a mounted child with the same id.
|
|
48
|
+
# @param path [Array<Symbol>] path in this registry's namespace
|
|
49
|
+
# @return [Array(Command::Registry, Array<Symbol>)] owning registry and path in its namespace
|
|
50
|
+
def resolve(path)
|
|
51
|
+
path = Array(path)
|
|
52
|
+
path.each_index do |i|
|
|
53
|
+
prefix = path[0, i]
|
|
54
|
+
mount = @specs[prefix]&.mount
|
|
55
|
+
next if mount.nil? || @children_index[prefix]&.key?(path[i]) || !mount.accepts?(path[i])
|
|
56
|
+
return mount.registry.resolve(mount.at + path[i..])
|
|
57
|
+
end
|
|
58
|
+
[self, path]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @param path [Array<Symbol>] path in this registry's namespace
|
|
62
|
+
# @return [Boolean] true if the node at path is declared in this registry (not mounted)
|
|
63
|
+
def local?(path)
|
|
64
|
+
resolve(path).first.equal?(self)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @param path [Array<Symbol>] local path of a node
|
|
68
|
+
# @return [Command::Mount, nil] the mount declared on that node
|
|
69
|
+
def mount_of(path)
|
|
70
|
+
@specs[Array(path)]&.mount
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @param path [Array<Symbol>] path in this registry's namespace
|
|
74
|
+
# @return [Command::Mount, nil] the mount declared on the node at path, following mounts
|
|
75
|
+
def mount_at(path)
|
|
76
|
+
registry, local_path = resolve(path)
|
|
77
|
+
registry.mount_of(local_path)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Children declared on the node itself, without the ones exposed by its mount.
|
|
81
|
+
# @param path [Array<Symbol>] path in this registry's namespace
|
|
82
|
+
# @return [Hash{Symbol => Command}]
|
|
83
|
+
def own_children_of(path)
|
|
84
|
+
registry, local_path = resolve(path)
|
|
85
|
+
return registry.own_children_of(local_path) unless registry.equal?(self)
|
|
86
|
+
@children_index[local_path] || {}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Arguments read by the node at path, in order.
|
|
90
|
+
# For a child exposed by a mount, the mount's arguments come first (read by the host).
|
|
91
|
+
# @param path [Array<Symbol>] path in this registry's namespace
|
|
92
|
+
# @return [Array<Argument>]
|
|
93
|
+
def arguments_at(path)
|
|
94
|
+
path = Array(path)
|
|
95
|
+
(path.empty? ? [] : mount_arguments(path)) + (self[path]&.arguments || [])
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Register a Command. Raises if the full_path is already registered.
|
|
99
|
+
# Also updates the children index so children_of remains O(1).
|
|
100
|
+
# @param spec [Command]
|
|
101
|
+
# @param before [Symbol, nil] Id of a registered child of the same parent: the command is placed before it; `nil`: after the other children
|
|
102
|
+
# @raise [ArgumentError] on duplicate full path, or unknown `before`
|
|
103
|
+
# @return [Command] the registered spec
|
|
104
|
+
def register(spec, before: nil)
|
|
105
|
+
path = spec.full_path
|
|
106
|
+
raise ArgumentError, "Duplicate command path: #{path.inspect}" if @specs.key?(path)
|
|
107
|
+
# Index: parent_path -> { child_id -> spec }
|
|
108
|
+
parent = path[0..-2] # [] for root-level commands
|
|
109
|
+
siblings = (@children_index[parent] ||= {})
|
|
110
|
+
raise ArgumentError, "#{path.inspect}: no command #{before.inspect} to place it before" unless before.nil? || siblings.key?(before)
|
|
111
|
+
@specs[path] = spec
|
|
112
|
+
if before.nil?
|
|
113
|
+
siblings[spec.id] = spec
|
|
114
|
+
else
|
|
115
|
+
@children_index[parent] = siblings.each_with_object({}) do |(id, sibling), placed|
|
|
116
|
+
placed[spec.id] = spec if id.eql?(before)
|
|
117
|
+
placed[id] = sibling
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
spec
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Returns a Hash mapping each child id to its Command for all direct
|
|
124
|
+
# children of `path`. Empty hash if no children are registered.
|
|
125
|
+
# For a mount node: mounted children (filtered) followed by local children,
|
|
126
|
+
# local ones overriding mounted ones with the same id.
|
|
127
|
+
# @param path [Array<Symbol>] parent path ([] for root-level commands)
|
|
128
|
+
# @return [Hash{Symbol => Command}]
|
|
129
|
+
def children_of(path)
|
|
130
|
+
registry, local_path = resolve(path)
|
|
131
|
+
return registry.children_of(local_path) unless registry.equal?(self)
|
|
132
|
+
own = @children_index[local_path] || {}
|
|
133
|
+
mount = @specs[local_path]&.mount
|
|
134
|
+
return own if mount.nil?
|
|
135
|
+
mount.registry.children_of(mount.at).select { |id, _| mount.accepts?(id) }.merge(own)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# All leaf paths under path, in tree order, following mounts.
|
|
139
|
+
# A mount cycle (a sub-tree mounting one of its ancestors) is not expanded twice.
|
|
140
|
+
# @param path [Array<Symbol>] root of the listing ([] for all)
|
|
141
|
+
# @param expand_mounts [Boolean] `false`: a mount node is listed as a leaf, followed by its own children only
|
|
142
|
+
# @return [Array<Array<Symbol>>]
|
|
143
|
+
def leaf_paths(path = [], chain = [], expand_mounts: true)
|
|
144
|
+
children_of(path).keys.flat_map do |id|
|
|
145
|
+
child = path + [id]
|
|
146
|
+
key = subtree_key(child)
|
|
147
|
+
next [] if chain.include?(key)
|
|
148
|
+
if !expand_mounts && mount_at(child)
|
|
149
|
+
next [child] + own_children_of(child).keys.flat_map do |own_id|
|
|
150
|
+
own_child = child + [own_id]
|
|
151
|
+
children_of(own_child).empty? ? [own_child] : leaf_paths(own_child, chain + [key], expand_mounts: false)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
children_of(child).empty? ? [child] : leaf_paths(child, chain + [key], expand_mounts: expand_mounts)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Command path designated by words of a command line: sub-commands (or their aliases, or a unique prefix),
|
|
159
|
+
# each possibly followed by its positional arguments, e.g. `packages receive ALL` designates `packages receive`.
|
|
160
|
+
# Words after a leaf command are its arguments.
|
|
161
|
+
# @param words [Array<Symbol>] words following the plugin name
|
|
162
|
+
# @return [Array(Array<Symbol>, Symbol)] command path, and the first word that is neither a sub-command
|
|
163
|
+
# nor an expected argument (`nil` if none)
|
|
164
|
+
def command_path(words)
|
|
165
|
+
path = []
|
|
166
|
+
# Number of positional arguments still accepted by the node at path
|
|
167
|
+
args_left = 0
|
|
168
|
+
words.each do |word|
|
|
169
|
+
children = children_of(path)
|
|
170
|
+
break if children.empty?
|
|
171
|
+
id = child_id(children, word)
|
|
172
|
+
if id
|
|
173
|
+
path += [id]
|
|
174
|
+
arguments = arguments_at(path)
|
|
175
|
+
args_left = arguments.any?(&:multiple) ? Float::INFINITY : arguments.length
|
|
176
|
+
elsif args_left.positive?
|
|
177
|
+
args_left -= 1
|
|
178
|
+
else
|
|
179
|
+
return [path, word]
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
[path, nil]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Position reached by words of a command line, as read by the dispatcher: an intermediate command reads
|
|
186
|
+
# its positional arguments before its sub-command, a leaf command reads the following words as arguments.
|
|
187
|
+
# Unlike `command_path`, a word expected as argument is never taken as a sub-command.
|
|
188
|
+
# @param words [Array<Symbol>] words following the plugin name
|
|
189
|
+
# @return [Array(Array<Symbol>, Integer), nil] command path, and number of its positional arguments given;
|
|
190
|
+
# `nil` if a word is neither a sub-command nor an expected argument
|
|
191
|
+
def command_position(words)
|
|
192
|
+
path = []
|
|
193
|
+
given = 0
|
|
194
|
+
words.each do |word|
|
|
195
|
+
children = children_of(path)
|
|
196
|
+
arguments = arguments_at(path)
|
|
197
|
+
if given < arguments.length || (children.empty? && arguments.last&.multiple)
|
|
198
|
+
given += 1
|
|
199
|
+
else
|
|
200
|
+
id = child_id(children, word)
|
|
201
|
+
return nil if id.nil?
|
|
202
|
+
path += [id]
|
|
203
|
+
given = 0
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
[path, given]
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# @return [Array<Array<Symbol>>] all locally registered full paths
|
|
210
|
+
def all_paths
|
|
211
|
+
@specs.keys
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Register an Option. Raises if the option name is already registered.
|
|
215
|
+
# @param spec [Option]
|
|
216
|
+
# @param before [Symbol, nil] Name of an option to place this one before, also when that option is registered later
|
|
217
|
+
# (e.g. by the body of a plugin class, after the modules mounted on it); the anchor can itself be placed before another option.
|
|
218
|
+
# An option whose anchor is not registered, or that is in a cycle of anchors, is not an error: see `option_specs`
|
|
219
|
+
# @raise [ArgumentError] on duplicate option name
|
|
220
|
+
# @return [Option] the registered spec
|
|
221
|
+
def register_option(spec, before: nil)
|
|
222
|
+
raise ArgumentError, "Duplicate option: #{spec.name.inspect}" if @option_specs.key?(spec.name)
|
|
223
|
+
@option_before[spec.name] = before unless before.nil?
|
|
224
|
+
@option_specs[spec.name] = spec
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# @param name [Symbol] option name
|
|
228
|
+
# @return [Boolean] true if an option of this name is registered (no ordering)
|
|
229
|
+
def option_registered?(name)
|
|
230
|
+
@option_specs.key?(name)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Options that cannot be placed (the anchor of their `before:` is not registered, or the anchors form a cycle) do not raise,
|
|
234
|
+
# so that a wrong anchor in one module does not break every command: they go at the end, in order of registration,
|
|
235
|
+
# and a warning names each of them and its anchor (logged once per registry, as the list is built several times).
|
|
236
|
+
# @return [Hash{Symbol => Option}] all registered option specs, in order of registration, except those placed before another one (see `register_option`)
|
|
237
|
+
def option_specs
|
|
238
|
+
return @option_specs.dup if @option_before.empty?
|
|
239
|
+
ordered = @option_specs.except(*@option_before.keys)
|
|
240
|
+
pending = @option_before.dup
|
|
241
|
+
until pending.empty?
|
|
242
|
+
# an option whose anchor is itself a pending option waits for the anchor to be placed (chains, whatever the declaration order)
|
|
243
|
+
ready = pending.select { |_name, anchor| ordered.key?(anchor) }
|
|
244
|
+
unless ready.empty?
|
|
245
|
+
ready.each do |name, anchor|
|
|
246
|
+
spec = @option_specs[name]
|
|
247
|
+
ordered = ordered.each_with_object({}) do |(id, other), placed|
|
|
248
|
+
placed[name] = spec if id.eql?(anchor)
|
|
249
|
+
placed[id] = other
|
|
250
|
+
end
|
|
251
|
+
pending.delete(name)
|
|
252
|
+
end
|
|
253
|
+
next
|
|
254
|
+
end
|
|
255
|
+
# the remaining ones cannot be placed: unknown anchor, or cycle
|
|
256
|
+
warn_unplaced(pending)
|
|
257
|
+
@option_specs.each { |name, spec| ordered[name] = spec if pending.key?(name) }
|
|
258
|
+
break
|
|
259
|
+
end
|
|
260
|
+
ordered
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# @return [Boolean] true if at least one spec is registered
|
|
264
|
+
def any?
|
|
265
|
+
!@specs.empty?
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# @return [Boolean] true if no specs have been registered
|
|
269
|
+
def none?
|
|
270
|
+
@specs.empty?
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Cross-spec consistency checks.
|
|
274
|
+
# @param plugin_class [Class, nil] when given, also verify that methods referenced by Symbol
|
|
275
|
+
# (implicit and explicit actions, `setup:`, `condition:`, `lookup:`, mount `instance:`) exist
|
|
276
|
+
# @raise [ArgumentError] on any violation
|
|
277
|
+
# @return [self]
|
|
278
|
+
def validate!(plugin_class: nil)
|
|
279
|
+
@children_index.each do |parent_path, children|
|
|
280
|
+
# Rule: every non-root parent path that appears in the children index must have
|
|
281
|
+
# a registered Command. A missing parent means commands_under(:x) was used
|
|
282
|
+
# without a matching command :x declaration.
|
|
283
|
+
unless parent_path.empty? || @specs.key?(parent_path)
|
|
284
|
+
raise ArgumentError,
|
|
285
|
+
"commands_under(#{parent_path.map(&:inspect).join(', ')}) used but #{parent_path.last.inspect} has no command declaration"
|
|
286
|
+
end
|
|
287
|
+
# Rule: an alias designates a single command, and does not hide a sibling command
|
|
288
|
+
aliases = children.values.flat_map { |c| Array(c.aliases) }
|
|
289
|
+
conflicts = aliases.select { |a| children.key?(a) || aliases.count(a) > 1 }.uniq
|
|
290
|
+
raise ArgumentError, "#{parent_path.inspect}: alias conflicts with a sibling command or alias: #{conflicts.inspect}" unless conflicts.empty?
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
@specs.each_value do |spec|
|
|
294
|
+
path = spec.full_path
|
|
295
|
+
|
|
296
|
+
validate_arguments(path, spec.arguments, plugin_class)
|
|
297
|
+
# Rule: methods called on the plugin instance exist
|
|
298
|
+
if plugin_class
|
|
299
|
+
{setup: spec.setup, condition: spec.condition}.each do |attribute, method_name|
|
|
300
|
+
raise ArgumentError, "#{path.inspect}: no method #{method_name} on #{plugin_class} for #{attribute}:" unless method_name.nil? || instance_method?(plugin_class, method_name)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
if (mount = spec.mount)
|
|
305
|
+
# Rule: a mount needs an instance method, no action, and must point to existing target nodes
|
|
306
|
+
raise ArgumentError, "#{path.inspect}: mount requires instance:" if mount.instance.nil?
|
|
307
|
+
# Mount arguments precede the arguments of the mounted command: they cannot be optional
|
|
308
|
+
raise ArgumentError, "#{path.inspect}: mount arguments must be mandatory" unless mount.arguments.all?(&:mandatory)
|
|
309
|
+
validate_arguments(path, mount.arguments, plugin_class)
|
|
310
|
+
raise ArgumentError, "#{path.inspect}: mount and action: are exclusive" if spec.action
|
|
311
|
+
raise ArgumentError, "#{path.inspect}: mount and read_only: are exclusive" if spec.read_only.eql?(true)
|
|
312
|
+
raise ArgumentError, "#{path.inspect}: mount at #{mount.at.inspect} not found in #{mount.plugin}" unless mount.at.empty? || mount.registry[mount.at]
|
|
313
|
+
target_ids = mount.registry.children_of(mount.at).keys
|
|
314
|
+
unknown = Array(mount.only) + Array(mount.except) - target_ids
|
|
315
|
+
raise ArgumentError, "#{path.inspect}: mount only/except unknown in #{mount.plugin}: #{unknown.inspect}" unless unknown.empty?
|
|
316
|
+
instance_defined = plugin_class.nil? || instance_method?(plugin_class, mount.instance)
|
|
317
|
+
raise ArgumentError, "#{path.inspect}: no method #{mount.instance} on #{plugin_class}" unless instance_defined
|
|
318
|
+
next
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Rule: only a leaf is executed, so only a leaf is read-only
|
|
322
|
+
raise ArgumentError, "#{path.inspect}: read_only: only on a leaf" if spec.read_only.eql?(true) && @children_index[path]&.any?
|
|
323
|
+
next if @children_index[path]&.any? # intermediate node: skip
|
|
324
|
+
action = spec.action
|
|
325
|
+
if action.nil?
|
|
326
|
+
next unless plugin_class
|
|
327
|
+
# Rule: leaf commands with no explicit action must have a matching instance method
|
|
328
|
+
action = Command.action_method(path)
|
|
329
|
+
unless instance_method?(plugin_class, action)
|
|
330
|
+
raise ArgumentError,
|
|
331
|
+
"#{path.inspect}: no action: and no method #{action} on #{plugin_class}"
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
action = plugin_class.instance_method(action) if action.is_a?(Symbol) && plugin_class && instance_method?(plugin_class, action)
|
|
335
|
+
# Rule: the action receives the whole dispatch context as keywords (setup results, arguments),
|
|
336
|
+
# so it must accept any keyword (`**`): a lambda or method with fixed arity would raise ArgumentError.
|
|
337
|
+
# A non-lambda Proc ignores extra keywords.
|
|
338
|
+
next if action.is_a?(Symbol) || (action.is_a?(Proc) && !action.lambda?)
|
|
339
|
+
raise ArgumentError, "#{path.inspect}: action must accept any keyword (**), parameters: #{action.parameters.inspect}" unless action.parameters.any? { |kind, _| kind.eql?(:keyrest) }
|
|
340
|
+
end
|
|
341
|
+
self
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
private
|
|
345
|
+
|
|
346
|
+
# As the parser: an exact name, else the unique name starting with word (e.g. `brow` for alias `browse` of `ls`).
|
|
347
|
+
# @param children [Hash{Symbol => Command}] children of a node
|
|
348
|
+
# @param word [Symbol] command line word
|
|
349
|
+
# @return [Symbol, nil] id of the child designated by word: id or alias, or unique prefix of one
|
|
350
|
+
def child_id(children, word)
|
|
351
|
+
# Name (id or alias) => id
|
|
352
|
+
names = children.each_with_object({}) do |(id, spec), h|
|
|
353
|
+
h[id] = id
|
|
354
|
+
Array(spec.aliases).each { |a| h[a] = id }
|
|
355
|
+
end
|
|
356
|
+
return names[word] if names.key?(word)
|
|
357
|
+
matching = names.keys.select { |name| name.to_s.start_with?(word.to_s) }
|
|
358
|
+
names[matching.first] if matching.length.eql?(1)
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# @return [Boolean] true if plugin_class defines instance method name (public or private)
|
|
362
|
+
def instance_method?(plugin_class, name)
|
|
363
|
+
plugin_class.method_defined?(name) || plugin_class.private_method_defined?(name)
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
# Consistency of the positional arguments of a node, in reading order.
|
|
367
|
+
# @param path [Array<Symbol>] node path, for error messages
|
|
368
|
+
# @param arg_specs [Array<Argument>] arguments of the node (or of its mount)
|
|
369
|
+
# @param plugin_class [Class, nil] when given, verify that `lookup:` methods exist
|
|
370
|
+
# @raise [ArgumentError] on any violation
|
|
371
|
+
def validate_arguments(path, arg_specs, plugin_class)
|
|
372
|
+
previous = nil
|
|
373
|
+
Array(arg_specs).each do |arg|
|
|
374
|
+
where = "#{path.inspect}: argument #{arg.name}"
|
|
375
|
+
# Rule: an argument following an optional one, or one that takes all remaining arguments, is never read reliably
|
|
376
|
+
raise ArgumentError, "#{where}: mandatory after optional #{previous.name}" if previous && arg.mandatory && !previous.mandatory
|
|
377
|
+
raise ArgumentError, "#{where}: after #{previous.name}, which takes all remaining arguments" if previous&.multiple.eql?(true)
|
|
378
|
+
raise ArgumentError, "#{where}: complete: #{arg.complete.inspect} is not one of #{Complete::KINDS.inspect}" unless arg.complete.nil? || Complete::KINDS.include?(arg.complete)
|
|
379
|
+
# Rule: an argument that completes to a local path (`complete: :file` or `:folder`) must declare how the command uses it (`access:`),
|
|
380
|
+
# so that a path chosen by the client is checked against the security level, or marked `:none` when it is not a local path read or written here.
|
|
381
|
+
raise ArgumentError, "#{where}: complete: #{arg.complete.inspect} requires access: (one of #{Argument::ACCESSES.inspect})" if Complete::LOCAL_PATH_KINDS.include?(arg.complete) && arg.access.nil?
|
|
382
|
+
unless arg.lookup.nil?
|
|
383
|
+
# Rule: the percent-selector lookup is only used for identifiers
|
|
384
|
+
raise ArgumentError, "#{where}: lookup: requires type: :identifier" unless arg.type.eql?(:identifier)
|
|
385
|
+
raise ArgumentError, "#{where}: no method #{arg.lookup} on #{plugin_class} for lookup:" if arg.lookup.is_a?(Symbol) && plugin_class && !instance_method?(plugin_class, arg.lookup)
|
|
386
|
+
end
|
|
387
|
+
previous = arg
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# @param path [Array<Symbol>] non-empty path in this registry's namespace
|
|
392
|
+
# @return [Array<Argument>] arguments of the mount exposing the last segment of path, if any
|
|
393
|
+
def mount_arguments(path)
|
|
394
|
+
registry, parent = resolve(path[0..-2])
|
|
395
|
+
return registry.send(:mount_arguments, parent + [path.last]) unless registry.equal?(self)
|
|
396
|
+
mount = @specs[parent]&.mount
|
|
397
|
+
return [] if mount.nil? || @children_index[parent]&.key?(path.last) || !mount.accepts?(path.last)
|
|
398
|
+
mount.arguments
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Identity of the sub-tree exposed at path: the mount point for a mount node, else the owning node.
|
|
402
|
+
# @return [Array(Integer, Array<Symbol>)]
|
|
403
|
+
def subtree_key(path)
|
|
404
|
+
registry, local_path = resolve(path)
|
|
405
|
+
mount = registry.mount_of(local_path)
|
|
406
|
+
mount ? [mount.registry.object_id, mount.at] : [registry.object_id, local_path]
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
# Log a warning for the options that cannot be placed, once per option
|
|
410
|
+
# @param unplaced [Hash{Symbol => Symbol}] option name => anchor
|
|
411
|
+
def warn_unplaced(unplaced)
|
|
412
|
+
fresh = unplaced.reject { |name, _anchor| @option_warned.key?(name) }
|
|
413
|
+
return if fresh.empty?
|
|
414
|
+
fresh.each_key { |name| @option_warned[name] = true }
|
|
415
|
+
Log.log.warn { "Options not placed before their anchor (unknown option or cycle), put at the end: #{fresh.map { |name, anchor| "#{name} before #{anchor}" }.join(', ')}" }
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
def initialize
|
|
419
|
+
# Keyed by Array<Symbol> full path
|
|
420
|
+
@specs = {}
|
|
421
|
+
# Keyed by Symbol option name
|
|
422
|
+
@option_specs = {}
|
|
423
|
+
# Option name => name of the option it is placed before
|
|
424
|
+
@option_before = {}
|
|
425
|
+
# Names of the options already reported by warn_unplaced
|
|
426
|
+
@option_warned = {}
|
|
427
|
+
# Children index: parent Array<Symbol> -> Hash{child_id Symbol => Command}
|
|
428
|
+
# Built incrementally in register(); enables O(1) children_of lookups.
|
|
429
|
+
@children_index = {}
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: IBM Corp.
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require_relative 'security'
|
|
7
|
+
require_relative 'argument'
|
|
8
|
+
|
|
9
|
+
module Aspera
|
|
10
|
+
module Cli
|
|
11
|
+
# Declares a single command node in the flat registry.
|
|
12
|
+
#
|
|
13
|
+
# @!attribute id [Symbol] Unique identifier within its parent's namespace
|
|
14
|
+
# @!attribute parent [Symbol, Array<Symbol>, nil] Full path to parent; nil for root commands
|
|
15
|
+
# @!attribute description [String] User-facing help text
|
|
16
|
+
# @!attribute arguments [Array<Argument>] Positional arguments, in order.
|
|
17
|
+
# The first Argument with type: :identifier is treated as the instance
|
|
18
|
+
# identifier for intermediate nodes (consumed in Phase A) and leaf nodes.
|
|
19
|
+
# @!attribute action [Symbol, Proc, nil] Instance method (Symbol) or inline block (Proc) called when this is a leaf command
|
|
20
|
+
# @!attribute setup [Symbol, nil] Instance method called before dispatching to children; returns Hash merged into ctx
|
|
21
|
+
# @!attribute aliases [Array<Symbol>, nil] Alternative names accepted for this command (each resolves to this command's id)
|
|
22
|
+
# @!attribute transfer_paths [:send, :receive, nil] File-list resolution delegated to TransferAgent; mutually exclusive with arguments
|
|
23
|
+
# @!attribute condition [Symbol, nil] Instance method returning Boolean; if false command is hidden from dispatch
|
|
24
|
+
# @!attribute query_schema [String, nil] Schema path for --query help; when set, the runner hints `--query=help`
|
|
25
|
+
# @!attribute mount [Command::Mount, Hash, nil] Expose children of another plugin's registry under this node
|
|
26
|
+
# @!attribute security [Symbol, nil] Most restrictive security level at which the command is available (`:low`: executes code), see `Security`
|
|
27
|
+
# @!attribute read_only [Boolean, :default] Leaf only: the command changes nothing (on the server or locally), so it can be repeated (option `poll`).
|
|
28
|
+
# `:default` (default): read-only if the id is one of READ_ONLY_IDS (`list`, `show`, ...)
|
|
29
|
+
Command = Struct.new(
|
|
30
|
+
:id,
|
|
31
|
+
:parent,
|
|
32
|
+
:description,
|
|
33
|
+
:arguments,
|
|
34
|
+
:action,
|
|
35
|
+
:setup,
|
|
36
|
+
:aliases,
|
|
37
|
+
:transfer_paths,
|
|
38
|
+
:condition,
|
|
39
|
+
:query_schema,
|
|
40
|
+
:mount,
|
|
41
|
+
:security,
|
|
42
|
+
:read_only,
|
|
43
|
+
keyword_init: true
|
|
44
|
+
) do
|
|
45
|
+
def initialize(**kwargs)
|
|
46
|
+
Aspera.assert_values(kwargs[:security], Security::LEVELS + [nil]) { "#{kwargs[:id]}: security" }
|
|
47
|
+
kwargs[:read_only] = :default if kwargs[:read_only].nil?
|
|
48
|
+
Aspera.assert_values(kwargs[:read_only], [true, false, :default]) { "#{kwargs[:id]}: read_only" }
|
|
49
|
+
# Coerce each element of arguments: from Hash to Argument if needed
|
|
50
|
+
if kwargs[:arguments]
|
|
51
|
+
kwargs[:arguments] = kwargs[:arguments].map do |a|
|
|
52
|
+
a.is_a?(Hash) ? Argument.new(**a) : a
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
kwargs[:mount] = Command::Mount.new(**kwargs[:mount]) if kwargs[:mount].is_a?(Hash)
|
|
56
|
+
super
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class << self
|
|
60
|
+
# Derive the implicit action method name from a path array.
|
|
61
|
+
# e.g. [:admin, :user, :list] -> :action_admin_user_list
|
|
62
|
+
# @param path [Array<Symbol>]
|
|
63
|
+
# @return [Symbol]
|
|
64
|
+
def action_method(path)
|
|
65
|
+
:"action_#{path.join('_')}"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [Boolean] `true` if the command changes nothing: explicit `read_only`, or from its id (READ_ONLY_IDS)
|
|
70
|
+
def read_only?
|
|
71
|
+
read_only.eql?(:default) ? Command::READ_ONLY_IDS.include?(id) : read_only
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Compute the full path as Array<Symbol> from parent + id.
|
|
75
|
+
# @return [Array<Symbol>]
|
|
76
|
+
def full_path
|
|
77
|
+
Array(parent) + [id]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Derive the implicit action method name from the full path.
|
|
81
|
+
# @return [Symbol]
|
|
82
|
+
def action_method_name
|
|
83
|
+
self.class.action_method(full_path)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class Command
|
|
88
|
+
# Ids of the commands read-only by default (`read_only: :default`): they change nothing
|
|
89
|
+
READ_ONLY_IDS = %i[list show status info health ls browse find].freeze
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# `Command::Mount` reopens the struct: loaded once it is defined
|
|
95
|
+
require_relative 'command/mount'
|