|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE.md in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { AzureWizardPromptStep, IActionContext, NoResourceFoundError } from '@microsoft/vscode-azext-utils'; |
| 7 | +import { parseDockerLikeImageName } from '@microsoft/vscode-container-client'; |
| 8 | +import { CommonRegistry } from '@microsoft/vscode-docker-registries'; |
| 9 | +import * as vscode from 'vscode'; |
| 10 | +import { ext } from '../../../extensionVariables'; |
| 11 | +import { NormalizedImageNameInfo } from '../../../tree/images/NormalizedImageNameInfo'; |
| 12 | +import { AzureSubscriptionRegistryItem, isAzureSubscriptionRegistryItem } from '../../../tree/registries/Azure/AzureRegistryDataProvider'; |
| 13 | +import { UnifiedRegistryItem } from '../../../tree/registries/UnifiedRegistryTreeDataProvider'; |
| 14 | +import { registryExperience } from '../../../utils/registryExperience'; |
| 15 | +import { PushImageWizardContext } from './PushImageWizardContext'; |
| 16 | + |
| 17 | +export class GetRegistryTargetPromptStep extends AzureWizardPromptStep<PushImageWizardContext> { |
| 18 | + public async configureBeforePrompt(wizardContext: PushImageWizardContext): Promise<void> { |
| 19 | + // If the image is qualified (has a slash), we'll look for a matching registry in the tree view |
| 20 | + if (this.registryIsDeterminate(wizardContext.initialTag)) { |
| 21 | + wizardContext.connectedRegistry = await this.tryGetConnectedRegistryForPath(wizardContext, wizardContext.initialTag); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public async prompt(wizardContext: PushImageWizardContext): Promise<void> { |
| 26 | + try { |
| 27 | + const pickedNode = await registryExperience<CommonRegistry | AzureSubscriptionRegistryItem>(wizardContext, { contextValueFilter: { include: [/commonregistry/i, /azuresubscription/i] } }); |
| 28 | + if (isAzureSubscriptionRegistryItem(pickedNode.wrappedItem)) { |
| 29 | + // An Azure subscription node was chosen. The CreatePickAcrPromptStep will be activated, instead of the generic RecursiveQuickPickStep that would normally be used. |
| 30 | + wizardContext.azureSubscriptionNode = pickedNode as UnifiedRegistryItem<AzureSubscriptionRegistryItem>; |
| 31 | + } else { |
| 32 | + wizardContext.connectedRegistry = pickedNode as UnifiedRegistryItem<CommonRegistry>; |
| 33 | + } |
| 34 | + } catch (error) { |
| 35 | + if (error instanceof NoResourceFoundError) { |
| 36 | + // Do nothing, move on without a selected registry |
| 37 | + } else { |
| 38 | + // Rethrow |
| 39 | + throw error; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public shouldPrompt(wizardContext: PushImageWizardContext): boolean { |
| 45 | + return !wizardContext.connectedRegistry && !this.registryIsDeterminate(wizardContext.initialTag) && this.shouldPromptForRegistry; |
| 46 | + } |
| 47 | + |
| 48 | + private async tryGetConnectedRegistryForPath(context: IActionContext, baseImagePath: string): Promise<UnifiedRegistryItem<CommonRegistry> | undefined> { |
| 49 | + const baseImageNameInfo = parseDockerLikeImageName(baseImagePath); |
| 50 | + const normalizedImageNameInfo = new NormalizedImageNameInfo(baseImageNameInfo); |
| 51 | + |
| 52 | + const allRegistries = await vscode.window.withProgress({ |
| 53 | + location: vscode.ProgressLocation.Notification, |
| 54 | + title: vscode.l10n.t('Determining registry to push to...'), |
| 55 | + }, () => ext.registriesTree.getConnectedRegistries(normalizedImageNameInfo.normalizedRegistry)); |
| 56 | + |
| 57 | + return allRegistries.find((registry) => registry.wrappedItem.baseUrl.authority === normalizedImageNameInfo.normalizedRegistry); |
| 58 | + } |
| 59 | + |
| 60 | + private get shouldPromptForRegistry(): boolean { |
| 61 | + return vscode.workspace.getConfiguration('docker').get('promptForRegistryWhenPushingImages', true); |
| 62 | + } |
| 63 | + |
| 64 | + private registryIsDeterminate(imageName: string): boolean { |
| 65 | + return imageName.includes('/'); |
| 66 | + } |
| 67 | +} |
0 commit comments