aspera-cli 4.28.0 → 4.29.1

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 (242) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/BUGS.md +5 -10
  4. data/CHANGELOG.md +217 -2
  5. data/CODE_OF_CONDUCT.md +18 -0
  6. data/CONTRIBUTING.md +135 -56
  7. data/GOVERNANCE.md +30 -0
  8. data/LICENSE +201 -0
  9. data/README.md +122 -26
  10. data/SECURITY.md +84 -0
  11. data/bin/ascli +8 -5
  12. data/bin/asession +9 -6
  13. data/docs/README.md +1895 -632
  14. data/docs/test-mcp-with-ai.md +8 -8
  15. data/lib/aspera/agent/base.rb +23 -2
  16. data/lib/aspera/agent/connect.rb +16 -12
  17. data/lib/aspera/agent/desktop.rb +10 -7
  18. data/lib/aspera/agent/direct.rb +20 -19
  19. data/lib/aspera/agent/factory.rb +6 -3
  20. data/lib/aspera/agent/httpgw.rb +7 -5
  21. data/lib/aspera/agent/node.rb +15 -7
  22. data/lib/aspera/agent/transferd.rb +167 -43
  23. data/lib/aspera/api/alee.rb +4 -1
  24. data/lib/aspera/api/aoc.rb +14 -11
  25. data/lib/aspera/api/ats.rb +4 -2
  26. data/lib/aspera/api/cos_node.rb +7 -4
  27. data/lib/aspera/api/faspex.rb +16 -9
  28. data/lib/aspera/api/httpgw.rb +9 -6
  29. data/lib/aspera/api/node.rb +29 -22
  30. data/lib/aspera/ascmd.rb +8 -5
  31. data/lib/aspera/ascp/installation.rb +95 -67
  32. data/lib/aspera/ascp/log.schema.yaml +1084 -0
  33. data/lib/aspera/ascp/log_analyzer/advice.rb +97 -0
  34. data/lib/aspera/ascp/log_analyzer/diagnostics.rb +344 -0
  35. data/lib/aspera/ascp/log_analyzer/lines.rb +75 -0
  36. data/lib/aspera/ascp/log_analyzer/log_file.rb +593 -0
  37. data/lib/aspera/ascp/log_analyzer/records.rb +379 -0
  38. data/lib/aspera/ascp/log_analyzer/rules.rb +172 -0
  39. data/lib/aspera/ascp/log_analyzer/session.rb +782 -0
  40. data/lib/aspera/ascp/log_analyzer.rb +370 -0
  41. data/lib/aspera/ascp/log_locations.rb +163 -0
  42. data/lib/aspera/ascp/management.rb +16 -8
  43. data/lib/aspera/ascp/session_errors.rb +217 -0
  44. data/lib/aspera/assert.rb +4 -0
  45. data/lib/aspera/cli/ascp_actions.rb +12 -19
  46. data/lib/aspera/cli/ascp_logs_actions.rb +188 -0
  47. data/lib/aspera/cli/async_transfer_store.rb +7 -4
  48. data/lib/aspera/cli/bootstrapper.rb +38 -33
  49. data/lib/aspera/cli/command_line.rb +63 -8
  50. data/lib/aspera/cli/command_registry.rb +9 -1
  51. data/lib/aspera/cli/command_spec.rb +47 -7
  52. data/lib/aspera/cli/completion/ascli.bash +19 -5
  53. data/lib/aspera/cli/completion/ascli.fish +13 -2
  54. data/lib/aspera/cli/completion/ascli.ps1 +31 -9
  55. data/lib/aspera/cli/completion/ascli.zsh +22 -6
  56. data/lib/aspera/cli/completion_actions.rb +303 -69
  57. data/lib/aspera/cli/context.rb +9 -4
  58. data/lib/aspera/cli/deprecation.rb +5 -2
  59. data/lib/aspera/cli/error.rb +6 -0
  60. data/lib/aspera/cli/extended_value.rb +66 -31
  61. data/lib/aspera/cli/formatter.rb +43 -30
  62. data/lib/aspera/cli/gem_checker.rb +9 -6
  63. data/lib/aspera/cli/hints.rb +22 -10
  64. data/lib/aspera/cli/http.rb +38 -10
  65. data/lib/aspera/cli/info.rb +12 -7
  66. data/lib/aspera/cli/mailer.rb +6 -3
  67. data/lib/aspera/cli/mcp/http_servlet.rb +7 -8
  68. data/lib/aspera/cli/mcp/secrets.rb +8 -5
  69. data/lib/aspera/cli/mcp/tool.rb +20 -17
  70. data/lib/aspera/cli/option_declarator.rb +10 -4
  71. data/lib/aspera/cli/option_registry.rb +6 -3
  72. data/lib/aspera/cli/option_types.rb +17 -2
  73. data/lib/aspera/cli/option_value.rb +77 -18
  74. data/lib/aspera/cli/options.schema.yaml +641 -80
  75. data/lib/aspera/cli/parser.rb +71 -35
  76. data/lib/aspera/cli/plugins/alee.rb +7 -4
  77. data/lib/aspera/cli/plugins/aoc.rb +248 -37
  78. data/lib/aspera/cli/plugins/ats.rb +10 -9
  79. data/lib/aspera/cli/plugins/base.rb +50 -18
  80. data/lib/aspera/cli/plugins/basic_auth.rb +5 -2
  81. data/lib/aspera/cli/plugins/config.rb +210 -104
  82. data/lib/aspera/cli/plugins/console.rb +6 -3
  83. data/lib/aspera/cli/plugins/cos.rb +8 -4
  84. data/lib/aspera/cli/plugins/factory.rb +21 -7
  85. data/lib/aspera/cli/plugins/faspex5.rb +31 -26
  86. data/lib/aspera/cli/plugins/faspio.rb +7 -5
  87. data/lib/aspera/cli/plugins/httpgw.rb +7 -4
  88. data/lib/aspera/cli/plugins/mcp.rb +42 -22
  89. data/lib/aspera/cli/plugins/node.rb +39 -38
  90. data/lib/aspera/cli/plugins/oauth.rb +23 -6
  91. data/lib/aspera/cli/plugins/orchestrator.rb +287 -83
  92. data/lib/aspera/cli/plugins/preview.rb +72 -45
  93. data/lib/aspera/cli/plugins/server.rb +66 -14
  94. data/lib/aspera/cli/plugins/shares.rb +87 -18
  95. data/lib/aspera/cli/plugins/transferd.rb +228 -0
  96. data/lib/aspera/cli/poll.rb +236 -0
  97. data/lib/aspera/cli/preset_actions.rb +144 -30
  98. data/lib/aspera/cli/preset_manager.rb +113 -35
  99. data/lib/aspera/cli/prompt.rb +157 -9
  100. data/lib/aspera/cli/result.rb +12 -8
  101. data/lib/aspera/cli/runner.rb +89 -48
  102. data/lib/aspera/cli/secret_finder.rb +4 -1
  103. data/lib/aspera/cli/security.rb +89 -9
  104. data/lib/aspera/cli/special_values.rb +4 -1
  105. data/lib/aspera/cli/sync_actions.rb +15 -10
  106. data/lib/aspera/cli/table_fitter.rb +3 -0
  107. data/lib/aspera/cli/terminal_formatter.rb +9 -6
  108. data/lib/aspera/cli/transfer_actions.rb +11 -8
  109. data/lib/aspera/cli/transfer_agent.rb +43 -20
  110. data/lib/aspera/cli/transfer_progress.rb +14 -45
  111. data/lib/aspera/cli/vault_actions.rb +45 -0
  112. data/lib/aspera/cli/vault_manager.rb +188 -42
  113. data/lib/aspera/cli/version.rb +4 -1
  114. data/lib/aspera/cli/web_secret.rb +12 -10
  115. data/lib/aspera/cli/wizard.rb +42 -39
  116. data/lib/aspera/command_line_builder.rb +9 -5
  117. data/lib/aspera/command_line_converter.rb +4 -1
  118. data/lib/aspera/coverage.rb +103 -33
  119. data/lib/aspera/data_repository.rb +4 -1
  120. data/lib/aspera/dot_container.rb +4 -1
  121. data/lib/aspera/environment.rb +23 -12
  122. data/lib/aspera/exec_spec.rb +3 -0
  123. data/lib/aspera/faspex_gw.rb +13 -9
  124. data/lib/aspera/faspex_postproc.rb +15 -12
  125. data/lib/aspera/formatter_interface.rb +4 -1
  126. data/lib/aspera/graphql.rb +5 -1
  127. data/lib/aspera/hash_ext.rb +3 -0
  128. data/lib/aspera/id_generator.rb +20 -5
  129. data/lib/aspera/inflate.rb +43 -0
  130. data/lib/aspera/json_rpc/client.rb +6 -5
  131. data/lib/aspera/json_rpc/version.rb +3 -0
  132. data/lib/aspera/line_logger.rb +4 -1
  133. data/lib/aspera/link_header.rb +4 -1
  134. data/lib/aspera/log.rb +18 -5
  135. data/lib/aspera/markdown.rb +5 -2
  136. data/lib/aspera/mime.rb +3 -0
  137. data/lib/aspera/nagios.rb +6 -4
  138. data/lib/aspera/node_emulator.rb +15 -13
  139. data/lib/aspera/oauth/client/base.rb +124 -73
  140. data/lib/aspera/oauth/client/boot.rb +7 -4
  141. data/lib/aspera/oauth/client/factory.rb +41 -18
  142. data/lib/aspera/oauth/client/generic.rb +5 -1
  143. data/lib/aspera/oauth/client/json_credentials.rb +7 -3
  144. data/lib/aspera/oauth/client/jwt.rb +6 -3
  145. data/lib/aspera/oauth/client/shares_web.rb +94 -0
  146. data/lib/aspera/oauth/client/url_json.rb +7 -3
  147. data/lib/aspera/oauth/client/web.rb +40 -13
  148. data/lib/aspera/oauth/server/pkce.rb +6 -5
  149. data/lib/aspera/orchestrator_workflow_checker.rb +129 -2
  150. data/lib/aspera/persistency_action_once.rb +5 -3
  151. data/lib/aspera/persistency_folder.rb +6 -3
  152. data/lib/aspera/preview/file_types.rb +6 -3
  153. data/lib/aspera/preview/generator.rb +9 -5
  154. data/lib/aspera/preview/options.schema.yaml +3 -3
  155. data/lib/aspera/preview/terminal.rb +16 -6
  156. data/lib/aspera/preview/utils.rb +6 -4
  157. data/lib/aspera/products/connect.rb +5 -2
  158. data/lib/aspera/products/desktop.rb +17 -2
  159. data/lib/aspera/products/other.rb +4 -2
  160. data/lib/aspera/products/transferd.rb +6 -2
  161. data/lib/aspera/products/transferd_service.rb +610 -0
  162. data/lib/aspera/proxy_auto_config.js +2 -2
  163. data/lib/aspera/proxy_auto_config.rb +5 -2
  164. data/lib/aspera/rainbow.rb +3 -0
  165. data/lib/aspera/rest/aspera_errors.rb +4 -1
  166. data/lib/aspera/rest/call_error.rb +3 -0
  167. data/lib/aspera/rest/client.rb +45 -34
  168. data/lib/aspera/rest/error_analyzer.rb +5 -2
  169. data/lib/aspera/rest/list.rb +8 -4
  170. data/lib/aspera/rest/parameters.rb +3 -0
  171. data/lib/aspera/rest/util.rb +8 -5
  172. data/lib/aspera/rest.rb +9 -6
  173. data/lib/aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml +9 -9
  174. data/lib/aspera/schema/IBM Aspera Orchestrator API-v1-enhanced.yaml +118 -30
  175. data/lib/aspera/schema/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +11 -11
  176. data/lib/aspera/schema/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +2437 -1437
  177. data/lib/aspera/schema/async_tables.yaml +11 -10
  178. data/lib/aspera/schema/documentation.rb +8 -2
  179. data/lib/aspera/schema/reader.rb +7 -0
  180. data/lib/aspera/schema/registry.rb +44 -28
  181. data/lib/aspera/schema/transferd-config.schema.json +333 -0
  182. data/lib/aspera/schema/validator.rb +4 -1
  183. data/lib/aspera/sealed_hash.rb +150 -0
  184. data/lib/aspera/secret/hider.rb +213 -0
  185. data/lib/aspera/secret/reference.rb +150 -0
  186. data/lib/aspera/secret/resolver.rb +212 -0
  187. data/lib/aspera/secret/store/base.rb +153 -0
  188. data/lib/aspera/secret/store/encrypted_hash.rb +329 -0
  189. data/lib/aspera/secret/store/factory.rb +103 -0
  190. data/lib/aspera/secret/store/hashicorp_vault.rb +390 -0
  191. data/lib/aspera/secret/store/keeper.rb +141 -0
  192. data/lib/aspera/secret/store/macos_security.rb +284 -0
  193. data/lib/aspera/secret/store/one_password_api.rb +99 -0
  194. data/lib/aspera/secret/store/one_password_base.rb +42 -0
  195. data/lib/aspera/secret/store/one_password_cli.rb +112 -0
  196. data/lib/aspera/secret/store/secret_service.rb +125 -0
  197. data/lib/aspera/secret/strength.rb +119 -0
  198. data/lib/aspera/servlet_helpers.rb +5 -2
  199. data/lib/aspera/ssh.rb +10 -7
  200. data/lib/aspera/ssl.rb +5 -2
  201. data/lib/aspera/string_ext.rb +3 -0
  202. data/lib/aspera/sync/args.schema.yaml +3 -0
  203. data/lib/aspera/sync/conf.schema.yaml +9 -4
  204. data/lib/aspera/sync/database.rb +3 -0
  205. data/lib/aspera/sync/operations.rb +17 -15
  206. data/lib/aspera/temp_file_manager.rb +8 -4
  207. data/lib/aspera/timer_limiter.rb +3 -0
  208. data/lib/aspera/transfer/error.rb +4 -1
  209. data/lib/aspera/transfer/faux_file.rb +4 -1
  210. data/lib/aspera/transfer/parameters.rb +17 -14
  211. data/lib/aspera/transfer/result.rb +23 -0
  212. data/lib/aspera/transfer/resumer.rb +6 -4
  213. data/lib/aspera/transfer/spec.rb +9 -22
  214. data/lib/aspera/units/decimal.rb +67 -0
  215. data/lib/aspera/units/duration.rb +109 -0
  216. data/lib/aspera/uri_reader.rb +64 -9
  217. data/lib/aspera/web_auth/index.html.erb +6 -2
  218. data/lib/aspera/web_auth/paste.rb +109 -0
  219. data/lib/aspera/web_auth/receiver.rb +50 -0
  220. data/lib/aspera/web_auth/relay.rb +76 -0
  221. data/lib/aspera/web_auth/reply.html.erb +150 -0
  222. data/lib/aspera/web_auth/server.rb +47 -10
  223. data/lib/aspera/web_page.rb +7 -2
  224. data/lib/aspera/web_server_simple.rb +6 -3
  225. data/lib/aspera/yaml.rb +5 -1
  226. data/llms-install.md +88 -0
  227. data/llms.txt +87 -0
  228. data/skills/ascli/SKILL.md +94 -0
  229. data.tar.gz.sig +0 -0
  230. metadata +77 -20
  231. metadata.gz.sig +0 -0
  232. data/lib/aspera/duration.rb +0 -42
  233. data/lib/aspera/keychain/base.rb +0 -30
  234. data/lib/aspera/keychain/encrypted_hash.rb +0 -192
  235. data/lib/aspera/keychain/factory.rb +0 -58
  236. data/lib/aspera/keychain/hashicorp_vault.rb +0 -77
  237. data/lib/aspera/keychain/macos_security.rb +0 -277
  238. data/lib/aspera/keychain/one_password_api.rb +0 -91
  239. data/lib/aspera/keychain/one_password_base.rb +0 -39
  240. data/lib/aspera/keychain/one_password_cli.rb +0 -100
  241. data/lib/aspera/secret_hider.rb +0 -196
  242. data/lib/aspera/secret_strength.rb +0 -114
