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.
Files changed (264) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +4 -4
  3. data/CHANGELOG.md +119 -0
  4. data/CONTRIBUTING.md +8 -1
  5. data/README.md +2 -0
  6. data/TODO.md +4 -5
  7. data/bin/asession +2 -2
  8. data/docs/README.md +616 -86
  9. data/docs/test-mcp-with-ai.md +4 -0
  10. data/lib/aspera/agent/base.rb +62 -6
  11. data/lib/aspera/agent/factory.rb +127 -20
  12. data/lib/aspera/ascp/data_repository.rb +71 -0
  13. data/lib/aspera/ascp/installation.rb +16 -11
  14. data/lib/aspera/ascp/log_analyzer/lines.rb +4 -4
  15. data/lib/aspera/ascp/log_analyzer/log_file.rb +5 -3
  16. data/lib/aspera/ascp/log_analyzer/records.rb +11 -6
  17. data/lib/aspera/ascp/log_analyzer.rb +43 -18
  18. data/lib/aspera/ascp/log_locations.rb +2 -2
  19. data/lib/aspera/assert.rb +8 -0
  20. data/lib/aspera/bundle_registry.rb +528 -0
  21. data/lib/aspera/cli/argument.rb +110 -0
  22. data/lib/aspera/cli/bool_value.rb +41 -0
  23. data/lib/aspera/cli/bootstrapper.rb +31 -24
  24. data/lib/aspera/cli/command/mount.rb +53 -0
  25. data/lib/aspera/cli/command/registry.rb +434 -0
  26. data/lib/aspera/cli/command.rb +95 -0
  27. data/lib/aspera/cli/command_line.rb +91 -91
  28. data/lib/aspera/cli/complete.rb +22 -0
  29. data/lib/aspera/cli/completion_actions.rb +28 -26
  30. data/lib/aspera/cli/context.rb +29 -6
  31. data/lib/aspera/cli/error.rb +39 -0
  32. data/lib/aspera/cli/extended_value.rb +25 -4
  33. data/lib/aspera/cli/formatter.rb +8 -8
  34. data/lib/aspera/cli/hints.rb +17 -52
  35. data/lib/aspera/cli/http.rb +17 -12
  36. data/lib/aspera/cli/mailer.rb +7 -24
  37. data/lib/aspera/cli/mcp/tool.rb +10 -2
  38. data/lib/aspera/cli/option/declarator.rb +80 -0
  39. data/lib/aspera/cli/option/registry.rb +74 -0
  40. data/lib/aspera/cli/option/source.rb +38 -0
  41. data/lib/aspera/cli/option/type.rb +37 -0
  42. data/lib/aspera/cli/option/value.rb +376 -0
  43. data/lib/aspera/cli/option.rb +78 -0
  44. data/lib/aspera/cli/options.schema.yaml +28 -783
  45. data/lib/aspera/cli/parser.rb +73 -41
  46. data/lib/aspera/cli/plugins/base.rb +122 -42
  47. data/lib/aspera/cli/plugins/basic_auth.rb +13 -2
  48. data/lib/aspera/cli/plugins/config.rb +101 -167
  49. data/lib/aspera/cli/plugins/factory.rb +139 -43
  50. data/lib/aspera/cli/plugins/mcp.rb +61 -54
  51. data/lib/aspera/cli/plugins/oauth.rb +19 -6
  52. data/lib/aspera/cli/poll.rb +14 -20
  53. data/lib/aspera/cli/preset_actions.rb +11 -3
  54. data/lib/aspera/cli/preset_manager.rb +16 -6
  55. data/lib/aspera/cli/result.rb +2 -2
  56. data/lib/aspera/cli/runner.rb +71 -99
  57. data/lib/aspera/cli/secret_finder.rb +1 -1
  58. data/lib/aspera/cli/security.rb +102 -4
  59. data/lib/aspera/{preview/terminal.rb → cli/terminal_image.rb} +93 -94
  60. data/lib/aspera/cli/vault_manager.rb +3 -3
  61. data/lib/aspera/cli/version.rb +1 -1
  62. data/lib/aspera/cli/wizard.rb +9 -3
  63. data/lib/aspera/coverage.rb +1 -2
  64. data/lib/aspera/line_logger.rb +35 -6
  65. data/lib/aspera/log.rb +3 -2
  66. data/lib/aspera/oauth/client/base.rb +19 -1
  67. data/lib/aspera/oauth/client/factory.rb +1 -7
  68. data/lib/aspera/plugin.schema.yaml +178 -0
  69. data/lib/aspera/plugins/alee/api.rb +25 -0
  70. data/lib/aspera/{cli/plugins/alee.rb → plugins/alee/commands.rb} +10 -8
  71. data/lib/aspera/plugins/alee/plugin.yaml +3 -0
  72. data/lib/aspera/plugins/aoc/admin_resources.rb +148 -0
  73. data/lib/aspera/plugins/aoc/api.rb +783 -0
  74. data/lib/aspera/{cli/plugins/aoc.rb → plugins/aoc/commands.rb} +404 -330
  75. data/lib/aspera/plugins/aoc/graphql.rb +45 -0
  76. data/lib/aspera/plugins/aoc/plugin.yaml +31 -0
  77. data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +15042 -7076
  78. data/lib/aspera/plugins/aoc/schemas/options.yaml +47 -0
  79. data/lib/aspera/plugins/ats/api.rb +51 -0
  80. data/lib/aspera/{cli/plugins/ats.rb → plugins/ats/commands.rb} +33 -33
  81. data/lib/aspera/plugins/ats/plugin.yaml +6 -0
  82. data/lib/aspera/plugins/connect/agent.rb +199 -0
  83. data/lib/aspera/plugins/connect/plugin.yaml +11 -0
  84. data/lib/aspera/plugins/connect/schemas/agent_options.yaml +6 -0
  85. data/lib/aspera/{cli/plugins/console.rb → plugins/console/commands.rb} +41 -33
  86. data/lib/aspera/plugins/console/plugin.yaml +8 -0
  87. data/lib/aspera/plugins/cos/api.rb +114 -0
  88. data/lib/aspera/{cli/plugins/cos.rb → plugins/cos/commands.rb} +19 -16
  89. data/lib/aspera/plugins/cos/plugin.yaml +9 -0
  90. data/lib/aspera/plugins/cos/schemas/options.yaml +30 -0
  91. data/lib/aspera/plugins/desktop/agent.rb +165 -0
  92. data/lib/aspera/plugins/desktop/plugin.yaml +11 -0
  93. data/lib/aspera/plugins/desktop/schemas/agent_options.yaml +6 -0
  94. data/lib/aspera/plugins/direct/agent.rb +518 -0
  95. data/lib/aspera/plugins/direct/plugin.yaml +11 -0
  96. data/lib/aspera/plugins/direct/schemas/agent_options.yaml +86 -0
  97. data/lib/aspera/plugins/faspex4/gateway_options.rb +18 -0
  98. data/lib/aspera/plugins/faspex4/gw_servlet.rb +122 -0
  99. data/lib/aspera/plugins/faspex4/plugin.yaml +7 -0
  100. data/lib/aspera/plugins/faspex4/schemas/options.yaml +57 -0
  101. data/lib/aspera/plugins/faspex5/api.rb +269 -0
  102. data/lib/aspera/{cli/plugins/faspex5.rb → plugins/faspex5/commands.rb} +152 -114
  103. data/lib/aspera/plugins/faspex5/faspex4_post_proc_servlet.rb +241 -0
  104. data/lib/aspera/plugins/faspex5/plugin.yaml +14 -0
  105. data/lib/aspera/{schema → plugins/faspex5/schemas}/IBM Aspera Faspex API-5.0-enhanced.yaml +279 -76
  106. data/lib/aspera/plugins/faspex5/schemas/options.yaml +87 -0
  107. data/lib/aspera/{cli/plugins/faspio.rb → plugins/faspio/commands.rb} +11 -7
  108. data/lib/aspera/plugins/faspio/plugin.yaml +8 -0
  109. data/lib/aspera/{schema → plugins/faspio/schemas}/IBM Aspera faspio Gateway API-1.0.0-enhanced.yaml +4 -2
  110. data/lib/aspera/plugins/httpgw/agent.rb +126 -0
  111. data/lib/aspera/plugins/httpgw/api.rb +411 -0
  112. data/lib/aspera/{cli/plugins/httpgw.rb → plugins/httpgw/commands.rb} +9 -9
  113. data/lib/aspera/plugins/httpgw/plugin.yaml +12 -0
  114. data/lib/aspera/plugins/httpgw/schemas/agent_options.yaml +24 -0
  115. data/lib/aspera/plugins/node/agent.rb +184 -0
  116. data/lib/aspera/plugins/node/api.rb +699 -0
  117. data/lib/aspera/{cli/plugins/node.rb → plugins/node/commands.rb} +166 -141
  118. data/lib/aspera/plugins/node/emulator.rb +774 -0
  119. data/lib/aspera/plugins/node/plugin.yaml +20 -0
  120. data/lib/aspera/plugins/node/schemas/agent_options.yaml +20 -0
  121. data/lib/aspera/plugins/node/schemas/options.yaml +190 -0
  122. data/lib/aspera/{cli/plugins/orchestrator.rb → plugins/orchestrator/commands.rb} +139 -110
  123. data/lib/aspera/plugins/orchestrator/plugin.yaml +34 -0
  124. data/lib/aspera/{schema → plugins/orchestrator/schemas}/IBM Aspera Orchestrator API-v1-enhanced.yaml +486 -16
  125. data/lib/aspera/plugins/orchestrator/schemas/options.yaml +143 -0
  126. data/lib/aspera/plugins/orchestrator/workflow_checker.rb +692 -0
  127. data/lib/aspera/{cli/plugins/preview.rb → plugins/preview/commands.rb} +53 -51
  128. data/lib/aspera/plugins/preview/file_types.rb +121 -0
  129. data/lib/aspera/plugins/preview/generator.rb +350 -0
  130. data/lib/aspera/plugins/preview/plugin.yaml +12 -0
  131. data/lib/aspera/{preview/options.schema.yaml → plugins/preview/schemas/options.yaml} +1 -1
  132. data/lib/aspera/plugins/preview/utils.rb +213 -0
  133. data/lib/aspera/plugins/server/ascmd.rb +374 -0
  134. data/lib/aspera/{cli/plugins/server.rb → plugins/server/commands.rb} +48 -32
  135. data/lib/aspera/plugins/server/plugin.yaml +9 -0
  136. data/lib/aspera/plugins/server/schemas/options.yaml +30 -0
  137. data/lib/aspera/{cli/plugins/shares.rb → plugins/shares/commands.rb} +29 -21
  138. data/lib/aspera/plugins/shares/plugin.yaml +10 -0
  139. data/lib/aspera/plugins/shares/shares_web.rb +97 -0
  140. data/lib/aspera/plugins/transferd/agent.rb +258 -0
  141. data/lib/aspera/plugins/transferd/api.rb +180 -0
  142. data/lib/aspera/plugins/transferd/client.rb +67 -0
  143. data/lib/aspera/{cli/plugins/transferd.rb → plugins/transferd/commands.rb} +43 -42
  144. data/lib/aspera/plugins/transferd/config_commands.rb +38 -0
  145. data/lib/aspera/plugins/transferd/plugin.yaml +27 -0
  146. data/lib/aspera/plugins/transferd/product.rb +146 -0
  147. data/lib/aspera/plugins/transferd/schemas/agent_options.yaml +31 -0
  148. data/lib/aspera/plugins/transferd/schemas/options.yaml +80 -0
  149. data/lib/aspera/plugins/transferd/service.rb +617 -0
  150. data/lib/aspera/plugins/xfer/ascp_actions.rb +131 -0
  151. data/lib/aspera/plugins/xfer/ascp_logs_actions.rb +198 -0
  152. data/lib/aspera/plugins/xfer/async_transfer_store.rb +131 -0
  153. data/lib/aspera/plugins/xfer/config_commands.rb +119 -0
  154. data/lib/aspera/plugins/xfer/helper.rb +19 -0
  155. data/lib/aspera/plugins/xfer/plugin.yaml +42 -0
  156. data/lib/aspera/plugins/xfer/schemas/agent_extension.schema.yaml +26 -0
  157. data/lib/aspera/plugins/xfer/schemas/options.yaml +125 -0
  158. data/lib/aspera/plugins/xfer/sync_actions.rb +253 -0
  159. data/lib/aspera/plugins/xfer/transfer_actions.rb +176 -0
  160. data/lib/aspera/plugins/xfer/transfer_agent.rb +505 -0
  161. data/lib/aspera/products/cli_v3.rb +34 -0
  162. data/lib/aspera/products/connect.rb +1 -1
  163. data/lib/aspera/products/drive.rb +29 -0
  164. data/lib/aspera/products/hsts.rb +34 -0
  165. data/lib/aspera/products/transferd.rb +25 -3
  166. data/lib/aspera/products.rb +54 -0
  167. data/lib/aspera/prompt.rb +184 -0
  168. data/lib/aspera/proxy_auto_config.rb +3 -3
  169. data/lib/aspera/rest/aspera_errors.rb +4 -38
  170. data/lib/aspera/rest/error_analyzer.rb +6 -2
  171. data/lib/aspera/rest/list.rb +4 -5
  172. data/lib/aspera/schema/reader.rb +15 -0
  173. data/lib/aspera/schema/registry.rb +239 -45
  174. data/lib/aspera/schema/validator.rb +63 -17
  175. data/lib/aspera/sealed_hash.rb +4 -0
  176. data/lib/aspera/secret/hider.rb +159 -24
  177. data/lib/aspera/secret/reference.rb +8 -0
  178. data/lib/aspera/secret/resolver.rb +8 -4
  179. data/lib/aspera/secret/store/base.rb +10 -9
  180. data/lib/aspera/secret/store/encrypted_hash.rb +6 -6
  181. data/lib/aspera/secret/store/hashicorp_vault.rb +3 -3
  182. data/lib/aspera/secret/store/keeper.rb +2 -2
  183. data/lib/aspera/secret/store/macos_security.rb +2 -3
  184. data/lib/aspera/secret/store/one_password_api.rb +2 -2
  185. data/lib/aspera/secret/store/one_password_cli.rb +3 -3
  186. data/lib/aspera/secret/store/secret_service.rb +2 -2
  187. data/lib/aspera/ssl.rb +3 -3
  188. data/lib/aspera/standard_input.rb +20 -0
  189. data/lib/aspera/{schema → sync}/async_tables.yaml +1 -1
  190. data/lib/aspera/sync/database.rb +7 -0
  191. data/lib/aspera/sync/log_parser.rb +138 -0
  192. data/lib/aspera/sync/operations.rb +59 -34
  193. data/lib/aspera/transfer/command_line_builder.rb +248 -0
  194. data/lib/aspera/transfer/command_line_converter.rb +37 -0
  195. data/lib/aspera/transfer/faux_file.rb +2 -2
  196. data/lib/aspera/transfer/parameters.rb +9 -10
  197. data/lib/aspera/transfer/spec.rb +28 -1
  198. data/lib/aspera/transfer/spec.schema.yaml +30 -5
  199. data/lib/aspera/units/decimal.rb +1 -1
  200. data/lib/aspera/units/timestamp.rb +84 -0
  201. data/lib/aspera/uri_reader.rb +2 -2
  202. data/lib/aspera/web_auth/paste.rb +2 -2
  203. data/lib/aspera/web_server_simple.rb +10 -4
  204. data.tar.gz.sig +0 -0
  205. metadata +137 -80
  206. metadata.gz.sig +0 -0
  207. data/lib/aspera/agent/connect.rb +0 -188
  208. data/lib/aspera/agent/desktop.rb +0 -154
  209. data/lib/aspera/agent/direct.rb +0 -496
  210. data/lib/aspera/agent/httpgw.rb +0 -100
  211. data/lib/aspera/agent/node.rb +0 -166
  212. data/lib/aspera/agent/transferd.rb +0 -341
  213. data/lib/aspera/api/alee.rb +0 -19
  214. data/lib/aspera/api/aoc.rb +0 -776
  215. data/lib/aspera/api/ats.rb +0 -49
  216. data/lib/aspera/api/cos_node.rb +0 -107
  217. data/lib/aspera/api/faspex.rb +0 -258
  218. data/lib/aspera/api/httpgw.rb +0 -409
  219. data/lib/aspera/api/node.rb +0 -684
  220. data/lib/aspera/ascmd.rb +0 -248
  221. data/lib/aspera/cli/ascp_actions.rb +0 -113
  222. data/lib/aspera/cli/ascp_logs_actions.rb +0 -188
  223. data/lib/aspera/cli/async_transfer_store.rb +0 -129
  224. data/lib/aspera/cli/command_registry.rb +0 -371
  225. data/lib/aspera/cli/command_spec.rb +0 -273
  226. data/lib/aspera/cli/option_declarator.rb +0 -74
  227. data/lib/aspera/cli/option_registry.rb +0 -72
  228. data/lib/aspera/cli/option_types.rb +0 -120
  229. data/lib/aspera/cli/option_value.rb +0 -345
  230. data/lib/aspera/cli/prompt.rb +0 -186
  231. data/lib/aspera/cli/sync_actions.rb +0 -181
  232. data/lib/aspera/cli/transfer_actions.rb +0 -98
  233. data/lib/aspera/cli/transfer_agent.rb +0 -348
  234. data/lib/aspera/command_line_builder.rb +0 -244
  235. data/lib/aspera/command_line_converter.rb +0 -35
  236. data/lib/aspera/data_repository.rb +0 -69
  237. data/lib/aspera/faspex_gw.rb +0 -172
  238. data/lib/aspera/faspex_postproc.rb +0 -237
  239. data/lib/aspera/graphql.rb +0 -41
  240. data/lib/aspera/node_emulator.rb +0 -758
  241. data/lib/aspera/oauth/client/shares_web.rb +0 -94
  242. data/lib/aspera/orchestrator_workflow_checker.rb +0 -688
  243. data/lib/aspera/preview/file_types.rb +0 -119
  244. data/lib/aspera/preview/generator.rb +0 -345
  245. data/lib/aspera/preview/utils.rb +0 -211
  246. data/lib/aspera/products/other.rb +0 -92
  247. data/lib/aspera/products/transferd_service.rb +0 -610
  248. /data/lib/aspera/{data → ascp/data}/1 +0 -0
  249. /data/lib/aspera/{data → ascp/data}/2 +0 -0
  250. /data/lib/aspera/{data → ascp/data}/3 +0 -0
  251. /data/lib/aspera/{data → ascp/data}/4 +0 -0
  252. /data/lib/aspera/{data → ascp/data}/5 +0 -0
  253. /data/lib/aspera/{data → ascp/data}/6 +0 -0
  254. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_account.graphql +0 -0
  255. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_usage.graphql +0 -0
  256. /data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +0 -0
  257. /data/lib/aspera/{schema → plugins/console/schemas}/IBM Aspera Console-enhanced.yaml +0 -0
  258. /data/lib/aspera/{schema → plugins/node/schemas}/IBM Aspera Node API-4.4.6.yaml +0 -0
  259. /data/lib/aspera/{preview → plugins/preview}/image_error.png +0 -0
  260. /data/lib/aspera/{preview → plugins/preview}/video_error.png +0 -0
  261. /data/lib/aspera/{schema → plugins/shares/schemas}/IBM_Aspera_Shares.yaml +0 -0
  262. /data/lib/{transferd_pb.rb → aspera/plugins/transferd/grpc/transferd_pb.rb} +0 -0
  263. /data/lib/{transferd_services_pb.rb → aspera/plugins/transferd/grpc/transferd_services_pb.rb} +0 -0
  264. /data/lib/aspera/{schema → plugins/transferd/schemas}/transferd-config.schema.json +0 -0
