aspera-cli 4.27.5 → 4.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/BUGS.md +5 -10
- data/CHANGELOG.md +301 -4
- data/CODE_OF_CONDUCT.md +18 -0
- data/CONTRIBUTING.md +139 -58
- data/GOVERNANCE.md +30 -0
- data/LICENSE +201 -0
- data/README.md +103 -26
- data/SECURITY.md +70 -0
- data/bin/ascli +8 -5
- data/bin/asession +10 -7
- data/docs/README.md +2551 -772
- data/docs/test-mcp-with-ai.md +13 -11
- data/lib/aspera/agent/base.rb +23 -2
- data/lib/aspera/agent/connect.rb +16 -12
- data/lib/aspera/agent/desktop.rb +10 -7
- data/lib/aspera/agent/direct.rb +34 -22
- data/lib/aspera/agent/factory.rb +8 -4
- data/lib/aspera/agent/httpgw.rb +7 -5
- data/lib/aspera/agent/node.rb +15 -7
- data/lib/aspera/agent/transferd.rb +167 -43
- data/lib/aspera/api/alee.rb +4 -1
- data/lib/aspera/api/aoc.rb +14 -11
- data/lib/aspera/api/ats.rb +4 -2
- data/lib/aspera/api/cos_node.rb +8 -5
- data/lib/aspera/api/faspex.rb +16 -9
- data/lib/aspera/api/httpgw.rb +9 -6
- data/lib/aspera/api/node.rb +23 -19
- data/lib/aspera/ascmd.rb +8 -5
- data/lib/aspera/ascp/installation.rb +84 -42
- data/lib/aspera/ascp/log.schema.yaml +1084 -0
- data/lib/aspera/ascp/log_analyzer/advice.rb +97 -0
- data/lib/aspera/ascp/log_analyzer/diagnostics.rb +344 -0
- data/lib/aspera/ascp/log_analyzer/lines.rb +75 -0
- data/lib/aspera/ascp/log_analyzer/log_file.rb +593 -0
- data/lib/aspera/ascp/log_analyzer/records.rb +379 -0
- data/lib/aspera/ascp/log_analyzer/rules.rb +172 -0
- data/lib/aspera/ascp/log_analyzer/session.rb +782 -0
- data/lib/aspera/ascp/log_analyzer.rb +354 -0
- data/lib/aspera/ascp/management.rb +102 -48
- data/lib/aspera/ascp/session_errors.rb +217 -0
- data/lib/aspera/assert.rb +4 -0
- data/lib/aspera/cli/ascp_actions.rb +16 -37
- data/lib/aspera/cli/ascp_logs_actions.rb +176 -0
- data/lib/aspera/cli/async_transfer_store.rb +7 -4
- data/lib/aspera/cli/bootstrapper.rb +37 -34
- data/lib/aspera/cli/command_line.rb +24 -6
- data/lib/aspera/cli/command_registry.rb +49 -4
- data/lib/aspera/cli/command_spec.rb +34 -4
- data/lib/aspera/cli/completion/ascli.bash +53 -5
- data/lib/aspera/cli/completion/ascli.fish +27 -8
- data/lib/aspera/cli/completion/ascli.ps1 +63 -0
- data/lib/aspera/cli/completion/ascli.zsh +36 -4
- data/lib/aspera/cli/completion_actions.rb +594 -0
- data/lib/aspera/cli/context.rb +9 -4
- data/lib/aspera/cli/deprecation.rb +5 -2
- data/lib/aspera/cli/error.rb +45 -0
- data/lib/aspera/cli/extended_value.rb +83 -24
- data/lib/aspera/cli/formatter.rb +142 -73
- data/lib/aspera/cli/gem_checker.rb +9 -6
- data/lib/aspera/cli/hints.rb +22 -11
- data/lib/aspera/cli/http.rb +17 -13
- data/lib/aspera/cli/info.rb +17 -6
- data/lib/aspera/cli/mailer.rb +7 -4
- data/lib/aspera/cli/mcp/http_servlet.rb +155 -0
- data/lib/aspera/cli/mcp/secrets.rb +146 -0
- data/lib/aspera/cli/mcp/tool.rb +232 -0
- data/lib/aspera/cli/option_declarator.rb +10 -4
- data/lib/aspera/cli/option_registry.rb +6 -3
- data/lib/aspera/cli/option_types.rb +17 -2
- data/lib/aspera/cli/option_value.rb +72 -11
- data/lib/aspera/cli/options.schema.yaml +856 -81
- data/lib/aspera/cli/parser.rb +64 -31
- data/lib/aspera/cli/plugins/alee.rb +7 -4
- data/lib/aspera/cli/plugins/aoc.rb +250 -44
- data/lib/aspera/cli/plugins/ats.rb +9 -8
- data/lib/aspera/cli/plugins/base.rb +51 -25
- data/lib/aspera/cli/plugins/basic_auth.rb +5 -2
- data/lib/aspera/cli/plugins/config.rb +207 -141
- data/lib/aspera/cli/plugins/console.rb +9 -4
- data/lib/aspera/cli/plugins/cos.rb +8 -4
- data/lib/aspera/cli/plugins/factory.rb +22 -8
- data/lib/aspera/cli/plugins/faspex5.rb +37 -44
- data/lib/aspera/cli/plugins/faspio.rb +41 -8
- data/lib/aspera/cli/plugins/httpgw.rb +8 -4
- data/lib/aspera/cli/plugins/mcp.rb +186 -121
- data/lib/aspera/cli/plugins/node.rb +41 -34
- data/lib/aspera/cli/plugins/oauth.rb +5 -1
- data/lib/aspera/cli/plugins/orchestrator.rb +572 -59
- data/lib/aspera/cli/plugins/preview.rb +69 -44
- data/lib/aspera/cli/plugins/server.rb +66 -12
- data/lib/aspera/cli/plugins/shares.rb +288 -48
- data/lib/aspera/cli/plugins/transferd.rb +223 -0
- data/lib/aspera/cli/preset_actions.rb +155 -33
- data/lib/aspera/cli/preset_manager.rb +72 -16
- data/lib/aspera/cli/prompt.rb +7 -2
- data/lib/aspera/cli/result.rb +28 -11
- data/lib/aspera/cli/runner.rb +193 -80
- data/lib/aspera/cli/secret_finder.rb +4 -1
- data/lib/aspera/cli/security.rb +204 -0
- data/lib/aspera/cli/special_values.rb +4 -1
- data/lib/aspera/cli/sync_actions.rb +13 -10
- data/lib/aspera/cli/table_fitter.rb +119 -0
- data/lib/aspera/cli/terminal_formatter.rb +32 -40
- data/lib/aspera/cli/transfer_actions.rb +11 -8
- data/lib/aspera/cli/transfer_agent.rb +31 -19
- data/lib/aspera/cli/transfer_progress.rb +13 -45
- data/lib/aspera/cli/vault_actions.rb +45 -0
- data/lib/aspera/cli/vault_manager.rb +197 -50
- data/lib/aspera/cli/version.rb +4 -1
- data/lib/aspera/cli/web_secret.html.erb +61 -0
- data/lib/aspera/cli/web_secret.rb +222 -0
- data/lib/aspera/cli/wizard.rb +58 -45
- data/lib/aspera/command_line_builder.rb +35 -5
- data/lib/aspera/command_line_converter.rb +4 -1
- data/lib/aspera/coverage.rb +103 -33
- data/lib/aspera/data_repository.rb +42 -25
- data/lib/aspera/dot_container.rb +4 -1
- data/lib/aspera/duration.rb +45 -0
- data/lib/aspera/environment.rb +106 -33
- data/lib/aspera/exec_spec.rb +9 -5
- data/lib/aspera/faspex_gw.rb +105 -35
- data/lib/aspera/faspex_postproc.rb +193 -47
- data/lib/aspera/formatter_interface.rb +4 -2
- data/lib/aspera/graphql.rb +5 -1
- data/lib/aspera/hash_ext.rb +3 -0
- data/lib/aspera/id_generator.rb +6 -3
- data/lib/aspera/json_rpc/client.rb +55 -35
- data/lib/aspera/json_rpc/version.rb +4 -0
- data/lib/aspera/line_logger.rb +4 -1
- data/lib/aspera/link_header.rb +4 -1
- data/lib/aspera/log.rb +20 -10
- data/lib/aspera/markdown.rb +155 -115
- data/lib/aspera/mime.rb +9 -0
- data/lib/aspera/nagios.rb +9 -5
- data/lib/aspera/node_emulator.rb +111 -112
- data/lib/aspera/oauth/client/base.rb +163 -0
- data/lib/aspera/oauth/{boot.rb → client/boot.rb} +8 -4
- data/lib/aspera/oauth/{factory.rb → client/factory.rb} +13 -3
- data/lib/aspera/oauth/{generic.rb → client/generic.rb} +5 -1
- data/lib/aspera/oauth/{json_credentials.rb → client/json_credentials.rb} +4 -1
- data/lib/aspera/oauth/{jwt.rb → client/jwt.rb} +6 -3
- data/lib/aspera/oauth/client/shares_web.rb +85 -0
- data/lib/aspera/oauth/{url_json.rb → client/url_json.rb} +4 -1
- data/lib/aspera/oauth/client/web.rb +84 -0
- data/lib/aspera/oauth/server/authorize.html.erb +34 -0
- data/lib/aspera/oauth/server/pkce.rb +427 -0
- data/lib/aspera/orchestrator_workflow_checker.rb +679 -0
- data/lib/aspera/persistency_action_once.rb +5 -3
- data/lib/aspera/persistency_folder.rb +6 -3
- data/lib/aspera/preview/file_types.rb +6 -3
- data/lib/aspera/preview/generator.rb +9 -5
- data/lib/aspera/preview/options.schema.yaml +3 -3
- data/lib/aspera/preview/terminal.rb +11 -6
- data/lib/aspera/preview/utils.rb +6 -4
- data/lib/aspera/products/connect.rb +5 -2
- data/lib/aspera/products/desktop.rb +4 -1
- data/lib/aspera/products/other.rb +4 -2
- data/lib/aspera/products/transferd.rb +6 -2
- data/lib/aspera/products/transferd_service.rb +610 -0
- data/lib/aspera/proxy_auto_config.js +2 -2
- data/lib/aspera/proxy_auto_config.rb +5 -2
- data/lib/aspera/rainbow.rb +30 -1
- data/lib/aspera/rest/aspera_errors.rb +55 -60
- data/lib/aspera/rest/call_error.rb +3 -0
- data/lib/aspera/rest/client.rb +42 -42
- data/lib/aspera/rest/error_analyzer.rb +14 -2
- data/lib/aspera/rest/list.rb +14 -10
- data/lib/aspera/rest/parameters.rb +3 -0
- data/lib/aspera/rest/util.rb +13 -4
- data/lib/aspera/rest.rb +9 -7
- data/lib/aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml +9 -11
- data/lib/aspera/schema/IBM Aspera Orchestrator API-v1-enhanced.yaml +4089 -0
- data/lib/aspera/schema/IBM Aspera faspio Gateway API-1.0.0-enhanced.yaml +1097 -0
- data/lib/aspera/schema/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +11 -11
- data/lib/aspera/schema/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +2437 -1437
- data/lib/aspera/schema/IBM_Aspera_Shares.yaml +819 -7
- data/lib/aspera/schema/async_tables.yaml +11 -10
- data/lib/aspera/schema/documentation.rb +56 -78
- data/lib/aspera/schema/reader.rb +46 -11
- data/lib/aspera/schema/registry.rb +45 -24
- data/lib/aspera/schema/transferd-config.schema.json +333 -0
- data/lib/aspera/schema/validator.rb +4 -1
- data/lib/aspera/sealed_hash.rb +166 -0
- data/lib/aspera/secret/hider.rb +201 -0
- data/lib/aspera/secret/reference.rb +150 -0
- data/lib/aspera/secret/resolver.rb +212 -0
- data/lib/aspera/secret/store/base.rb +153 -0
- data/lib/aspera/secret/store/encrypted_hash.rb +329 -0
- data/lib/aspera/secret/store/factory.rb +103 -0
- data/lib/aspera/secret/store/hashicorp_vault.rb +390 -0
- data/lib/aspera/secret/store/keeper.rb +141 -0
- data/lib/aspera/secret/store/macos_security.rb +284 -0
- data/lib/aspera/secret/store/one_password_api.rb +99 -0
- data/lib/aspera/secret/store/one_password_base.rb +42 -0
- data/lib/aspera/secret/store/one_password_cli.rb +110 -0
- data/lib/aspera/secret/store/secret_service.rb +125 -0
- data/lib/aspera/secret/strength.rb +119 -0
- data/lib/aspera/servlet_helpers.rb +114 -0
- data/lib/aspera/ssh.rb +10 -7
- data/lib/aspera/ssl.rb +5 -2
- data/lib/aspera/string_ext.rb +3 -0
- data/lib/aspera/sync/args.schema.yaml +6 -1
- data/lib/aspera/sync/conf.schema.yaml +25 -10
- data/lib/aspera/sync/database.rb +3 -0
- data/lib/aspera/sync/operations.rb +34 -12
- data/lib/aspera/temp_file_manager.rb +85 -22
- data/lib/aspera/timer_limiter.rb +3 -0
- data/lib/aspera/transfer/error.rb +4 -1
- data/lib/aspera/transfer/faux_file.rb +4 -1
- data/lib/aspera/transfer/parameters.rb +22 -14
- data/lib/aspera/transfer/result.rb +3 -0
- data/lib/aspera/transfer/resumer.rb +6 -4
- data/lib/aspera/transfer/spec.rb +45 -18
- data/lib/aspera/transfer/spec.schema.yaml +8 -5
- data/lib/aspera/transfer/units.rb +75 -0
- data/lib/aspera/uri_reader.rb +57 -7
- data/lib/aspera/web_auth/index.html.erb +20 -0
- data/lib/aspera/web_auth/paste.rb +109 -0
- data/lib/aspera/web_auth/receiver.rb +50 -0
- data/lib/aspera/web_auth/relay.rb +76 -0
- data/lib/aspera/web_auth/reply.html.erb +150 -0
- data/lib/aspera/web_auth/server.rb +59 -14
- data/lib/aspera/web_page.html.erb +262 -0
- data/lib/aspera/web_page.rb +66 -0
- data/lib/aspera/web_server_simple.rb +82 -13
- data/lib/aspera/yaml.rb +5 -1
- data/llms.txt +81 -0
- data/skills/ascli/SKILL.md +88 -0
- data.tar.gz.sig +0 -0
- metadata +116 -48
- metadata.gz.sig +0 -0
- data/lib/aspera/cli/mcp_tool.rb +0 -189
- data/lib/aspera/keychain/base.rb +0 -25
- data/lib/aspera/keychain/encrypted_hash.rb +0 -188
- data/lib/aspera/keychain/factory.rb +0 -59
- data/lib/aspera/keychain/hashicorp_vault.rb +0 -74
- data/lib/aspera/keychain/macos_security.rb +0 -171
- data/lib/aspera/keychain/one_password_api.rb +0 -86
- data/lib/aspera/keychain/one_password_base.rb +0 -34
- data/lib/aspera/keychain/one_password_cli.rb +0 -98
- data/lib/aspera/oauth/base.rb +0 -141
- data/lib/aspera/oauth/web.rb +0 -57
- data/lib/aspera/oauth.rb +0 -9
- data/lib/aspera/schema/IBM Aspera Orchestrator API-v1.yaml +0 -1784
- data/lib/aspera/schema/IBM Aspera faspio Gateway API-1.0.0.yaml +0 -249
- data/lib/aspera/secret_hider.rb +0 -117
- data/lib/aspera/web_auth/index.html +0 -147
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
#
|
|
123
|
-
def setup_extended_value_handlers
|
|
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,
|
|
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)
|
|
@@ -174,7 +178,6 @@ module Aspera
|
|
|
174
178
|
OAuth::Web.additional_info = "#{Info::CMD_NAME} v#{Cli::VERSION}"
|
|
175
179
|
Transfer::Parameters.file_list_folder = File.join(@context.main_folder, FILE_LIST_FOLDER_NAME)
|
|
176
180
|
Rest::ErrorAnalyzer.instance.log_file = File.join(@context.main_folder, REST_EXCEPTIONS_LOG_FILENAME)
|
|
177
|
-
Rest::AsperaErrors.register_handlers
|
|
178
181
|
end
|
|
179
182
|
|
|
180
183
|
# @return [String] ~/.aspera
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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'
|
|
7
10
|
|
|
8
11
|
module Aspera
|
|
9
12
|
module Cli
|
|
@@ -116,6 +119,10 @@ module Aspera
|
|
|
116
119
|
@current_option = nil
|
|
117
120
|
# When set, only positional arguments after this token are available
|
|
118
121
|
@arguments_after = nil
|
|
122
|
+
# Last positional token consumed by `shift_arguments`
|
|
123
|
+
@last_shifted = nil
|
|
124
|
+
# Token of the last command word (set by `mark_command`)
|
|
125
|
+
@command_token = nil
|
|
119
126
|
process_options = true
|
|
120
127
|
argv.each do |value|
|
|
121
128
|
if process_options && value.eql?(Option::STOP)
|
|
@@ -205,9 +212,18 @@ module Aspera
|
|
|
205
212
|
else Aspera.error_unexpected_value(multiple) { 'multiple' }
|
|
206
213
|
end
|
|
207
214
|
selected.each { |t| t.consumed = true }
|
|
215
|
+
@last_shifted = selected.last unless selected.empty?
|
|
208
216
|
selected.map(&:value)
|
|
209
217
|
end
|
|
210
218
|
|
|
219
|
+
# Remember the last consumed positional token as the current command word.
|
|
220
|
+
# Options placed after it can then be selected with `each_long_option_with_value(after_command: true)`.
|
|
221
|
+
# @return [nil]
|
|
222
|
+
def mark_command
|
|
223
|
+
@command_token = @last_shifted
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
211
227
|
# Add an argument before the available positional arguments
|
|
212
228
|
# @param value [String] argument value
|
|
213
229
|
def unshift_argument(value)
|
|
@@ -216,9 +232,11 @@ module Aspera
|
|
|
216
232
|
end
|
|
217
233
|
|
|
218
234
|
# Consume all long options with a value, whether applied or not.
|
|
235
|
+
# @param after_command [Boolean] `true`: only options placed after the last command word (see `mark_command`)
|
|
219
236
|
# @yieldparam tok [Option] long option token with a value
|
|
220
|
-
def each_long_option_with_value
|
|
221
|
-
@tokens.
|
|
237
|
+
def each_long_option_with_value(after_command: false)
|
|
238
|
+
start = after_command && @command_token ? @tokens.index { |t| t.equal?(@command_token) } + 1 : 0
|
|
239
|
+
@tokens.drop(start).each do |tok|
|
|
222
240
|
next unless tok.is_a?(Option) && tok.short_char.nil? && !tok.value.nil?
|
|
223
241
|
yield(tok)
|
|
224
242
|
tok.consumed = true
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
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
|
|
@@ -20,7 +24,8 @@ module Aspera
|
|
|
20
24
|
# own_children_of(path) - Hash{Symbol => CommandSpec} of direct children declared on the node, without mounted ones
|
|
21
25
|
# arguments_at(path) - Array of ArgumentSpec read by the node at path (mount arguments first)
|
|
22
26
|
# leaf_paths - Array of all leaf paths (follows mounts, or stops at mount nodes)
|
|
23
|
-
# command_path(words) - command path designated by command line words (aliases, arguments)
|
|
27
|
+
# command_path(words) - command path designated by command line words (aliases, prefixes, arguments)
|
|
28
|
+
# command_position(words) - command path and number of its arguments, as read by the dispatcher
|
|
24
29
|
# all_paths - Array of all locally registered full paths
|
|
25
30
|
# any? - true if at least one spec has been registered
|
|
26
31
|
# validate! - cross-spec consistency checks; raises on violation
|
|
@@ -139,7 +144,7 @@ module Aspera
|
|
|
139
144
|
end
|
|
140
145
|
end
|
|
141
146
|
|
|
142
|
-
# Command path designated by words of a command line: sub-commands (or their aliases),
|
|
147
|
+
# Command path designated by words of a command line: sub-commands (or their aliases, or a unique prefix),
|
|
143
148
|
# each possibly followed by its positional arguments, e.g. `packages receive ALL` designates `packages receive`.
|
|
144
149
|
# Words after a leaf command are its arguments.
|
|
145
150
|
# @param words [Array<Symbol>] words following the plugin name
|
|
@@ -152,7 +157,7 @@ module Aspera
|
|
|
152
157
|
words.each do |word|
|
|
153
158
|
children = children_of(path)
|
|
154
159
|
break if children.empty?
|
|
155
|
-
id =
|
|
160
|
+
id = child_id(children, word)
|
|
156
161
|
if id
|
|
157
162
|
path += [id]
|
|
158
163
|
arguments = arguments_at(path)
|
|
@@ -166,6 +171,30 @@ module Aspera
|
|
|
166
171
|
[path, nil]
|
|
167
172
|
end
|
|
168
173
|
|
|
174
|
+
# Position reached by words of a command line, as read by the dispatcher: an intermediate command reads
|
|
175
|
+
# its positional arguments before its sub-command, a leaf command reads the following words as arguments.
|
|
176
|
+
# Unlike `command_path`, a word expected as argument is never taken as a sub-command.
|
|
177
|
+
# @param words [Array<Symbol>] words following the plugin name
|
|
178
|
+
# @return [Array(Array<Symbol>, Integer), nil] command path, and number of its positional arguments given;
|
|
179
|
+
# `nil` if a word is neither a sub-command nor an expected argument
|
|
180
|
+
def command_position(words)
|
|
181
|
+
path = []
|
|
182
|
+
given = 0
|
|
183
|
+
words.each do |word|
|
|
184
|
+
children = children_of(path)
|
|
185
|
+
arguments = arguments_at(path)
|
|
186
|
+
if given < arguments.length || (children.empty? && arguments.last&.multiple)
|
|
187
|
+
given += 1
|
|
188
|
+
else
|
|
189
|
+
id = child_id(children, word)
|
|
190
|
+
return nil if id.nil?
|
|
191
|
+
path += [id]
|
|
192
|
+
given = 0
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
[path, given]
|
|
196
|
+
end
|
|
197
|
+
|
|
169
198
|
# @return [Array<Array<Symbol>>] all locally registered full paths
|
|
170
199
|
def all_paths
|
|
171
200
|
@specs.keys
|
|
@@ -265,6 +294,21 @@ module Aspera
|
|
|
265
294
|
|
|
266
295
|
private
|
|
267
296
|
|
|
297
|
+
# As the parser: an exact name, else the unique name starting with word (e.g. `brow` for alias `browse` of `ls`).
|
|
298
|
+
# @param children [Hash{Symbol => CommandSpec}] children of a node
|
|
299
|
+
# @param word [Symbol] command line word
|
|
300
|
+
# @return [Symbol, nil] id of the child designated by word: id or alias, or unique prefix of one
|
|
301
|
+
def child_id(children, word)
|
|
302
|
+
# Name (id or alias) => id
|
|
303
|
+
names = children.each_with_object({}) do |(id, spec), h|
|
|
304
|
+
h[id] = id
|
|
305
|
+
Array(spec.aliases).each { |a| h[a] = id }
|
|
306
|
+
end
|
|
307
|
+
return names[word] if names.key?(word)
|
|
308
|
+
matching = names.keys.select { |name| name.to_s.start_with?(word.to_s) }
|
|
309
|
+
names[matching.first] if matching.length.eql?(1)
|
|
310
|
+
end
|
|
311
|
+
|
|
268
312
|
# @return [Boolean] true if plugin_class defines instance method name (public or private)
|
|
269
313
|
def instance_method?(plugin_class, name)
|
|
270
314
|
plugin_class.method_defined?(name) || plugin_class.private_method_defined?(name)
|
|
@@ -282,6 +326,7 @@ module Aspera
|
|
|
282
326
|
# Rule: an argument following an optional one, or one that takes all remaining arguments, is never read reliably
|
|
283
327
|
raise ArgumentError, "#{where}: mandatory after optional #{previous.name}" if previous && arg.mandatory && !previous.mandatory
|
|
284
328
|
raise ArgumentError, "#{where}: after #{previous.name}, which takes all remaining arguments" if previous&.multiple.eql?(true)
|
|
329
|
+
raise ArgumentError, "#{where}: complete: #{arg.complete.inspect} is not one of #{Complete::KINDS.inspect}" unless arg.complete.nil? || Complete::KINDS.include?(arg.complete)
|
|
285
330
|
unless arg.lookup.nil?
|
|
286
331
|
# Rule: the percent-selector lookup is only used for identifiers
|
|
287
332
|
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
|
-
|
|
4
|
-
|
|
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,6 +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
|
|
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
|
+
|
|
27
41
|
ArgumentSpec = Struct.new(
|
|
28
42
|
:name,
|
|
29
43
|
:description,
|
|
@@ -36,6 +50,9 @@ module Aspera
|
|
|
36
50
|
:lookup,
|
|
37
51
|
:allowed,
|
|
38
52
|
:interactive,
|
|
53
|
+
:evaluate,
|
|
54
|
+
:complete,
|
|
55
|
+
:omit_absent,
|
|
39
56
|
keyword_init: true
|
|
40
57
|
) do
|
|
41
58
|
def initialize(**kwargs)
|
|
@@ -43,7 +60,11 @@ module Aspera
|
|
|
43
60
|
kwargs[:multiple] = false if kwargs[:multiple].nil?
|
|
44
61
|
kwargs[:bulk] = false if kwargs[:bulk].nil?
|
|
45
62
|
kwargs[:interactive] = false if kwargs[:interactive].nil?
|
|
46
|
-
kwargs[:
|
|
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])
|
|
47
68
|
super
|
|
48
69
|
end
|
|
49
70
|
|
|
@@ -77,6 +98,8 @@ module Aspera
|
|
|
77
98
|
# @!attribute shorthand [String, nil] For a `Hash` option: a `String` value is stored as `{shorthand => value}`
|
|
78
99
|
# @!attribute deprecation [Hash, nil] Forwarded to options.declare as deprecation: `{last:, message:}`
|
|
79
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`
|
|
80
103
|
OptionSpec = Struct.new(
|
|
81
104
|
:name,
|
|
82
105
|
:description,
|
|
@@ -87,6 +110,8 @@ module Aspera
|
|
|
87
110
|
:shorthand,
|
|
88
111
|
:deprecation,
|
|
89
112
|
:schema,
|
|
113
|
+
:complete,
|
|
114
|
+
:security,
|
|
90
115
|
keyword_init: true
|
|
91
116
|
) do
|
|
92
117
|
# Declare this option on a parser, resolving the `on_set` callback.
|
|
@@ -103,7 +128,9 @@ module Aspera
|
|
|
103
128
|
on_set: resolved_on_set(target),
|
|
104
129
|
shorthand: shorthand,
|
|
105
130
|
deprecation: deprecation,
|
|
106
|
-
schema: schema
|
|
131
|
+
schema: schema,
|
|
132
|
+
complete: complete,
|
|
133
|
+
security: security
|
|
107
134
|
)
|
|
108
135
|
end
|
|
109
136
|
|
|
@@ -179,6 +206,7 @@ module Aspera
|
|
|
179
206
|
# @!attribute condition [Symbol, nil] Instance method returning Boolean; if false command is hidden from dispatch
|
|
180
207
|
# @!attribute query_schema [String, nil] Schema path for --query help; when set, the runner hints `--query=help`
|
|
181
208
|
# @!attribute mount [MountSpec, Hash, nil] Expose children of another plugin's registry under this node
|
|
209
|
+
# @!attribute security [Symbol, nil] Most restrictive security level at which the command is available (`:low`: executes code), see `Security`
|
|
182
210
|
CommandSpec = Struct.new(
|
|
183
211
|
:id,
|
|
184
212
|
:parent,
|
|
@@ -191,9 +219,11 @@ module Aspera
|
|
|
191
219
|
:condition,
|
|
192
220
|
:query_schema,
|
|
193
221
|
:mount,
|
|
222
|
+
:security,
|
|
194
223
|
keyword_init: true
|
|
195
224
|
) do
|
|
196
225
|
def initialize(**kwargs)
|
|
226
|
+
Aspera.assert_values(kwargs[:security], Security::LEVELS + [nil]) { "#{kwargs[:id]}: security" }
|
|
197
227
|
# Coerce each element of arguments: from Hash to ArgumentSpec if needed
|
|
198
228
|
if kwargs[:arguments]
|
|
199
229
|
kwargs[:arguments] = kwargs[:arguments].map do |a|
|
|
@@ -2,11 +2,59 @@
|
|
|
2
2
|
# Activate: eval "$(ascli config completion bash)"
|
|
3
3
|
|
|
4
4
|
_ascli_complete() {
|
|
5
|
-
local
|
|
6
|
-
# COMP_WORDS
|
|
7
|
-
local
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
local line=${COMP_LINE:0:COMP_POINT}
|
|
6
|
+
# Shell words up to the cursor: COMP_WORDS is also split on COMP_WORDBREAKS (e.g. `=`, `:`), join the parts not separated by blanks
|
|
7
|
+
local -a words=()
|
|
8
|
+
local i word blanks rest=$line
|
|
9
|
+
for ((i = 0; i <= COMP_CWORD; i++)); do
|
|
10
|
+
blanks=${rest%%[![:blank:]]*}
|
|
11
|
+
rest=${rest#"$blanks"}
|
|
12
|
+
if ((i == COMP_CWORD)); then word=$rest; else word=${COMP_WORDS[i]}; fi
|
|
13
|
+
if ((i == 0)) || [[ -n $blanks ]]; then
|
|
14
|
+
words+=("$word")
|
|
15
|
+
else
|
|
16
|
+
words[${#words[@]} - 1]+=$word
|
|
17
|
+
fi
|
|
18
|
+
rest=${rest#"$word"}
|
|
19
|
+
done
|
|
20
|
+
local cur=${words[${#words[@]} - 1]}
|
|
21
|
+
# Readline replaces only the text after the last word break character, but with it if it is `@` or `$` (special prefixes)
|
|
22
|
+
local prefix=${cur%"${line##*["$COMP_WORDBREAKS"]}"}
|
|
23
|
+
[[ $prefix == *[@\$] ]] && prefix=${prefix%?}
|
|
24
|
+
# words[0] is the program name itself: pass the words before the cursor, then the word at cursor
|
|
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_]+:)$'
|
|
28
|
+
output=$(ascli config completion candidates -- "${words[@]:1:${#words[@]}-2}" "$cur" 2>/dev/null)
|
|
29
|
+
directive=${output##*$'\n'}
|
|
30
|
+
output=${output%"$directive"}
|
|
31
|
+
COMPREPLY=()
|
|
32
|
+
while IFS= read -r entry; do
|
|
33
|
+
# Remove the description
|
|
34
|
+
word=${entry%%$'\t'*}
|
|
35
|
+
[[ -n $word && $word == "$cur"* ]] || continue
|
|
36
|
+
COMPREPLY+=("${word#"$prefix"}")
|
|
37
|
+
[[ $word =~ $continues ]] && compopt -o nospace 2>/dev/null
|
|
38
|
+
done <<<"$output"
|
|
39
|
+
case $directive in
|
|
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
|
|
47
|
+
compopt -o filenames 2>/dev/null
|
|
48
|
+
while IFS= read -r word; do
|
|
49
|
+
word=$files_prefix$word
|
|
50
|
+
COMPREPLY+=("${word#"$prefix"}")
|
|
51
|
+
done < <(compgen -A "$kind" -- "${cur#"$files_prefix"}")
|
|
52
|
+
;;
|
|
53
|
+
":hint "*)
|
|
54
|
+
# Two entries: bash displays them, but inserts nothing
|
|
55
|
+
COMPREPLY=("${directive#:hint }" " ")
|
|
56
|
+
;;
|
|
57
|
+
esac
|
|
10
58
|
}
|
|
11
59
|
|
|
12
60
|
complete -F _ascli_complete ascli
|
|
@@ -5,12 +5,31 @@
|
|
|
5
5
|
# Remove completions of a previous activation
|
|
6
6
|
complete --command ascli --erase
|
|
7
7
|
|
|
8
|
-
#
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
# to `ascli config completion words` and return the result as completions.
|
|
13
|
-
complete --command ascli --arguments '(
|
|
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 `:`)
|
|
10
|
+
function __ascli_complete
|
|
11
|
+
# Words before the cursor (ascli itself excluded), then the word at cursor
|
|
14
12
|
set -l words (commandline -opc)
|
|
15
|
-
|
|
16
|
-
)
|
|
13
|
+
set -l current (commandline -ct)
|
|
14
|
+
set -l lines (ascli config completion candidates -- $words[2..] "$current" 2>/dev/null)
|
|
15
|
+
set -q lines[1]; or return
|
|
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 `:`
|
|
18
|
+
set -l directive $lines[-1]
|
|
19
|
+
set -e lines[-1]
|
|
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
|
|
30
|
+
# printf without argument would output an empty line
|
|
31
|
+
set -q lines[1]; and printf '%s\n' $lines
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# File completion only where proposed by ascli
|
|
35
|
+
complete --command ascli --no-files --arguments '(__ascli_complete)'
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# PowerShell completion for ascli
|
|
2
|
+
# Activate, in $PROFILE: ascli config completion powershell | Out-String | Invoke-Expression
|
|
3
|
+
# Or save as a file with extension .ps1, and dot-source it in $PROFILE
|
|
4
|
+
|
|
5
|
+
& {
|
|
6
|
+
# Output of ascli, per words typed: it depends only on the version of ascli, so it is kept for the session
|
|
7
|
+
$cache = @{}
|
|
8
|
+
# Also applies to ascli.cmd and ascli.bat, and to a path to them: PowerShell matches the name without folder and extension
|
|
9
|
+
Register-ArgumentCompleter -Native -CommandName ascli -ScriptBlock {
|
|
10
|
+
param($wordToComplete, $commandAst, $cursorPosition)
|
|
11
|
+
# Run ascli as typed, e.g. .\ascli.cmd when not in PATH
|
|
12
|
+
$command = $commandAst.GetCommandName()
|
|
13
|
+
# CommandElements[0] is the command itself; pass only the words typed before the cursor, unquoted (e.g. '@:')
|
|
14
|
+
$words = @($commandAst.CommandElements | Select-Object -Skip 1 |
|
|
15
|
+
Where-Object { $_.Extent.EndOffset -lt $cursorPosition } |
|
|
16
|
+
ForEach-Object { if ($_ -is [System.Management.Automation.Language.StringConstantExpressionAst]) { $_.Value } else { $_.Extent.Text } })
|
|
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 { '_' }
|
|
23
|
+
$key = (@($command) + $words + $current) -join ' '
|
|
24
|
+
if (-not $cache.ContainsKey($key)) {
|
|
25
|
+
$cache[$key] = @(& $command config completion candidates -- @words $current 2>$null)
|
|
26
|
+
}
|
|
27
|
+
$lines = $cache[$key]
|
|
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
|
+
}
|
|
49
|
+
# Lines `word<TAB>description`, then the directive
|
|
50
|
+
$results = @($lines | Select-Object -First ($lines.Count - 1) | ForEach-Object {
|
|
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 }))
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
if ($results.Count -gt 0) { return $results }
|
|
57
|
+
# No result: one that does not change the word (a space for an empty word), so that PowerShell does not complete file paths.
|
|
58
|
+
# It shows the hint in the list, if any (e.g. `<String>`)
|
|
59
|
+
$text = if ($wordToComplete) { $wordToComplete } else { ' ' }
|
|
60
|
+
$hint = if ($directive -like ':hint *') { $directive.Substring(6) } else { $text }
|
|
61
|
+
[System.Management.Automation.CompletionResult]::new($text, $hint, 'ParameterValue', $hint)
|
|
62
|
+
}.GetNewClosure()
|
|
63
|
+
}
|
|
@@ -4,10 +4,42 @@
|
|
|
4
4
|
# Or save as file `_ascli` in a folder of $fpath
|
|
5
5
|
|
|
6
6
|
_ascli() {
|
|
7
|
-
local -a
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
local -a lines values values_cont opts opts_cont
|
|
8
|
+
local directive line word entry files_prefix
|
|
9
|
+
# words[1] is 'ascli' itself: pass the words before the cursor, then the word at cursor
|
|
10
|
+
lines=("${(@f)$(ascli config completion candidates -- "${(@)words[2,CURRENT-1]}" "$PREFIX" 2>/dev/null)}")
|
|
11
|
+
directive=${lines[-1]}
|
|
12
|
+
# Candidates `word<TAB>description` to `word:description`, as expected by _describe
|
|
13
|
+
for line in "${(@)lines[1,-2]}"; do
|
|
14
|
+
[[ -n $line ]] || continue
|
|
15
|
+
word=${line%%$'\t'*}
|
|
16
|
+
entry=${word//:/\\:}
|
|
17
|
+
[[ $line == *$'\t'* ]] && entry+=":${line#*$'\t'}"
|
|
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
|
|
27
|
+
done
|
|
28
|
+
case $directive in
|
|
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
|
+
;;
|
|
35
|
+
":hint "*) _message -r "${directive#:hint }" ;;
|
|
36
|
+
*)
|
|
37
|
+
# Options are listed after the other candidates
|
|
38
|
+
_alternative \
|
|
39
|
+
'values:value: _describe -t values value values -- values_cont -S ""' \
|
|
40
|
+
'options:option: _describe -t options option opts -- opts_cont -S ""'
|
|
41
|
+
;;
|
|
42
|
+
esac
|
|
11
43
|
}
|
|
12
44
|
|
|
13
45
|
if [[ "${funcstack[1]}" == _ascli ]]; then
|