@@ -1,24 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/cli/plugins/factory'
4
- require 'aspera/cli/extended_value'
5
- require 'aspera/cli/special_values'
6
- require 'aspera/cli/info'
7
- require 'aspera/cli/transfer_progress'
8
- require 'aspera/cli/preset_manager'
9
- require 'aspera/cli/http'
10
- require 'aspera/ascp/installation'
11
- require 'aspera/products/transferd'
12
- require 'aspera/transfer/parameters'
13
- require 'aspera/proxy_auto_config'
14
- require 'aspera/environment'
15
- require 'aspera/persistency_folder'
16
- require 'aspera/rest'
17
- require 'aspera/log'
18
- require 'aspera/assert'
19
- require 'aspera/oauth/client/web'
20
- require 'aspera/ssl'
21
- require 'aspera/schema/registry'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'plugins/factory'
7
+ require_relative 'extended_value'
8
+ require_relative 'special_values'
9
+ require_relative 'info'
10
+ require_relative 'transfer_progress'
11
+ require_relative 'preset_manager'
12
+ require_relative 'vault_manager'
13
+ require_relative 'http'
14
+ require_relative '../ascp/installation'
15
+ require_relative '../products/transferd'
16
+ require_relative '../transfer/parameters'
17
+ require_relative '../proxy_auto_config'
18
+ require_relative '../environment'
19
+ require_relative '../persistency_folder'
20
+ require_relative '../rest'
21
+ require_relative '../log'
22
+ require_relative '../assert'
23
+ require_relative '../oauth/client/web'
24
+ require_relative '../ssl'
25
+ require_relative '../schema/registry'
22
26
 