@@ -6,6 +6,7 @@
6
6
  require_relative '../environment'
7
7
  require_relative '../log'
8
8
  require_relative '../products/desktop'
9
+ require_relative '../products/transferd'
9
10
  require_relative 'log_analyzer'
10
11
  require 'json'
11
12
  require 'set'
@@ -121,8 +122,7 @@ module Aspera
121
122
 
122
123
  # @return [String, nil] `fasp_runtime.log.dir` (folder given to `ascp -L`) of the transferd service installed by `transferd service install`
123
124
  def transferd_service_log_folder
124
- require_relative '../products/transferd_service'
125
- file = Products::TransferdService::CONFIG_FILE
125
+ file = Products::Transferd::SERVICE_CONFIG_FILE
126
126
  return unless File.file?(file) && File.readable?(file)
127
127
  folder = JSON.parse(File.read(file)).dig('fasp_runtime', 'log', 'dir')
128
128
  folder if folder.is_a?(String) && File.absolute_path?(folder)
data/lib/aspera/assert.rb CHANGED
@@ -24,6 +24,14 @@ module Aspera
24
24
  class EntityNotFound < Error
25
25
  end
26
26
 
27
+ # A component of the local system is missing or unsuitable, e.g. SDK, tool, font, encoder: neither a bug nor a parameter error
28
+ class EnvironmentError < Error
29
+ end
30
+
31
+ # Credentials refused, e.g. by the login of a server, or password of a file
32
+ class AuthenticationError < Error
33
+ end
34
+
27
35
  class << self
