Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert default command template values into ShellQuotedString format #4433

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/commands/selectCommandTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { IActionContext, IAzureQuickPickItem, IAzureQuickPickOptions, UserCancelledError } from '@microsoft/vscode-azext-utils';
import { PortBinding, VoidCommandResponse } from '@microsoft/vscode-container-client';
import { PortBinding, quoted, VoidCommandResponse } from '@microsoft/vscode-container-client';
import * as vscode from 'vscode';
import { ext } from '../extensionVariables';
import { isDockerComposeClient } from '../runtimes/OrchestratorRuntimeManager';
Expand Down Expand Up @@ -168,14 +168,35 @@ export async function selectCommandTemplate(
throw new Error(vscode.l10n.t('No command template was found for command \'{0}\'', command));
}

actionContext.telemetry.properties.isDefaultCommand = defaultTemplates.some(t => t.template === selectedTemplate.template) ? 'true' : 'false';
const isDefault = defaultTemplates.some(t => t.template === selectedTemplate.template);
actionContext.telemetry.properties.isDefaultCommand = isDefault ? 'true' : 'false';
actionContext.telemetry.properties.isCommandRegexMatched = selectedTemplate.match ? 'true' : 'false';

// This is not really ideal (putting the full command line into `command` instead of `command` + `args`), but parsing a string into a command + args like that is really hard
// Fortunately, `TaskCommandRunnerFactory` does not really care

let resolvedCommand = resolveVariables(selectedTemplate.template, folder, additionalVariables);

if (!isDefault) {
// This is not really ideal (putting the full command line into `command` instead of `command` + `args`), but parsing a string into a command + args like that is really hard
// Fortunately, `TaskCommandRunnerFactory` does not really care
return {
command: resolvedCommand,
args: undefined,
};
}

// For the default command, we can make assumptions that allow us to parse into command + args for better shell support

const argsRegex = /(?:"[^"]*")|(?:[^"\s]*)/g;
const commandAndArgs = resolvedCommand.match(argsRegex).filter(arg => arg.length !== 0);
if (commandAndArgs.length > 0) {
resolvedCommand = commandAndArgs[0].replace(/"/g, '');
}

const resolvedArgs = commandAndArgs.slice(1).map(arg => arg.startsWith('"') ? quoted(arg.replace(/"/g, '')) : arg);

return {
command: resolveVariables(selectedTemplate.template, folder, additionalVariables),
args: undefined,
command: resolvedCommand,
args: resolvedArgs,
};
}

Expand Down
Loading