23
27
  module Aspera
24
28
  module Cli
@@ -28,7 +32,7 @@ module Aspera
28
32
  #
29
33
  # Responsibilities:
30
34
  # - resolve context.main_folder (option :home)
31
- # - populate context.persistency, context.presets, context.http_config, context.progress_bar
35
+ # - populate context.persistency, context.presets, context.vault, context.http_config, context.progress_bar
32
36
  # - register plugin lookup folders
33
37
  # - register @preset / @vault extended-value handlers
34
38
  # - configure global singletons: Rest::Parameters, OAuth::Factory, SSL, Transfer::Parameters,
@@ -73,16 +77,15 @@ module Aspera
73
77
 
74
78
  # Run the full bootstrap sequence.
75
79
  # Must be called after context.options and context.formatter are set.
76
- # On return, context.persistency, context.presets, context.http_config and
80
+ # On return, context.persistency, context.presets, context.vault, context.http_config and
77
81
  # context.progress_bar are populated and all global singletons are configured.
78
82
  #
79
83
  # @param gem_plugins_folder [String] folder of built-in plugins (from Plugins::Config)
80
- # @param vault_value_cb [Proc] block(name): secret value (from VaultManager)
81
- def run(gem_plugins_folder:, vault_value_cb:)
84
+ def run(gem_plugins_folder:)
82
85
  setup_main_folder
83
86
  setup_persistency_and_plugin_folders(gem_plugins_folder)
84
87
  setup_config_file
85
- setup_extended_value_handlers(vault_value_cb)
88
+ setup_extended_value_handlers
86
89
  setup_progress_bar
87
90
  setup_pac_executor
