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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/BUGS.md +5 -10
- data/CHANGELOG.md +217 -2
- data/CODE_OF_CONDUCT.md +18 -0
- data/CONTRIBUTING.md +135 -56
- data/GOVERNANCE.md +30 -0
- data/LICENSE +201 -0
- data/README.md +122 -26
- data/SECURITY.md +84 -0
- data/bin/ascli +8 -5
- data/bin/asession +9 -6
- data/docs/README.md +1895 -632
- data/docs/test-mcp-with-ai.md +8 -8
- 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 +20 -19
- data/lib/aspera/agent/factory.rb +6 -3
- 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 +7 -4
- data/lib/aspera/api/faspex.rb +16 -9
- data/lib/aspera/api/httpgw.rb +9 -6
- data/lib/aspera/api/node.rb +29 -22
- data/lib/aspera/ascmd.rb +8 -5
- data/lib/aspera/ascp/installation.rb +95 -67
- 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 +370 -0
- data/lib/aspera/ascp/log_locations.rb +163 -0
- data/lib/aspera/ascp/management.rb +16 -8
- data/lib/aspera/ascp/session_errors.rb +217 -0
- data/lib/aspera/assert.rb +4 -0
- data/lib/aspera/cli/ascp_actions.rb +12 -19
- data/lib/aspera/cli/ascp_logs_actions.rb +188 -0
- data/lib/aspera/cli/async_transfer_store.rb +7 -4
- data/lib/aspera/cli/bootstrapper.rb +38 -33
- data/lib/aspera/cli/command_line.rb +63 -8
- data/lib/aspera/cli/command_registry.rb +9 -1
- data/lib/aspera/cli/command_spec.rb +47 -7
- data/lib/aspera/cli/completion/ascli.bash +19 -5
- data/lib/aspera/cli/completion/ascli.fish +13 -2
- data/lib/aspera/cli/completion/ascli.ps1 +31 -9
- data/lib/aspera/cli/completion/ascli.zsh +22 -6
- data/lib/aspera/cli/completion_actions.rb +303 -69
- data/lib/aspera/cli/context.rb +9 -4
- data/lib/aspera/cli/deprecation.rb +5 -2
- data/lib/aspera/cli/error.rb +6 -0
- data/lib/aspera/cli/extended_value.rb +66 -31
- data/lib/aspera/cli/formatter.rb +43 -30
- data/lib/aspera/cli/gem_checker.rb +9 -6
- data/lib/aspera/cli/hints.rb +22 -10
- data/lib/aspera/cli/http.rb +38 -10
- data/lib/aspera/cli/info.rb +12 -7
- data/lib/aspera/cli/mailer.rb +6 -3
- data/lib/aspera/cli/mcp/http_servlet.rb +7 -8
- data/lib/aspera/cli/mcp/secrets.rb +8 -5
- data/lib/aspera/cli/mcp/tool.rb +20 -17
- 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 +77 -18
- data/lib/aspera/cli/options.schema.yaml +641 -80
- data/lib/aspera/cli/parser.rb +71 -35
- data/lib/aspera/cli/plugins/alee.rb +7 -4
- data/lib/aspera/cli/plugins/aoc.rb +248 -37
- data/lib/aspera/cli/plugins/ats.rb +10 -9
- data/lib/aspera/cli/plugins/base.rb +50 -18
- data/lib/aspera/cli/plugins/basic_auth.rb +5 -2
- data/lib/aspera/cli/plugins/config.rb +210 -104
- data/lib/aspera/cli/plugins/console.rb +6 -3
- data/lib/aspera/cli/plugins/cos.rb +8 -4
- data/lib/aspera/cli/plugins/factory.rb +21 -7
- data/lib/aspera/cli/plugins/faspex5.rb +31 -26
- data/lib/aspera/cli/plugins/faspio.rb +7 -5
- data/lib/aspera/cli/plugins/httpgw.rb +7 -4
- data/lib/aspera/cli/plugins/mcp.rb +42 -22
- data/lib/aspera/cli/plugins/node.rb +39 -38
- data/lib/aspera/cli/plugins/oauth.rb +23 -6
- data/lib/aspera/cli/plugins/orchestrator.rb +287 -83
- data/lib/aspera/cli/plugins/preview.rb +72 -45
- data/lib/aspera/cli/plugins/server.rb +66 -14
- data/lib/aspera/cli/plugins/shares.rb +87 -18
- data/lib/aspera/cli/plugins/transferd.rb +228 -0
- data/lib/aspera/cli/poll.rb +236 -0
- data/lib/aspera/cli/preset_actions.rb +144 -30
- data/lib/aspera/cli/preset_manager.rb +113 -35
- data/lib/aspera/cli/prompt.rb +157 -9
- data/lib/aspera/cli/result.rb +12 -8
- data/lib/aspera/cli/runner.rb +89 -48
- data/lib/aspera/cli/secret_finder.rb +4 -1
- data/lib/aspera/cli/security.rb +89 -9
- data/lib/aspera/cli/special_values.rb +4 -1
- data/lib/aspera/cli/sync_actions.rb +15 -10
- data/lib/aspera/cli/table_fitter.rb +3 -0
- data/lib/aspera/cli/terminal_formatter.rb +9 -6
- data/lib/aspera/cli/transfer_actions.rb +11 -8
- data/lib/aspera/cli/transfer_agent.rb +43 -20
- data/lib/aspera/cli/transfer_progress.rb +14 -45
- data/lib/aspera/cli/vault_actions.rb +45 -0
- data/lib/aspera/cli/vault_manager.rb +188 -42
- data/lib/aspera/cli/version.rb +4 -1
- data/lib/aspera/cli/web_secret.rb +12 -10
- data/lib/aspera/cli/wizard.rb +42 -39
- data/lib/aspera/command_line_builder.rb +9 -5
- data/lib/aspera/command_line_converter.rb +4 -1
- data/lib/aspera/coverage.rb +103 -33
- data/lib/aspera/data_repository.rb +4 -1
- data/lib/aspera/dot_container.rb +4 -1
- data/lib/aspera/environment.rb +23 -12
- data/lib/aspera/exec_spec.rb +3 -0
- data/lib/aspera/faspex_gw.rb +13 -9
- data/lib/aspera/faspex_postproc.rb +15 -12
- data/lib/aspera/formatter_interface.rb +4 -1
- data/lib/aspera/graphql.rb +5 -1
- data/lib/aspera/hash_ext.rb +3 -0
- data/lib/aspera/id_generator.rb +20 -5
- data/lib/aspera/inflate.rb +43 -0
- data/lib/aspera/json_rpc/client.rb +6 -5
- data/lib/aspera/json_rpc/version.rb +3 -0
- data/lib/aspera/line_logger.rb +4 -1
- data/lib/aspera/link_header.rb +4 -1
- data/lib/aspera/log.rb +18 -5
- data/lib/aspera/markdown.rb +5 -2
- data/lib/aspera/mime.rb +3 -0
- data/lib/aspera/nagios.rb +6 -4
- data/lib/aspera/node_emulator.rb +15 -13
- data/lib/aspera/oauth/client/base.rb +124 -73
- data/lib/aspera/oauth/client/boot.rb +7 -4
- data/lib/aspera/oauth/client/factory.rb +41 -18
- data/lib/aspera/oauth/client/generic.rb +5 -1
- data/lib/aspera/oauth/client/json_credentials.rb +7 -3
- data/lib/aspera/oauth/client/jwt.rb +6 -3
- data/lib/aspera/oauth/client/shares_web.rb +94 -0
- data/lib/aspera/oauth/client/url_json.rb +7 -3
- data/lib/aspera/oauth/client/web.rb +40 -13
- data/lib/aspera/oauth/server/pkce.rb +6 -5
- data/lib/aspera/orchestrator_workflow_checker.rb +129 -2
- 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 +16 -6
- data/lib/aspera/preview/utils.rb +6 -4
- data/lib/aspera/products/connect.rb +5 -2
- data/lib/aspera/products/desktop.rb +17 -2
- 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 +3 -0
- data/lib/aspera/rest/aspera_errors.rb +4 -1
- data/lib/aspera/rest/call_error.rb +3 -0
- data/lib/aspera/rest/client.rb +45 -34
- data/lib/aspera/rest/error_analyzer.rb +5 -2
- data/lib/aspera/rest/list.rb +8 -4
- data/lib/aspera/rest/parameters.rb +3 -0
- data/lib/aspera/rest/util.rb +8 -5
- data/lib/aspera/rest.rb +9 -6
- data/lib/aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml +9 -9
- data/lib/aspera/schema/IBM Aspera Orchestrator API-v1-enhanced.yaml +118 -30
- 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/async_tables.yaml +11 -10
- data/lib/aspera/schema/documentation.rb +8 -2
- data/lib/aspera/schema/reader.rb +7 -0
- data/lib/aspera/schema/registry.rb +44 -28
- data/lib/aspera/schema/transferd-config.schema.json +333 -0
- data/lib/aspera/schema/validator.rb +4 -1
- data/lib/aspera/sealed_hash.rb +150 -0
- data/lib/aspera/secret/hider.rb +213 -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 +112 -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 +5 -2
- 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 +3 -0
- data/lib/aspera/sync/conf.schema.yaml +9 -4
- data/lib/aspera/sync/database.rb +3 -0
- data/lib/aspera/sync/operations.rb +17 -15
- data/lib/aspera/temp_file_manager.rb +8 -4
- 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 +17 -14
- data/lib/aspera/transfer/result.rb +23 -0
- data/lib/aspera/transfer/resumer.rb +6 -4
- data/lib/aspera/transfer/spec.rb +9 -22
- data/lib/aspera/units/decimal.rb +67 -0
- data/lib/aspera/units/duration.rb +109 -0
- data/lib/aspera/uri_reader.rb +64 -9
- data/lib/aspera/web_auth/index.html.erb +6 -2
- 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 +47 -10
- data/lib/aspera/web_page.rb +7 -2
- data/lib/aspera/web_server_simple.rb +6 -3
- data/lib/aspera/yaml.rb +5 -1
- data/llms-install.md +88 -0
- data/llms.txt +87 -0
- data/skills/ascli/SKILL.md +94 -0
- data.tar.gz.sig +0 -0
- metadata +77 -20
- metadata.gz.sig +0 -0
- data/lib/aspera/duration.rb +0 -42
- data/lib/aspera/keychain/base.rb +0 -30
- data/lib/aspera/keychain/encrypted_hash.rb +0 -192
- data/lib/aspera/keychain/factory.rb +0 -58
- data/lib/aspera/keychain/hashicorp_vault.rb +0 -77
- data/lib/aspera/keychain/macos_security.rb +0 -277
- data/lib/aspera/keychain/one_password_api.rb +0 -91
- data/lib/aspera/keychain/one_password_base.rb +0 -39
- data/lib/aspera/keychain/one_password_cli.rb +0 -100
- data/lib/aspera/secret_hider.rb +0 -196
- data/lib/aspera/secret_strength.rb +0 -114
|
@@ -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)
|
|
@@ -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
|
-
|
|
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'
|
|
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,
|
|
69
|
-
def
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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,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
|
|
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
|
-
:
|
|
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[:
|
|
49
|
-
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])
|
|
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"* ]]
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
#
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
26
|
-
|
|
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
|
-
$
|
|
30
|
-
if ($
|
|
31
|
-
[System.Management.Automation.CompletionResult]::new($
|
|
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 ($
|
|
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
|
-
|
|
15
|
+
word=${line%%$'\t'*}
|
|
16
|
+
entry=${word//:/\\:}
|
|
16
17
|
[[ $line == *$'\t'* ]] && entry+=":${line#*$'\t'}"
|
|
17
|
-
|
|
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
|
|
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
|
|
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
|
}
|