28
36
  # Replaces `raise` in assertion
29
37
  # Allows sending exception, or just error log, when type is `:error`
@@ -0,0 +1,528 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'assert'
7
+ require_relative 'log'
8
+ require_relative 'yaml'
9
+ require_relative 'string_ext'
10
+ require_relative 'schema/registry'
11
+ require_relative 'schema/validator'
12
+ require 'singleton'
13
+
14
+ module Aspera
15
+ # Registry of plugins: single files `<name>.rb` and bundles `<name>/plugin.yaml` of the plugin folders (sources).
16
+ # Reads the manifests of bundles (data only): the code of a plugin is loaded on use, after the check of the security level of its source,
17
+ # of its availability (manifest, dependencies) and of its gems.
18
+ # Gives the hooks of bundles as data: entries of extension points (`extensions`), modules mounted on a plugin (`mounts`), services (`service`);
19
+ # their code is loaded by `load_entry`.
20
+ # Usable without the CLI: the default source is the folder of the bundles of the gem; the CLI adds its folders.
21
+ #
22
+ # @!method self.instance
23
+ # Returns the singleton instance of BundleRegistry
24
+ # @return [BundleRegistry] the singleton instance
25
+ class BundleRegistry
26
+ include Singleton
27
+
28
+ # A plugin cannot be loaded: invalid manifest, dependency missing or unavailable, missing gem
29
+ class Unavailable < Error; end
30
+
31
+ # Folder of the built-in bundles of the gem
32
+ GEM_FOLDER = File.join(__dir__, 'plugins')
33
+ # Manifest of a bundle, in its folder
34
+ MANIFEST = 'plugin.yaml'
35
+ # Module of the constants of bundles: `Aspera::Plugins::<Name>::<Constant>`
36
+ BUNDLE_MODULE = 'Aspera::Plugins'
37
+ # Extension of a single file
38
+ RB_EXT = '.rb'
39
+
40
+ # Folder of plugins
41
+ # @!attribute folder [String] Absolute path
42
+ # @!attribute security [Symbol, nil] Most restrictive security level at which the code of its plugins is used, `nil` for all levels
43
+ # @!attribute ignore [Array<Symbol>] Names of single files that are not plugins (e.g. base classes)
44
+ Source = Data.define(:folder, :security, :ignore)
45
+
46
+ # Entry of an extension point, from manifest `extensions` of the bundle that provides it: data, its code is loaded by `load_entry`
47
+ # @!attribute bundle [Symbol] Name of the bundle that provides the entry
48
+ # @!attribute point [Symbol] Name of the extension point
49
+ # @!attribute fields [Hash] Fields of the entry defined by the schema of the point: all but `file` and `class`
50
+ # @!attribute file [String] File of the code, relative to the folder of the bundle
51
+ # @!attribute constant [String] Class defined by the file, relative to `Aspera::Plugins::<Name>`
52
+ Extension = Data.define(:bundle, :point, :fields, :file, :constant)
53
+
54
+ # Module added to the command tree of another plugin, from manifest `mounts`
55
+ # @!attribute bundle [Symbol] Name of the bundle that provides the module
56
+ # @!attribute target [Symbol] Name of the plugin that includes the module
57
+ # @!attribute file [String] File of the module, relative to the folder of the bundle
58
+ # @!attribute constant [String] Module defined by the file, relative to `Aspera::Plugins::<Name>`
59
+ Mount = Data.define(:bundle, :target, :file, :constant)
60
+
61
+ # Object shared through the context of the CLI, from manifest `services`
62
+ # @!attribute bundle [Symbol] Name of the bundle that provides the service
63
+ # @!attribute name [Symbol] Name of the service
64
+ # @!attribute file [String] File of the class, relative to the folder of the bundle
65
+ # @!attribute constant [String] Class defined by the file, relative to `Aspera::Plugins::<Name>`
66
+ Service = Data.define(:bundle, :name, :file, :constant)
67
+
68
+ # Schema file of a bundle, added to `Schema::Registry`
69
+ # @!attribute bundle [Symbol] Name of the bundle that provides the schema
70
+ # @!attribute key [Symbol] Key of the schema in `Schema::Registry`
71
+ # @!attribute file [String] Absolute path of the schema file
72
+ # @!attribute owned [Boolean] `true`: schema of ascli, values are validated against it
73
+ # @!attribute source [Hash, nil] Origin of a downloaded schema: `url` and optionally `version`
74
+ SchemaFile = Data.define(:bundle, :key, :file, :owned, :source)
75
+
76
+ # Plugin found in a source
77
+ # @!attribute name [Symbol] Name of the plugin
78
+ # @!attribute form [Symbol] `:single` (file `<name>.rb`: commands only) or `:bundle` (folder `<name>/` with a manifest)
79
+ # @!attribute path [String] File of a single file, folder of a bundle
80
+ # @!attribute security [Symbol, nil] Security level of its source
81
+ # @!attribute manifest [Hash] Manifest of a bundle (empty if invalid); for a single file: `name` and `commands`
82
+ # @!attribute error [String, nil] Why the manifest is invalid, `nil` if valid
83
+ Plugin = Data.define(:name, :form, :path, :security, :manifest, :error) do
84
+ # @return [Boolean] `true` for a bundle
85
+ def bundle? = form.eql?(:bundle)
86
+ # @return [String, nil] Title of a bundle
87
+ def title = manifest['title']
88
+ # @return [String, nil] Section of the instructions of the MCP server about the bundle
89
+ def mcp = manifest['mcp']
90
+ # @return [Boolean] `true` if the plugin provides a top level command
91
+ def commands? = manifest['commands'].eql?(true)
92
+ # @return [Array<Symbol>] Bundles referenced by this one
93
+ def depends = manifest.fetch('depends', []).map(&:to_sym)
94
+ # @return [Array<String>] Gems required by the code
95
+ def gems = manifest.fetch('gems', [])
96
+ # @return [Hash{Symbol => String}] Schema file of an entry, relative to the folder of the bundle, by name of the extension points it declares
97
+ def extension_points = manifest.fetch('extension_points', []).to_h { |point| [point['name'].to_sym, point['schema']] }
98
+
99
+ # @return [Array<Extension>] Entries of this bundle for extension points, in the order of the manifest
100
+ def extensions
101
+ manifest.fetch('extensions', {}).flat_map do |point, entries|
102
+ entries.map do |entry|
103
+ Extension.new(bundle: name, point: point.to_sym, fields: entry.except('file', 'class').freeze, file: entry['file'], constant: entry['class'])
104
+ end
105
+ end
106
+ end
107
+
108
+ # @return [Array<Mount>] Modules mounted by this bundle, in the order of the manifest
109
+ def mounts = manifest.fetch('mounts', []).map { |mount| Mount.new(bundle: name, target: mount['target'].to_sym, file: mount['file'], constant: mount['module']) }
110
+ # @return [Array<Service>] Services provided by this bundle, in the order of the manifest
111
+ def services = manifest.fetch('services', []).map { |service| Service.new(bundle: name, name: service['name'].to_sym, file: service['file'], constant: service['class']) }
112
+
113
+ # @return [Array<Array(Symbol, Symbol)>] Names declared by this bundle, in the order of the manifest, with repetitions:
114
+ # `[:service, <name>]`, `[:extension_point, <name>]` and `[:schema, <name>]`
115
+ def declared
116
+ services.map { |service| [:service, service.name] }
117
+ .concat(manifest.fetch('extension_points', []).map { |point| [:extension_point, point['name'].to_sym] })
118
+ .concat(schemas.map { |schema| [:schema, schema.key] })
119
+ end
120
+
121
+ # @return [Array<SchemaFile>] Schemas of this bundle, in the order of the manifest
122
+ def schemas
123
+ manifest.fetch('schemas', []).map do |schema|
124
+ SchemaFile.new(bundle: name, key: schema['key'].to_sym, file: File.join(path, schema['file']), owned: schema.fetch('owned', false), source: schema['source'])
125
+ end
126
+ end
127
+ end
128
+
129
+ # @param builtin [Boolean] `true`: the folder of the bundles of the gem is a source
130
+ def initialize(builtin: true)
131
+ @sources = []
132
+ # Registered plugins, by name
133
+ @plugins = {}
134
+ # Cache of `unavailable_reason`, by name
135
+ @reasons = {}
136
+ # Cache of `missing_gems`, by name
137
+ @missing_gems = {}
138
+ # Source folder of each registered plugin, by name
139
+ @origins = {}
140
+ # `true` if the sources were scanned since the last change of sources or of security checks
141
+ @scanned = false
142
+ # Cache of `claims`
143
+ @claims = nil
144
+ # Cache of `extensions` (key `[:extensions, <point>]`), of `unavailable_extensions` (key `[:unavailable_extensions, <point>]`), of `mounts` (key `[:mounts, <target>]`) and of `schemas` (key `:schemas`): emptied when plugins, sources or security checks change
145
+ @cache = {}
146
+ # Compiled schemas of extension points, by file
147
+ @point_schemas = {}
148
+ @security_allowed = ->(_level) { true }
149
+ @security_check = ->(_level, _what) {}
150
+ add_source(GEM_FOLDER) if builtin
151
+ end
152
+
153
+ # Set the checks of the security level of sources, injected by the user of the registry (the CLI gives the methods of `Cli::Security`).
154
+ # By default, everything is allowed.
155
+ # @param allowed [#call] `(level)`: `true` if a source of this level is scanned at the current level
156
+ # @param check [#call] `(level, what)`: raises if code of a source of this level may not be loaded at the current level
157
+ # @return [nil]
158
+ def security_checks(allowed:, check:)
159
+ @security_allowed = allowed
160
+ @security_check = check
161
+ @scanned = false
162
+ @cache.clear
163
+ nil
164
+ end
165
+
166
+ # @return [Array<String>] Folders of the sources, the first one with priority
167
+ def folders = @sources.map(&:folder)
168
+
169
+ # Add a folder to the sources: a name found in several sources is the plugin of the first one.
170
+ # A folder already added keeps its place, with the new security level.
171
+ # @param folder [String] Folder (absolute, as `require` resolves a relative path against `$LOAD_PATH`)
172
+ # @param security [Symbol, nil] Most restrictive security level at which plugins of this folder are used, `nil` for all levels (folder of the gem).
173
+ # A folder that a user or a client can write is `:low`: a file written there would be loaded as code.
174
+ # @param ignore [Array<Symbol>] Names of files `<name>.rb` that are not plugins
175
+ # @param last [Boolean] `false`: before the other sources, `true`: after them
176
+ # @return [nil]
177
+ def add_source(folder, security: nil, ignore: [], last: false)
178
+ source = Source.new(folder: File.expand_path(folder), security: security, ignore: ignore)
179
+ index = folders.index(source.folder)
180
+ @scanned = false
181
+ @cache.clear
182
+ if index
183
+ @sources[index] = source
184
+ # Plugins already registered from this folder get the new level
185
+ @origins.each { |name, origin| @plugins[name] = @plugins[name].with(security: security) if origin.eql?(source.folder) }
186
+ clear_reasons
187
+ elsif last
188
+ @sources.push(source)
189
+ else
190
+ @sources.unshift(source)
191
+ end
192
+ nil
193
+ end
194
+
195
+ # Register the plugins of the sources that are not registered yet.
196
+ # A registered plugin stays, even if its source is not scanned anymore (e.g. at a higher security level),
197
+ # unless a source added since, with more priority, provides a plugin of the same name.
198
+ # Reads the manifests of bundles: never loads code. An unavailable plugin is logged with the reason.
199
+ # @return [nil]
200
+ def scan
201
+ added = []
202
+ @scanned = true
203
+ @sources.each_with_index do |source, rank|
204
+ next unless File.directory?(source.folder)
205
+ # Plugins registered at start (level low) stay, but files that appear later are not
206
+ next unless @security_allowed.call(source.security)
207
+ singles, bundles = children(source)
208
+ (bundles.keys | singles.keys).each do |name|
209
+ origin = @origins[name]
210
+ next if origin && folders.index(origin) <= rank
211
+ @origins[name] = source.folder
212
+ @plugins[name] =
213
+ if bundles.key?(name)
214
+ Log.log.warn { "Plugin #{name}: bundle #{bundles[name]} is used, not #{singles[name]}" } if singles.key?(name)
215
+ read_bundle(name, bundles[name], source)
216
+ else
217
+ Plugin.new(name: name, form: :single, path: singles[name], security: source.security, manifest: {'name' => name.to_s, 'commands' => true}, error: nil)
218
+ end
219
+ added.push(name)
220
+ end
221
+ end
222
+ return if added.empty?
223
+ clear_reasons
224
+ @cache.clear
225
+ @claims = nil
226
+ added.each do |name|
227
+ reason = unavailable_reason(name)
228
+ Log.log.warn { "Plugin #{name} is unavailable: #{reason}" } unless reason.nil?
229
+ end
230
+ nil
231
+ end
232
+
233
+ # @return [Array<Symbol>] Names of registered plugins, available or not, sorted
234
+ def names = @plugins.keys.sort
235
+
236
+ # @param name [Symbol] Plugin name
237
+ # @return [Plugin, nil] Registered plugin, `nil` if none
238
+ def [](name) = @plugins[name]
239
+
240
+ # @param name [Symbol] Name of a registered plugin
241
+ # @return [String, nil] Why the plugin is unavailable, `nil` if available: invalid manifest, name of a service, extension point or schema already declared by a plugin registered before,
242
+ # dependency missing or unavailable, extension of a point not declared by a dependency or invalid for the schema of the point
243
+ def unavailable_reason(name)
244
+ Aspera.assert(@plugins.key?(name), type: EntityNotFound) { "plugin not found: #{name}" }
245
+ @reasons[name] = dependency_reason(name, []) unless @reasons.key?(name)
246
+ @reasons[name]
247
+ end
248
+
249
+ # @param name [Symbol] Name of a registered plugin
250
+ # @return [Array<String>] Gems of the plugin that are not installed
251
+ def missing_gems(name)
252
+ Aspera.assert(@plugins.key?(name), type: EntityNotFound) { "plugin not found: #{name}" }
253
+ @missing_gems[name] ||= @plugins[name].gems.reject { |gem| gem_installed?(gem) }.freeze
254
+ end
255
+
256
+ # @param name [Symbol] Name of a registered plugin
257
+ # @return [String, nil] Why the code of the plugin cannot be loaded: unavailable, or a gem is missing; `nil` if it can
258
+ def load_error(name)
259
+ reason = unavailable_reason(name)
260
+ return "plugin #{name} is unavailable: #{reason}" unless reason.nil?
261
+ missing = missing_gems(name)
262
+ return "plugin #{name} requires gem #{missing.join(', ')}: gem install #{missing.join(' ')}" unless missing.empty?
263
+ nil
264
+ end
265
+
266
+ # Load a file of the code of a plugin: checks the security level of its source, its availability and its gems.
267
+ # @param name [Symbol] Name of a registered plugin
268
+ # @param file [String, nil] File relative to the folder of a bundle, `nil` for a single file
269
+ # @return [nil]
270
+ # @raise [Unavailable] If the plugin is unavailable or a gem is missing
271
+ def require_code(name, file = nil)
272
+ plugin = @plugins[name]
273
+ Aspera.assert(!plugin.nil?, type: EntityNotFound) { "plugin not found: #{name}" }
274
+ Aspera.assert(file.nil?.eql?(!plugin.bundle?)) { "plugin #{name}: file #{file.inspect} for a #{plugin.form}" }
275
+ # A plugin registered at start can be replaced by a file written since: it is code
276
+ @security_check.call(plugin.security, "plugin #{name}")
277
+ error = load_error(name)
278
+ raise Unavailable, error unless error.nil?
279
+ require(file.nil? ? plugin.path : File.join(plugin.path, file))
280
+ nil
281
+ end
282
+
283
+ # Load a file of a bundle and get a constant it defines, see `require_code`
284
+ # @param name [Symbol] Name of a registered bundle
285
+ # @param file [String] File relative to the folder of the bundle
286
+ # @param constant [String] Constant relative to `Aspera::Plugins::<Name>`
287
+ # @return [Class, Module] The constant
288
+ # @raise [Unavailable] If the bundle is unavailable or a gem is missing
289
+ def load_constant(name, file, constant)
290
+ require_code(name, file)
291
+ Object.const_get(constant_name(name, constant))
292
+ end
293
+
294
+ # @param name [Symbol] Name of a bundle
295
+ # @param constant [String] Constant relative to `Aspera::Plugins::<Name>`
296
+ # @return [String] Full name of the constant, without loading any code
297
+ def constant_name(name, constant) = "#{BUNDLE_MODULE}::#{name.to_s.snake_to_capital}::#{constant}"
298
+
299
+ # Load the code of an entry of a bundle, see `load_constant`
300
+ # @param entry [Extension, Mount, Service] Entry given by `extensions`, `mounts` or `service`
301
+ # @return [Class, Module] The class or module of the entry
302
+ # @raise [Unavailable] If the bundle is unavailable or a gem is missing
303
+ def load_entry(entry) = load_constant(entry.bundle, entry.file, entry.constant)
304
+
305
+ # @param name [Symbol] Name of a registered plugin
306
+ # @return [Boolean] `true` if the security level of the source of the plugin allows to load its code at the current level
307
+ def allowed?(name)
308
+ plugin = @plugins[name]
309
+ Aspera.assert(!plugin.nil?, type: EntityNotFound) { "plugin not found: #{name}" }
310
+ @security_allowed.call(plugin.security)
311
+ end
312
+
313
+ # Entries of an extension point, from the available bundles, data only: the code of an entry is loaded by `load_entry`.
314
+ # A bundle that extends a point depends on the bundle that declares it, its entries are valid for the schema of the point (else it is unavailable).
315
+ # Registers the plugins of the sources first if it was not done.
316
+ # @param point [Symbol] Name of the extension point
317
+ # @return [Array<Extension>] Entries, by name of bundle, then in the order of the manifest; empty if no available bundle declares the point.
318
+ # The same frozen Array is returned until the plugins, the sources or the security checks change.
319
+ def extensions(point)
320
+ Aspera.assert_type(point, Symbol)
321
+ scan unless @scanned
322
+ @cache[[:extensions, point]] ||= available_plugins.flat_map { |plugin| plugin.extensions.select { |extension| extension.point.eql?(point) } }.freeze
323
+ end
324
+
325
+ # Entries of an extension point, from the unavailable bundles whose manifest is valid: data only, to tell why an entry is not used (see `load_error`).
326
+ # @param point [Symbol] Name of the extension point
327
+ # @return [Array<Extension>] Entries, by name of bundle, then in the order of the manifest
328
+ def unavailable_extensions(point)
329
+ Aspera.assert_type(point, Symbol)
330
+ scan unless @scanned
331
+ @cache[[:unavailable_extensions, point]] ||= names.reject { |name| unavailable_reason(name).nil? }.map { |name| @plugins[name] }
332
+ .select { |plugin| plugin.error.nil? }
333
+ .flat_map { |plugin| plugin.extensions.select { |extension| extension.point.eql?(point) } }.freeze
334
+ end
335
+
336
+ # Modules mounted on a plugin, from the available bundles, data only: the code of a module is loaded by `load_entry`.
337
+ # Registers the plugins of the sources first if it was not done (e.g. `config` includes its mounts when it is loaded).
338
+ # @param target [Symbol] Name of the plugin that includes the modules
339
+ # @return [Array<Mount>] Mounts, by name of bundle (a bundle after the bundles it depends on), then in the order of the manifest
340
+ def mounts(target)
341
+ Aspera.assert_type(target, Symbol)
342
+ scan unless @scanned
343
+ @cache[[:mounts, target]] ||= dependencies_first(available_plugins).flat_map { |plugin| plugin.mounts.select { |mount| mount.target.eql?(target) } }.freeze
344
+ end
345
+
346
+ # Service of the bundle that declares it, data only: its class is loaded by `load_entry`.
347
+ # Registers the plugins of the sources first if it was not done.
348
+ # @param name [Symbol] Name of the service
349
+ # @return [Service] The service
350
+ # @raise [Unavailable] If no plugin declares the service, or if the plugin that declares it is unavailable
351
+ def service(name)
352
+ Aspera.assert_type(name, Symbol)
353
+ scan unless @scanned
354
+ provider = claims[[:service, name]]
355
+ raise Unavailable, "no plugin provides service #{name}" if provider.nil?
356
+ reason = unavailable_reason(provider)
357
+ raise Unavailable, "service #{name}: plugin #{provider} is unavailable: #{reason}" unless reason.nil?
358
+ @plugins[provider].services.find { |service| service.name.eql?(name) }
359
+ end
360
+
361
+ # Schemas of the available bundles, data only: the file is read by `Schema::Registry`.
362
+ # Registers the plugins of the sources first if it was not done.
363
+ # @return [Array<SchemaFile>] Schemas, by name of bundle, then in the order of the manifest
364
+ def schemas
365
+ scan unless @scanned
366
+ @cache[:schemas] ||= available_plugins.flat_map(&:schemas).freeze
367
+ end
368
+
369
+ # @param key [Symbol] Key of a schema
370
+ # @return [SchemaFile, nil] The schema, `nil` if no available bundle provides it
371
+ def schema(key)
372
+ Aspera.assert_type(key, Symbol)
373
+ schemas.find { |schema| schema.key.eql?(key) }
374
+ end
375
+
376
+ private
377
+
378
+ # @return [Array<Plugin>] Available plugins, by name
379
+ def available_plugins = names.select { |name| unavailable_reason(name).nil? }.map { |name| @plugins[name] }
380
+
381
+ # @param plugins [Array<Plugin>] Plugins, in order of name
382
+ # @return [Array<Plugin>] Same plugins, each one after those it depends on (among the given ones), else in the given order
383
+ def dependencies_first(plugins)
384
+ by_name = plugins.to_h { |plugin| [plugin.name, plugin] }
385
+ done = {}
386
+ result = []
387
+ visit = lambda do |plugin|
388
+ next if done[plugin.name]
389
+ done[plugin.name] = true # before the dependencies: a cycle stops here
390
+ plugin.depends.each { |dependency| visit.call(by_name[dependency]) if by_name.key?(dependency) }
391
+ result.push(plugin)
392
+ end
393
+ plugins.each { |plugin| visit.call(plugin) }
394
+ result
395
+ end
396
+
397
+ # @return [Array<Plugin>] Registered plugins: by source in order of priority, then by name
398
+ def ordered_plugins = @plugins.values.sort_by { |plugin| [folders.index(@origins[plugin.name]), plugin.name] }
399
+
400
+ # Names of services, extension points and schemas, by the plugin that declares them: the first one with a valid manifest, in the order of the sources (priority), then by name:
401
+ # not the order of registration, which depends on when the sources were scanned.
402
+ # @return [Hash{Array(Symbol, Symbol) => Symbol}] Name of the plugin, by `[:service, <name>]`, `[:extension_point, <name>]` and `[:schema, <name>]`
403
+ def claims
404
+ @claims ||= ordered_plugins.each_with_object({}) do |plugin, result|
405
+ next unless plugin.error.nil?
406
+ plugin.declared.each { |claim| result[claim] ||= plugin.name }
407
+ end
408
+ end
409
+
410
+ # Forget the reasons that plugins are unavailable or miss gems
411
+ def clear_reasons
412
+ @reasons.clear
413
+ @missing_gems.clear
414
+ end
415
+
416
+ # @param plugin [Plugin] Registered plugin
417
+ # @return [String, nil] Why a service, extension point or schema of the plugin is declared twice by it, is a built-in schema or is declared by another plugin, `nil` if none
418
+ def claim_reason(plugin)
419
+ declared = plugin.declared
420
+ declared.each do |kind, name|
421
+ label = "#{kind.to_s.tr('_', ' ')} #{name}"
422
+ return "#{label} is declared twice" if declared.count([kind, name]) > 1
423
+ return "#{label} is a built-in schema" if kind.eql?(:schema) && Schema::Registry.built_in?(name)
424
+ owner = claims[[kind, name]]
425
+ return "#{label} is declared by plugin #{owner}" unless owner.eql?(plugin.name)
426
+ end
427
+ nil
428
+ end
429
+
430
+ # Entries of a bundle for extension points: each point is declared by a dependency, and, for a bundle that is not in the gem, each entry is valid for its schema.
431
+ # The manifests of the gem are validated by a spec: `json_schemer` is not loaded on each start.
432
+ # @param plugin [Plugin] Registered plugin, with available dependencies
433
+ # @return [String, nil] Why an entry is not valid, `nil` if all are valid
434
+ def extensions_reason(plugin)
435
+ plugin.extensions.group_by(&:point).each do |point, extensions|
436
+ declaring = plugin.depends.find { |dependency| @plugins[dependency].extension_points.key?(point) }
437
+ return "extension point #{point} is not declared by a dependency" if declaring.nil?
438
+ next if File.dirname(plugin.path).eql?(GEM_FOLDER)
439
+ schema_file = File.join(@plugins[declaring].path, @plugins[declaring].extension_points[point])
440
+ extensions.each_with_index do |extension, index|
441
+ errors = point_errors(extension.fields, schema_file)
442
+ return "invalid extension #{point} #{index + 1}: #{errors.join('; ')}" unless errors.empty?
443
+ end
444
+ end
445
+ nil
446
+ end
447
+
448
+ # @param fields [Hash] Fields of an extension entry
449
+ # @param schema_file [String] Schema of an entry of the extension point (JSON or YAML)
450
+ # @return [Array<String>] Errors of validation, empty if valid
451
+ def point_errors(fields, schema_file)
452
+ require 'json_schemer'
453
+ @point_schemas[schema_file] ||=
454
+ begin
455
+ schema = Yaml.safe_load(File.read(schema_file))
456
+ Aspera.assert_type(schema, Hash) { "schema #{schema_file}" }
457
+ JSONSchemer.schema(schema)
458
+ end
459
+ @point_schemas[schema_file].validate(fields).map { |error| error['error'] }
460
+ rescue Psych::Exception, AssertError, SystemCallError => e
461
+ ["schema of the extension point: #{e.message}"]
462
+ end
463
+
464
+ # @param source [Source] Source to scan
465
+ # @return [Array(Hash, Hash)] Single files and folders of bundles, by name, sorted: plugins are registered in a stable order (see `claims`)
466
+ def children(source)
467
+ singles = {}
468
+ bundles = {}
469
+ Dir.children(source.folder).sort.each do |child|
470
+ path = File.join(source.folder, child)
471
+ if child.end_with?(RB_EXT)
472
+ name = File.basename(child, RB_EXT).to_sym
473
+ singles[name] = path unless source.ignore.include?(name)
474
+ elsif File.file?(File.join(path, MANIFEST))
475
+ bundles[child.to_sym] = path
476
+ end
477
+ end
478
+ [singles, bundles]
479
+ end
480
+
481
+ # @param name [Symbol] Name of the bundle: name of its folder
482
+ # @param folder [String] Folder of the bundle
483
+ # @param source [Source] Source of the bundle
484
+ # @return [Plugin] The bundle, with the reason if its manifest is invalid
485
+ def read_bundle(name, folder, source)
486
+ # Manifests of the gem are validated by a spec: `json_schemer` is not loaded on each start
487
+ manifest, error = read_manifest(File.join(folder, MANIFEST), name, validate: !source.folder.eql?(GEM_FOLDER))
488
+ Plugin.new(name: name, form: :bundle, path: folder, security: source.security, manifest: manifest, error: error)
489
+ end
490
+
491
+ # @param file [String] Manifest file
492
+ # @param name [Symbol] Name of the bundle: name of its folder
493
+ # @param validate [Boolean] `true`: validate with the schema
494
+ # @return [Array(Hash, String)] Manifest (empty if invalid) and why it is invalid (`nil` if valid)
495
+ def read_manifest(file, name, validate:)
496
+ manifest = Yaml.safe_load(File.read(file))
497
+ return [{}, "invalid manifest #{file}: not a mapping"] unless manifest.is_a?(Hash)
498
+ errors = validate ? Schema::Validator.instance.errors(manifest, Schema::Registry::MANIFEST) : []
499
+ errors.push("name #{manifest['name']} is not the name of the folder #{name}") if errors.empty? && !manifest['name'].eql?(name.to_s)
500
+ return [manifest, nil] if errors.empty?
501
+ [{}, "invalid manifest #{file}: #{errors.join('; ')}"]
502
+ rescue Psych::Exception, AssertError, SystemCallError => e
503
+ [{}, "invalid manifest #{file}: #{e.message}"]
504
+ end
505
+
506
+ # @param name [Symbol] Name of a registered plugin
507
+ # @param chain [Array<Symbol>] Plugins that depend on it, to detect a cycle
508
+ # @return [String, nil] Why the plugin is unavailable, `nil` if available
509
+ def dependency_reason(name, chain)
510
+ plugin = @plugins[name]
511
+ return plugin.error unless plugin.error.nil?
512
+ reason = claim_reason(plugin)
513
+ return reason unless reason.nil?
514
+ chain += [name]
515
+ plugin.depends.each do |dependency|
516
+ return "dependency cycle: #{(chain + [dependency]).join(' → ')}" if chain.include?(dependency)
517
+ return "dependency #{dependency} is missing" unless @plugins.key?(dependency)
518
+ reason = dependency_reason(dependency, chain)
519
+ return "dependency #{dependency} is unavailable: #{reason}" unless reason.nil?
520
+ end
521
+ extensions_reason(plugin)
522
+ end
523
+
524
+ # @param name [String] Name of a gem
525
+ # @return [Boolean] `true` if the gem is installed (in the bundle, with Bundler)
526
+ def gem_installed?(name) = !Gem::Specification.find_all_by_name(name).empty?
527
+ end
528
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative '../standard_input'
7
+
8
+ module Aspera
9
+ module Cli
10
+ # Declares a positional argument consumed by a command.
11
+ # Declaration order in a command's `arguments:` array defines parsing order.
12
+ # Mandatory arguments must come before optional ones.
13
+ #
14
+ # @!attribute name [Symbol] Name used in help and error messages
15
+ # @!attribute description [String] User-facing description
16
+ # @!attribute type [Class, Array<Class>, :identifier, nil] Validated type; :identifier triggers instance_identifier.
17
+ # Default: String (unless allowed:); explicit nil accepts any value
18
+ # @!attribute mandatory [Boolean] Default true; optional args must come after all mandatory ones
19
+ # @!attribute multiple [Boolean, String] true: consume all remaining; String: consume until named marker
20
+ # @!attribute default [Object, nil] Default value when mandatory: false and no argument provided
21
+ # @!attribute schema [String, nil] JSON schema name for validation and --help introspection
22
+ # @!attribute bulk [Boolean] When true, wraps read+loop for bulk mode (Array if --bulk yes)
23
+ # @!attribute lookup [Symbol, Proc, nil] Percent-selector resolver (only used when type: :identifier).
24
+ # Symbol → resolved via send(lookup, field, value, **ctx).
25
+ # Proc/lambda → called via instance_exec(field, value, **ctx, &lookup).
26
+ # Style: use Symbol for named methods; ->(){} for 1-liners; lambda do…end for 2–3 statements.
27
+ # @!attribute allowed [Array<Symbol>, #call, nil] Allowed Symbol values; when set, type is forced to Symbol and accept_list is applied.
28
+ # A callable returns the values when read: the list of values that bundles add after the command is declared (e.g. agents)
29
+ # @!attribute interactive [Boolean] When true, prompts for this argument (only) when no CLI args are provided
30
+ # @!attribute evaluate [Symbol] How the value is read, one of `EVALUATIONS`. Default `:extended`.
31
+ # `:extended`: extended value (`@json:`, `@file:`, `@val:`...); a value without prefix stays a string.
32
+ # `:smart`: `true`/`yes`/`false`/`no` and numbers are converted, else extended value (as dotted options).
33
+ # `:none`: the string as typed, not evaluated (e.g. `@env:`).
34
+ # @!attribute complete [Symbol, nil] Values proposed by shell completion, one of `Complete::KINDS` (e.g. `:file`)
35
+ # @!attribute omit_absent [Boolean] For an optional argument: when not given, it is not passed to the action,
36
+ # so that the action (with its own default) can tell it from a value `nil` (`@none:`)
37
+ # @!attribute access [Symbol, nil] For a local path chosen by the client (`complete: :file` or `:folder`): how the command uses it,
38
+ # one of `ACCESSES`. The value is checked against the security level when the argument is read (`Cli::Security`),
39
+ # and replaced by the real path: `:read` (content read or displayed, even to send it to a remote service),
40
+ # `:write` (file created or modified). A path only sent by a transfer agent (not read by `ascli`) has no `access`.
41
+ # @!attribute stdin [Boolean] With `access: :read`: the command reads the standard input as a file, given as `-` or `@stdin:stream`:
42
+ # the value is then a `StandardInput` (type `String` or `StandardInput`), not checked as a path.
43
+ # Refused where `@stdin:` is disabled (MCP server mode). Without it, `-` is a path like any other.
44
+ Argument = Struct.new(
45
+ :name,
46
+ :description,
47
+ :type,
48
+ :mandatory,
49
+ :multiple,
50
+ :default,
51
+ :schema,
52
+ :bulk,
53
+ :lookup,
54
+ :allowed,
55
+ :interactive,
56
+ :evaluate,
57
+ :complete,
58
+ :omit_absent,
59
+ :access,
60
+ :stdin,
61
+ keyword_init: true
62
+ ) do
63
+ def initialize(**kwargs)
64
+ kwargs[:mandatory] = true if kwargs[:mandatory].nil?
65
+ kwargs[:multiple] = false if kwargs[:multiple].nil?
66
+ kwargs[:bulk] = false if kwargs[:bulk].nil?
67
+ kwargs[:interactive] = false if kwargs[:interactive].nil?
68
+ kwargs[:evaluate] = :extended if kwargs[:evaluate].nil?
69
+ kwargs[:omit_absent] = false if kwargs[:omit_absent].nil?
70
+ kwargs[:stdin] = false if kwargs[:stdin].nil?
71
+ raise ArgumentError, "omit_absent: only for an optional argument: #{kwargs[:name]}" if kwargs[:omit_absent] && kwargs[:mandatory]
72
+ raise ArgumentError, "stdin: sets the type: #{kwargs[:name]}" if kwargs[:stdin] && (kwargs.key?(:type) || kwargs[:allowed])
73
+ kwargs[:type] = [String, StandardInput] if kwargs[:stdin]
74
+ kwargs[:type] = String unless kwargs.key?(:type) || kwargs[:allowed]
75
+ raise ArgumentError, "evaluate: #{kwargs[:evaluate].inspect}, expecting one of: #{Argument::EVALUATIONS.join(', ')}" unless Argument::EVALUATIONS.include?(kwargs[:evaluate])
76
+ raise ArgumentError, "access: #{kwargs[:access].inspect}, expecting one of: #{Argument::ACCESSES.join(', ')}" unless kwargs[:access].nil? || Argument::ACCESSES.include?(kwargs[:access])
77
+ raise ArgumentError, "stdin: only with access: :read: #{kwargs[:name]}" if kwargs[:stdin] && !kwargs[:access].eql?(:read)
78
+ super
79
+ end
80
+
81
+ # The accessor of the Struct is removed first: redefining it would give a Ruby warning
82
+ remove_method :allowed
83
+
84
+ # @return [Array<Symbol>, nil] Allowed values: the result of the callable given to `allowed:`, evaluated at each read
85
+ def allowed
86
+ values = self[:allowed]
87
+ values.respond_to?(:call) ? values.call : values
88
+ end
89
+
90
+ # Syntax of argument for help, e.g. `<name>`, `[<name>]`, `<paths...>`, `<account:Hash>`.
91
+ # Type is shown only when the argument is not free text.
92
+ # @return [String]
93
+ def syntax
94
+ token = allowed ? allowed.join('|') : name.to_s
95
+ token += '...' if multiple
96
+ types = Array(type).grep(Class)
97
+ token += ":#{types.map(&:name).join('|')}" unless allowed || types.empty? || types.include?(String)
98
+ mandatory ? "<#{token}>" : "[<#{token}>]"
99
+ end
100
+ end
101
+
102
+ class Argument
103
+ # Ways to evaluate the value of an argument, see `Argument#evaluate`
104
+ EVALUATIONS = %i[extended smart none].freeze
105
+ # How a command uses a path given as an argument, see `Argument#access`.
106
+ # `:none`: not a local path read or written by `ascli` (a remote path, or a path checked elsewhere, e.g. a sync folder given to the local `async`).
107
+ ACCESSES = %i[read write none].freeze
108
+ end
109
+ end
110
+ end