88
91
  setup_rest_and_transfer_runtime
@@ -94,7 +97,7 @@ module Aspera
94
97
  def setup_main_folder
95
98
  @context.options.declare(
96
99
  :home, description: 'Home folder for tool',
97
- on_set: @context.method(:main_folder=),
100
+ on_set: @context.method(:main_folder=), complete: :folder, security: :low,
98
101
  default: default_app_main_folder(app_name: Info::CMD_NAME)
99
102
  )
100
103
  @context.options.parse_options!
@@ -105,13 +108,13 @@ module Aspera
105
108
  def setup_persistency_and_plugin_folders(gem_plugins_folder)
106
109
  @context.persistency = PersistencyFolder.new(File.join(@context.main_folder, PERSISTENCY_FOLDER))
107
110
  Plugins::Factory.instance.add_lookup_folder(gem_plugins_folder)
108
- Plugins::Factory.instance.add_lookup_folder(File.join(@context.main_folder, ASPERA_PLUGINS_FOLDERNAME))
111
+ Plugins::Factory.instance.add_lookup_folder(File.join(@context.main_folder, ASPERA_PLUGINS_FOLDERNAME), security: :low)
109
112
  end
110
113
 
111
114
  # Declare + parse :config_file option: sets context.presets + context.http_config
112
115
  def setup_config_file
113
116
  @context.options.declare(
114
- :config_file, description: 'Path to YAML file with preset configuration',
117
+ :config_file, description: 'Path to YAML file with preset configuration', complete: :file,
115
118
  default: File.join(@context.main_folder, DEFAULT_CONFIG_FILENAME)
116
119
  )
117
120
  @context.options.parse_options!
@@ -119,15 +122,16 @@ module Aspera
119
122
  @context.http_config = Http.new
120
123
  end
121
124
 
122
- # Register @preset and @vault extended-value handlers + global config default preset
123
- def setup_extended_value_handlers(vault_value_cb)
125
+ # context.vault + register @preset and @vault extended-value handlers + global config default preset
126
+ def setup_extended_value_handlers
124
127
  @context.options.declare(:secret, description: 'Secret for access keys')
125
- @context.options.declare(:vault, schema: Schema::Registry::VAULT_OPTIONS)
128
+ @context.options.declare(:vault, schema: Schema::Registry::VAULT_OPTIONS, convert: VaultManager.method(:convert_option))
126
129
  @context.options.declare(:vault_password, description: 'Vault password')
130
+ @context.vault = VaultManager.new(@context.options, @context.main_folder)
127
131
  # Register @preset and @vault handlers BEFORE parse_options! so that
128
132
  # values like --secret=@preset:name are correctly evaluated at parse time.
129
- ExtendedValue.instance.on(EXTEND_PRESET) { |v| @context.presets.by_name(v) }
130
- ExtendedValue.instance.on(EXTEND_VAULT, &vault_value_cb)
133
+ ExtendedValue.instance.on(EXTEND_PRESET, description: 'Option preset, or its parameter: <name>[.<parameter>]') { |v| @context.presets.by_name(v) }
134
+ ExtendedValue.instance.on(EXTEND_VAULT, description: 'Secret from vault: <label>.<field> of the default store, or //<store>/<path>[#<field>]') { |v| @context.vault.value(v) }
131
135
  @context.options.parse_options!
132
136
  # Load global config default preset (equivalent of add_plugin_default_preset(:config))
133
137
  default_config_name = @context.presets.plugin_default_name(CONF_GLOBAL_SYM)
@@ -148,7 +152,7 @@ module Aspera
148
152
 
149
153
  # Declare + parse :fpac / :proxy_credentials: sets up PAC executor
150
154
  def setup_pac_executor
151
- @context.options.declare(:fpac, description: 'Proxy auto configuration script')
155
+ @context.options.declare(:fpac, description: 'Proxy auto configuration script', security: :low)
152
156
  @context.options.declare(:proxy_credentials, description: 'HTTP proxy credentials for fpac: user, password', allowed: [Array, NilClass])
153
157
  @context.options.parse_options!
154
158
  pac_script = @context.options.get_option(:fpac)
@@ -172,6 +176,7 @@ module Aspera
172
176
  Rest::Parameters.instance.session_cb = ->(http_session) { @context.http_config.update_session(http_session) }
173
177
  Rest::Parameters.instance.spinner_cb = ->(title = nil, action: :spin) { @context.formatter.long_operation(title, action: action) }
174
178
  OAuth::Web.additional_info = "#{Info::CMD_NAME} v#{Cli::VERSION}"
179
+ Ascp::Installation.instance.referer_url = "#{Info::GEM_URL}/versions/#{Cli::VERSION}"
175
180
  Transfer::Parameters.file_list_folder = File.join(@context.main_folder, FILE_LIST_FOLDER_NAME)
176
181
  Rest::ErrorAnalyzer.instance.log_file = File.join(@context.main_folder, REST_EXCEPTIONS_LOG_FILENAME)
177
182
  end
@@ -1,9 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/cli/error'
4
- require 'aspera/dot_container'
5
- require 'aspera/log'
6
- require 'aspera/assert'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'error'
7
+ require_relative '../dot_container'
8
+ require_relative '../log'
9
+ require_relative '../assert'
10
+ require_relative '../secret/hider'
11
+ require 'json'
12
+ require 'yaml'
7
13
 
8
14
  module Aspera
9
15
  module Cli
@@ -65,8 +71,11 @@ module Aspera
65
71
  # @return [String, nil] inline value, or value of next token
66
72
  def value = @inline_value || @value_token&.value
67
73
 
68
- # @return [String] raw token, followed by its separate value if any
69
- def to_s = @value_token.nil? ? @raw : "#{@raw} #{@value_token.value}"
74
+ # @return [String] raw token, with declared secret value hidden (logs, messages)
75
+ def raw_hidden = inline? ? "#{@raw.delete_suffix(@inline_value)}#{Secret::Hider.instance.hidden_copy(@inline_value)}" : @raw
76
+
77
+ # @return [String] raw token, followed by its separate value if any, with declared secret value hidden (logs, messages)
78
+ def to_s = @value_token.nil? ? raw_hidden : "#{@raw} #{Secret::Hider.instance.hidden_copy(@value_token.value)}"
70
79
 
71
80
  class << self
72
81
  # @param token [String] command line token
@@ -108,6 +117,13 @@ module Aspera
108
117
  # unless that token looks like an option.
109
118
  # If the option turns out to be a flag, the claimed token is given back to positional arguments.
110
119
  class CommandLine
120
+ # Decoders of extended values whose content is checked for secrets by key name
121
+ STRUCTURED_DECODERS = {
122
+ '@json:' => ->(v) { JSON.parse(v) },
123
+ '@yaml:' => ->(v) { YAML.safe_load(v) }
124
+ }.freeze
125
+ private_constant :STRUCTURED_DECODERS
126
+
111
127
  # @param argv [Array<String>] command line arguments
112
128
  def initialize(argv)
113
129
  # @type [Array<Option, Argument>]
@@ -116,6 +132,10 @@ module Aspera
116
132
  @current_option = nil
117
133
  # When set, only positional arguments after this token are available
118
134
  @arguments_after = nil
135
+ # Last positional token consumed by `shift_arguments`
136
+ @last_shifted = nil
137
+ # Token of the last command word (set by `mark_command`)
138
+ @command_token = nil
119
139
  process_options = true
120
140
  argv.each do |value|
121
141
  if process_options && value.eql?(Option::STOP)
@@ -132,6 +152,8 @@ module Aspera
132
152
  @tokens.push(argument)
133
153
  end
134
154
  end
155
+ declare_secrets
156
+ Log.dump(:argv, level: :trace2) { @tokens.map { |t| t.is_a?(Option) ? t.raw_hidden : Secret::Hider.instance.hidden_copy(t.value) } }
135
157
  Log.log.trace1 { "arguments=#{pending_arguments},options=#{pending_options}" }
136
158
  end
137
159
 
@@ -205,9 +227,18 @@ module Aspera
205
227
  else Aspera.error_unexpected_value(multiple) { 'multiple' }
206
228
  end
207
229
  selected.each { |t| t.consumed = true }
230
+ @last_shifted = selected.last unless selected.empty?
208
231
  selected.map(&:value)
209
232
  end
210
233
 
234
+ # Remember the last consumed positional token as the current command word.
235
+ # Options placed after it can then be selected with `each_long_option_with_value(after_command: true)`.
236
+ # @return [nil]
237
+ def mark_command
238
+ @command_token = @last_shifted
239
+ nil
240
+ end
241
+
211
242
  # Add an argument before the available positional arguments
212
243
  # @param value [String] argument value
213
244
  def unshift_argument(value)
@@ -216,9 +247,11 @@ module Aspera
216
247
  end
217
248
 
218
249
  # Consume all long options with a value, whether applied or not.
250
+ # @param after_command [Boolean] `true`: only options placed after the last command word (see `mark_command`)
219
251
  # @yieldparam tok [Option] long option token with a value
220
- def each_long_option_with_value
221
- @tokens.each do |tok|
252
+ def each_long_option_with_value(after_command: false)
253
+ start = after_command && @command_token ? @tokens.index { |t| t.equal?(@command_token) } + 1 : 0
254
+ @tokens.drop(start).each do |tok|
222
255
  next unless tok.is_a?(Option) && tok.short_char.nil? && !tok.value.nil?
223
256
  yield(tok)
224
257
  tok.consumed = true
@@ -228,6 +261,28 @@ module Aspera
228
261
 
229
262
  private
230
263
 
264
+ # Declare secret values of long options, by option name (last key for dot notation), as those of presets and env vars,
265
+ # so that they are hidden wherever they appear (logs, results), before the option is declared.
266
+ # A structured value (`@json:`, `@yaml:`) that contains a secret is declared as a whole, with its content.
267
+ def declare_secrets
268
+ hider = Secret::Hider.instance
269
+ @tokens.each do |tok|
270
+ next unless tok.is_a?(Option) && tok.short_char.nil? && tok.value.is_a?(String)
271
+ hider.add_named_secrets((tok.dot_path || [tok.name]).last, tok.value)
272
+ prefix, decoder = STRUCTURED_DECODERS.find { |p, _| tok.value.start_with?(p) }
273
+ next if prefix.nil?
274
+ content = tok.value.delete_prefix(prefix)
275
+ decoded = begin
276
+ decoder.call(content)
277
+ rescue StandardError
278
+ next
279
+ end
280
+ next unless hider.contains_secret?(decoded)
281
+ hider.add_secrets_in(decoded)
282
+ hider.add_secret_values(tok.value, content)
283
+ end
284
+ end
285
+
231
286
  # Give back claimed token to positional arguments.
232
287
  # @param tok [Option] flag option token
233
288
  # @return [nil]
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/cli/command_spec'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative 'command_spec'
7
+ require_relative 'option_types'
4
8
 
5
9
  module Aspera
6
10
  module Cli
@@ -258,6 +262,7 @@ module Aspera
258
262
  raise ArgumentError, "#{path.inspect}: mount arguments must be mandatory" unless mount.arguments.all?(&:mandatory)
259
263
  validate_arguments(path, mount.arguments, plugin_class)
260
264
  raise ArgumentError, "#{path.inspect}: mount and action: are exclusive" if spec.action
265
+ raise ArgumentError, "#{path.inspect}: mount and read_only: are exclusive" if spec.read_only.eql?(true)
261
266
  raise ArgumentError, "#{path.inspect}: mount at #{mount.at.inspect} not found in #{mount.plugin}" unless mount.at.empty? || mount.registry[mount.at]
262
267
  target_ids = mount.registry.children_of(mount.at).keys
263
268
  unknown = Array(mount.only) + Array(mount.except) - target_ids
@@ -267,6 +272,8 @@ module Aspera
267
272
  next
268
273
  end
269
274
 
275
+ # Rule: only a leaf is executed, so only a leaf is read-only
276
+ raise ArgumentError, "#{path.inspect}: read_only: only on a leaf" if spec.read_only.eql?(true) && @children_index[path]&.any?
270
277
  next if @children_index[path]&.any? # intermediate node: skip
271
278
  action = spec.action
272
279
  if action.nil?
@@ -322,6 +329,7 @@ module Aspera
322
329
  # Rule: an argument following an optional one, or one that takes all remaining arguments, is never read reliably
323
330
  raise ArgumentError, "#{where}: mandatory after optional #{previous.name}" if previous && arg.mandatory && !previous.mandatory
324
331
  raise ArgumentError, "#{where}: after #{previous.name}, which takes all remaining arguments" if previous&.multiple.eql?(true)
332
+ raise ArgumentError, "#{where}: complete: #{arg.complete.inspect} is not one of #{Complete::KINDS.inspect}" unless arg.complete.nil? || Complete::KINDS.include?(arg.complete)
325
333
  unless arg.lookup.nil?
326
334
  # Rule: the percent-selector lookup is only used for identifiers
327
335
  raise ArgumentError, "#{where}: lookup: requires type: :identifier" unless arg.type.eql?(:identifier)
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aspera/schema/registry'
4
- require 'aspera/cli/option_types'
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative '../schema/registry'
7
+ require_relative 'option_types'
8
+ require_relative 'security'
5
9
 
6
10
  module Aspera
7
11
  module Cli
@@ -24,7 +28,16 @@ module Aspera
24
28
  # Style: use Symbol for named methods; ->(){} for 1-liners; lambda do…end for 2–3 statements.
25
29
  # @!attribute allowed [Array<Symbol>, nil] Allowed Symbol values; when set, type is forced to Symbol and accept_list is applied
26
30
  # @!attribute interactive [Boolean] When true, prompts for this argument (only) when no CLI args are provided
27
- # @!attribute raw [Boolean] When true, the value is not evaluated as extended value (e.g. `@env:`)
31
+ # @!attribute evaluate [Symbol] How the value is read, one of `EVALUATIONS`. Default `:extended`.
32
+ # `:extended`: extended value (`@json:`, `@file:`, `@val:`...); a value without prefix stays a string.
33
+ # `:smart`: `true`/`yes`/`false`/`no` and numbers are converted, else extended value (as dotted options).
34
+ # `:none`: the string as typed, not evaluated (e.g. `@env:`).
35
+ # @!attribute complete [Symbol, nil] Values proposed by shell completion, one of `Complete::KINDS` (e.g. `:file`)
36
+ # @!attribute omit_absent [Boolean] For an optional argument: when not given, it is not passed to the action,
37
+ # so that the action (with its own default) can tell it from a value `nil` (`@none:`)
38
+ # Ways to evaluate the value of an argument, see `ArgumentSpec#evaluate`
39
+ EVALUATIONS = %i[extended smart none].freeze
40
+
28
41
  ArgumentSpec = Struct.new(
29
42
  :name,
30
43
  :description,
@@ -37,7 +50,9 @@ module Aspera
37
50
  :lookup,
38
51
  :allowed,
39
52
  :interactive,
40
- :raw,
53
+ :evaluate,
54
+ :complete,
55
+ :omit_absent,
41
56
  keyword_init: true
42
57
  ) do
43
58
  def initialize(**kwargs)
@@ -45,8 +60,11 @@ module Aspera
45
60
  kwargs[:multiple] = false if kwargs[:multiple].nil?
46
61
  kwargs[:bulk] = false if kwargs[:bulk].nil?
47
62
  kwargs[:interactive] = false if kwargs[:interactive].nil?
48
- kwargs[:raw] = false if kwargs[:raw].nil?
49
- kwargs[:type] = String unless kwargs.key?(:type) || kwargs[:allowed]
63
+ kwargs[:evaluate] = :extended if kwargs[:evaluate].nil?
64
+ kwargs[:omit_absent] = false if kwargs[:omit_absent].nil?
65
+ raise ArgumentError, "omit_absent: only for an optional argument: #{kwargs[:name]}" if kwargs[:omit_absent] && kwargs[:mandatory]
66
+ kwargs[:type] = String unless kwargs.key?(:type) || kwargs[:allowed]
67
+ raise ArgumentError, "evaluate: #{kwargs[:evaluate].inspect}, expecting one of: #{EVALUATIONS.join(', ')}" unless EVALUATIONS.include?(kwargs[:evaluate])
50
68
  super
51
69
  end
52
70
 
@@ -80,6 +98,8 @@ module Aspera
80
98
  # @!attribute shorthand [String, nil] For a `Hash` option: a `String` value is stored as `{shorthand => value}`
81
99
  # @!attribute deprecation [Hash, nil] Forwarded to options.declare as deprecation: `{last:, message:}`
82
100
  # @!attribute schema [String, nil] JSON schema name; also derives description when nil
101
+ # @!attribute complete [Symbol, nil] Values proposed by shell completion, one of `Complete::KINDS` (e.g. `:file`)
102
+ # @!attribute security [Symbol, nil] Most restrictive security level at which the option is available (`:low`: executes code), see `Security`
83
103
  OptionSpec = Struct.new(
84
104
  :name,
85
105
  :description,
@@ -90,6 +110,8 @@ module Aspera
90
110
  :shorthand,
91
111
  :deprecation,
92
112
  :schema,
113
+ :complete,
114
+ :security,
93
115
  keyword_init: true
94
116
  ) do
95
117
  # Declare this option on a parser, resolving the `on_set` callback.
@@ -106,7 +128,9 @@ module Aspera
106
128
  on_set: resolved_on_set(target),
107
129
  shorthand: shorthand,
108
130
  deprecation: deprecation,
109
- schema: schema
131
+ schema: schema,
132
+ complete: complete,
133
+ security: security
110
134
  )
111
135
  end
112
136
 
@@ -167,6 +191,9 @@ module Aspera
167
191
  end
168
192
  end
169
193
 
194
+ # Ids of the commands read-only by default (`read_only: :default`): they change nothing
195
+ READ_ONLY_IDS = %i[list show status info health ls browse find].freeze
196
+
170
197
  # Declares a single command node in the flat registry.
171
198
  #
172
199
  # @!attribute id [Symbol] Unique identifier within its parent's namespace
@@ -182,6 +209,9 @@ module Aspera
182
209
  # @!attribute condition [Symbol, nil] Instance method returning Boolean; if false command is hidden from dispatch
183
210
  # @!attribute query_schema [String, nil] Schema path for --query help; when set, the runner hints `--query=help`
184
211
  # @!attribute mount [MountSpec, Hash, nil] Expose children of another plugin's registry under this node
212
+ # @!attribute security [Symbol, nil] Most restrictive security level at which the command is available (`:low`: executes code), see `Security`
213
+ # @!attribute read_only [Boolean, :default] Leaf only: the command changes nothing (on the server or locally), so it can be repeated (option `poll`).
214
+ # `:default` (default): read-only if the id is one of READ_ONLY_IDS (`list`, `show`, ...)
185
215
  CommandSpec = Struct.new(
186
216
  :id,
187
217
  :parent,
@@ -194,9 +224,14 @@ module Aspera
194
224
  :condition,
195
225
  :query_schema,
196
226
  :mount,
227
+ :security,
228
+ :read_only,
197
229
  keyword_init: true
198
230
  ) do
199
231
  def initialize(**kwargs)
232
+ Aspera.assert_values(kwargs[:security], Security::LEVELS + [nil]) { "#{kwargs[:id]}: security" }
233
+ kwargs[:read_only] = :default if kwargs[:read_only].nil?
234
+ Aspera.assert_values(kwargs[:read_only], [true, false, :default]) { "#{kwargs[:id]}: read_only" }
200
235
  # Coerce each element of arguments: from Hash to ArgumentSpec if needed
201
236
  if kwargs[:arguments]
202
237
  kwargs[:arguments] = kwargs[:arguments].map do |a|
@@ -217,6 +252,11 @@ module Aspera
217
252
  end
218
253
  end
219
254
 
255
+ # @return [Boolean] `true` if the command changes nothing: explicit `read_only`, or from its id (READ_ONLY_IDS)
256
+ def read_only?
257
+ read_only.eql?(:default) ? READ_ONLY_IDS.include?(id) : read_only
258
+ end
259
+
220
260
  # Compute the full path as Array<Symbol> from parent + id.
221
261
  # @return [Array<Symbol>]
222
262
  def full_path
@@ -18,10 +18,13 @@ _ascli_complete() {
18
18
  rest=${rest#"$word"}
19
19
  done
20
20
  local cur=${words[${#words[@]} - 1]}
21
- # Readline replaces only the text after the last word break character
21
+ # Readline replaces only the text after the last word break character, but with it if it is `@` or `$` (special prefixes)
22
22
  local prefix=${cur%"${line##*["$COMP_WORDBREAKS"]}"}
23
+ [[ $prefix == *[@\$] ]] && prefix=${prefix%?}
23
24
  # words[0] is the program name itself: pass the words before the cursor, then the word at cursor
24
- local output directive entry
25
+ local output directive entry files_prefix
26
+ # A word that continues is not followed by a space: `key=`, `key.`, `folder/`, `@modifier:`
27
+ local continues='([=./]|@[a-z0-9_]+:)$'
25
28
  output=$(ascli config completion candidates -- "${words[@]:1:${#words[@]}-2}" "$cur" 2>/dev/null)
26
29
  directive=${output##*$'\n'}
27
30
  output=${output%"$directive"}
@@ -29,12 +32,23 @@ _ascli_complete() {
29
32
  while IFS= read -r entry; do
30
33
  # Remove the description
31
34
  word=${entry%%$'\t'*}
32
- [[ -n $word && $word == "$cur"* ]] && COMPREPLY+=("${word#"$prefix"}")
35
+ [[ -n $word && $word == "$cur"* ]] || continue
36
+ COMPREPLY+=("${word#"$prefix"}")
37
+ [[ $word =~ $continues ]] && compopt -o nospace 2>/dev/null
33
38
  done <<<"$output"
34
39
  case $directive in
35
- :files)
40
+ :files | ":files "* | :folders | ":folders "*)
41
+ # Beginning of the word that is not part of the path, e.g. `--key=@file:`
42
+ files_prefix=${directive#:files}
43
+ files_prefix=${files_prefix#:folders}
44
+ files_prefix=${files_prefix# }
45
+ local kind=file
46
+ [[ $directive == :folders* ]] && kind=directory
36
47
  compopt -o filenames 2>/dev/null
37
- while IFS= read -r word; do COMPREPLY+=("$word"); done < <(compgen -f -- "${cur#"$prefix"}")
48
+ while IFS= read -r word; do
49
+ word=$files_prefix$word
50
+ COMPREPLY+=("${word#"$prefix"}")
51
+ done < <(compgen -A "$kind" -- "${cur#"$files_prefix"}")
38
52
  ;;
39
53
  ":hint "*)
40
54
  # Two entries: bash displays them, but inserts nothing
@@ -6,16 +6,27 @@
6
6
  complete --command ascli --erase
7
7
 
8
8
  # Candidates proposed by ascli, as lines `word<TAB>description`, as expected by fish
9
+ # (fish adds no space after a word ending with `=`, `.`, `/` or `:`)
9
10
  function __ascli_complete
10
11
  # Words before the cursor (ascli itself excluded), then the word at cursor
11
12
  set -l words (commandline -opc)
12
13
  set -l current (commandline -ct)
13
14
  set -l lines (ascli config completion candidates -- $words[2..] "$current" 2>/dev/null)
14
15
  set -q lines[1]; or return
15
- # Last line is the directive: `:files`, `:hint <text>` (cannot be displayed without being inserted), or `:`
16
+ # Last line is the directive: `:files` or `:folders` (followed by the beginning of the word that is not part of the path, if any),
17
+ # `:hint <text>` (cannot be displayed without being inserted), or `:`
16
18
  set -l directive $lines[-1]
17
19
  set -e lines[-1]
18
- test "$directive" = :files; and __fish_complete_path "$current"
20
+ if string match -qr '^:(files|folders)( |$)' -- $directive
21
+ set -l files_prefix (string replace -r '^:(files|folders) ?' '' -- $directive)
22
+ set -q files_prefix[1]; or set files_prefix ''
23
+ set -l path (string sub --start (math (string length -- "$files_prefix") + 1) -- $current)
24
+ if string match -q ':folders*' -- $directive
25
+ printf '%s\n' $files_prefix(__fish_complete_directories "$path")
26
+ else
27
+ printf '%s\n' $files_prefix(__fish_complete_path "$path")
28
+ end
29
+ end
19
30
  # printf without argument would output an empty line
20
31
  set -q lines[1]; and printf '%s\n' $lines
21
32
  end
@@ -14,28 +14,50 @@
14
14
  $words = @($commandAst.CommandElements | Select-Object -Skip 1 |
15
15
  Where-Object { $_.Extent.EndOffset -lt $cursorPosition } |
16
16
  ForEach-Object { if ($_ -is [System.Management.Automation.Language.StringConstantExpressionAst]) { $_.Value } else { $_.Extent.Text } })
17
- # Only the beginning of the word at cursor matters: `-` (option names), `--name.` (keys) or `--name=` (values), candidates are filtered here.
18
- # Other words give the same candidates: `.` is passed, as Windows PowerShell does not pass an empty argument.
19
- $current = if ($wordToComplete -match '^--[^=]*[=.]') { $Matches[0] } elseif ($wordToComplete.StartsWith('-')) { '-' } else { '.' }
17
+ # Word at cursor, unquoted: a word starting with `@` is quoted (else it is splatting)
18
+ $word = $wordToComplete -replace '^[''"]|[''"]$', ''
19
+ # Only the beginning of the word at cursor matters: up to its last `=`, `.`, `:` or `@` (values, keys, extended values),
20
+ # or `-` (option names). Candidates are filtered here.
21
+ # Other words give the same candidates: `_` is passed, as Windows PowerShell does not pass an empty argument.
22
+ $current = if ($word -match '^.*[=.:@]') { $Matches[0] } elseif ($word.StartsWith('-')) { '-' } else { '_' }
20
23
  $key = (@($command) + $words + $current) -join ' '
21
24
  if (-not $cache.ContainsKey($key)) {
22
25
  $cache[$key] = @(& $command config completion candidates -- @words $current 2>$null)
23
26
  }
24
27
  $lines = $cache[$key]
25
- # Directive `:files`: no result, so that PowerShell completes file paths
26
- if ($lines.Count -eq 0 -or $lines[-1] -eq ':files') { return }
28
+ if ($lines.Count -eq 0) { return }
29
+ $directive = $lines[-1]
30
+ # Text inserted: quoted if it starts with `@`, or contains a space or a special character
31
+ $quote = { param($text) if ($text -match '^@|[\s''"`$;,(){}|&<>]') { "'" + ($text -replace "'", "''") + "'" } else { $text } }
32
+ # Directive `:files` or `:folders`, followed by the beginning of the word that is not part of the path, if any
33
+ if ($directive -match '^:(files|folders)(?: (.*))?$') {
34
+ $folders = $Matches[1] -eq 'folders'
35
+ $prefix = [string]$Matches[2]
36
+ # No prefix: no result, so that PowerShell completes file paths
37
+ if (-not $prefix -or -not $word.StartsWith($prefix)) { return }
38
+ $path = $word.Substring($prefix.Length)
39
+ $parent = $path.Substring(0, $path.LastIndexOfAny([char[]]'/\') + 1)
40
+ $leaf = $path.Substring($parent.Length)
41
+ $folder = if ($parent) { [WildcardPattern]::Escape($parent) } else { '.' }
42
+ return @(Get-ChildItem -Path $folder -Force -ErrorAction SilentlyContinue |
43
+ Where-Object { $_.Name.StartsWith($leaf, [System.StringComparison]::OrdinalIgnoreCase) -and ($_.PSIsContainer -or -not $folders) } |
44
+ ForEach-Object {
45
+ $text = $prefix + $parent + $_.Name + $(if ($_.PSIsContainer) { [System.IO.Path]::DirectorySeparatorChar } else { '' })
46
+ [System.Management.Automation.CompletionResult]::new((& $quote $text), $_.Name, 'ParameterValue', $text)
47
+ })
48
+ }
27
49
  # Lines `word<TAB>description`, then the directive
28
50
  $results = @($lines | Select-Object -First ($lines.Count - 1) | ForEach-Object {
29
- $word, $description = $_ -split "`t", 2
30
- if ($word -and $word.StartsWith($wordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {
31
- [System.Management.Automation.CompletionResult]::new($word, $word, 'ParameterValue', $(if ($description) { $description } else { $word }))
51
+ $candidate, $description = $_ -split "`t", 2
52
+ if ($candidate -and $candidate.StartsWith($word, [System.StringComparison]::OrdinalIgnoreCase)) {
53
+ [System.Management.Automation.CompletionResult]::new((& $quote $candidate), $candidate, 'ParameterValue', $(if ($description) { $description } else { $candidate }))
32
54
  }
33
55
  })
34
56
  if ($results.Count -gt 0) { return $results }
35
57
  # No result: one that does not change the word (a space for an empty word), so that PowerShell does not complete file paths.
36
58
  # It shows the hint in the list, if any (e.g. `<String>`)
37
59
  $text = if ($wordToComplete) { $wordToComplete } else { ' ' }
38
- $hint = if ($lines[-1] -like ':hint *') { $lines[-1].Substring(6) } else { $text }
60
+ $hint = if ($directive -like ':hint *') { $directive.Substring(6) } else { $text }
39
61
  [System.Management.Automation.CompletionResult]::new($text, $hint, 'ParameterValue', $hint)
40
62
  }.GetNewClosure()
41
63
  }
@@ -4,24 +4,40 @@
4
4
  # Or save as file `_ascli` in a folder of $fpath
5
5
 
6
6
  _ascli() {
7
- local -a lines values opts
8
- local directive line entry
7
+ local -a lines values values_cont opts opts_cont
8
+ local directive line word entry files_prefix
9
9
  # words[1] is 'ascli' itself: pass the words before the cursor, then the word at cursor
10
10
  lines=("${(@f)$(ascli config completion candidates -- "${(@)words[2,CURRENT-1]}" "$PREFIX" 2>/dev/null)}")
11
11
  directive=${lines[-1]}
12
12
  # Candidates `word<TAB>description` to `word:description`, as expected by _describe
13
13
  for line in "${(@)lines[1,-2]}"; do
14
14
  [[ -n $line ]] || continue
15
- entry=${${line%%$'\t'*}//:/\\:}
15
+ word=${line%%$'\t'*}
16
+ entry=${word//:/\\:}
16
17
  [[ $line == *$'\t'* ]] && entry+=":${line#*$'\t'}"
17
- if [[ $line == -* ]]; then opts+=("$entry"); else values+=("$entry"); fi
18
+ # Option names (`--name`, `--name.key=`), else values (`--name=value`)
19
+ # A word that continues is not followed by a space: `key=`, `key.`, `folder/`, `@modifier:`
20
+ if [[ $word =~ '^-[^=]*=?$' ]]; then
21
+ if [[ $word =~ '[=.]$' ]]; then opts_cont+=("$entry"); else opts+=("$entry"); fi
22
+ elif [[ $word =~ '([=./]|@[a-z0-9_]+:)$' ]]; then
23
+ values_cont+=("$entry")
24
+ else
25
+ values+=("$entry")
26
+ fi
18
27
  done
19
28
  case $directive in
20
- :files) _files ;;
29
+ :files | ':files '* | :folders | ':folders '*)
30
+ # Beginning of the word that is not part of the path, e.g. `--key=@file:`
31
+ files_prefix=${${${directive#:files}#:folders}# }
32
+ [[ -n $files_prefix ]] && compset -P "${(b)files_prefix}"
33
+ if [[ $directive == :folders* ]]; then _files -/; else _files; fi
34
+ ;;
21
35
  ":hint "*) _message -r "${directive#:hint }" ;;
22
36
  *)
23
37
  # Options are listed after the other candidates
24
- _alternative 'values:value: _describe -t values value values' 'options:option: _describe -t options option opts'
38
+ _alternative \
39
+ 'values:value: _describe -t values value values -- values_cont -S ""' \
40
+ 'options:option: _describe -t options option opts -- opts_cont -S ""'
25
41
  ;;
26
42
  esac
27
43
  }