|
3 | 3 | * Licensed under the MIT License. See License.md in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -import { type Container, type EnvironmentVar, type Revision } from "@azure/arm-appcontainers"; |
| 6 | +import { KnownActiveRevisionsMode, type Container, type EnvironmentVar, type Revision } from "@azure/arm-appcontainers"; |
7 | 7 | import { type IActionContext } from "@microsoft/vscode-azext-utils"; |
8 | 8 | import { type AzureSubscription } from "@microsoft/vscode-azureresources-api"; |
| 9 | +import * as deepEqual from "deep-eql"; |
9 | 10 | import { ThemeIcon, type TreeItem } from "vscode"; |
10 | 11 | import { ext } from "../../extensionVariables"; |
11 | 12 | import { localize } from "../../utils/localize"; |
12 | 13 | import { getParentResource } from "../../utils/revisionDraftUtils"; |
13 | 14 | import { type ContainerAppModel } from "../ContainerAppItem"; |
14 | | -import { type RevisionsItemModel } from "../revisionManagement/RevisionItem"; |
15 | | - |
16 | | -export class EnvironmentVariableItem implements RevisionsItemModel { |
17 | | - _hideValue: boolean; |
18 | | - constructor(public readonly subscription: AzureSubscription, |
19 | | - public readonly containerApp: ContainerAppModel, |
20 | | - public readonly revision: Revision, |
21 | | - readonly containerId: string, |
| 15 | +import { RevisionDraftDescendantBase } from "../revisionManagement/RevisionDraftDescendantBase"; |
| 16 | +import { RevisionDraftItem } from "../revisionManagement/RevisionDraftItem"; |
| 17 | + |
| 18 | +const clickToView: string = localize('clickToView', 'Hidden value. Click to view.'); |
| 19 | + |
| 20 | +export class EnvironmentVariableItem extends RevisionDraftDescendantBase { |
| 21 | + static readonly contextValue: string = 'environmentVariableItem'; |
| 22 | + static readonly contextValueRegExp: RegExp = new RegExp(EnvironmentVariableItem.contextValue); |
| 23 | + |
| 24 | + id: string = `${this.parentResource.id}/${this.container.image}/${this.envVariable.name}`; |
| 25 | + |
| 26 | + private hideValue: boolean = true; |
| 27 | + private hiddenMessage: string; // Shown when 'hideValue' is true |
| 28 | + private hiddenValue: string; // Shown when 'hideValue' is false |
| 29 | + |
| 30 | + constructor( |
| 31 | + subscription: AzureSubscription, |
| 32 | + containerApp: ContainerAppModel, |
| 33 | + revision: Revision, |
| 34 | + readonly containersIdx: number, |
| 35 | + |
| 36 | + // Used as the basis for the view; can reflect either the original or the draft changes |
22 | 37 | readonly container: Container, |
23 | | - readonly envVariable: EnvironmentVar) { |
24 | | - this._hideValue = true; |
| 38 | + readonly envVariable: EnvironmentVar, |
| 39 | + ) { |
| 40 | + super(subscription, containerApp, revision); |
25 | 41 | } |
26 | | - id: string = `${this.parentResource.id}/${this.container.image}/${this.envVariable.name}` |
27 | 42 |
|
28 | 43 | getTreeItem(): TreeItem { |
29 | 44 | return { |
30 | | - label: this._hideValue ? localize('viewHidden', '{0}=Hidden value. Click to view.', this.envVariable.name) : `${this.envVariable.name}=${this.envOutput}`, |
31 | | - description: this.envVariable.secretRef && !this._hideValue ? localize('secretRef', 'Secret reference') : undefined, |
32 | | - contextValue: 'environmentVariableItem', |
| 45 | + label: this.label, |
| 46 | + contextValue: EnvironmentVariableItem.contextValue, |
| 47 | + description: this.envVariable.secretRef && !this.hideValue ? localize('secretRef', 'Secret reference') : undefined, |
33 | 48 | iconPath: new ThemeIcon('symbol-constant'), |
34 | 49 | command: { |
35 | | - command: 'containerapps.toggleEnvironmentVariableVisibility', |
| 50 | + command: 'containerApps.toggleEnvironmentVariableVisibility', |
36 | 51 | title: localize('commandtitle', 'Toggle Environment Variable Visibility'), |
37 | | - arguments: [this, this._hideValue,] |
| 52 | + arguments: [this, this.hideValue,] |
38 | 53 | } |
39 | 54 | } |
40 | 55 | } |
41 | 56 |
|
42 | 57 | public async toggleValueVisibility(_: IActionContext): Promise<void> { |
43 | | - this._hideValue = !this._hideValue; |
| 58 | + this.hideValue = !this.hideValue; |
44 | 59 | ext.branchDataProvider.refresh(this); |
45 | 60 | } |
46 | 61 |
|
| 62 | + public get label(): string { |
| 63 | + return this.hideValue ? this.hiddenMessage : this.hiddenValue; |
| 64 | + } |
| 65 | + |
| 66 | + private get envOutput(): string { |
| 67 | + return this.envVariable.value || this.envVariable.secretRef || ''; |
| 68 | + } |
| 69 | + |
47 | 70 | private get parentResource(): ContainerAppModel | Revision { |
48 | 71 | return getParentResource(this.containerApp, this.revision); |
49 | 72 | } |
50 | 73 |
|
51 | | - private get envOutput(): string { |
52 | | - return this.envVariable.value ?? this.envVariable.secretRef ?? ''; |
| 74 | + protected setProperties(): void { |
| 75 | + this.hiddenMessage = `${this.envVariable.name}=${clickToView}`; |
| 76 | + this.hiddenValue = `${this.envVariable.name}=${this.envOutput}`; |
| 77 | + } |
| 78 | + |
| 79 | + protected setDraftProperties(): void { |
| 80 | + this.hiddenMessage = `${this.envVariable.name}=${clickToView} *`; |
| 81 | + this.hiddenValue = `${this.envVariable.name}=${this.envOutput} *`; |
| 82 | + } |
| 83 | + |
| 84 | + hasUnsavedChanges(): boolean { |
| 85 | + // We only care about showing changes to descendants of the revision draft item when in multiple revisions mode |
| 86 | + if (this.containerApp.revisionsMode === KnownActiveRevisionsMode.Multiple && !RevisionDraftItem.hasDescendant(this)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + const currentContainers: Container[] = this.parentResource.template?.containers ?? []; |
| 91 | + const currentContainer: Container | undefined = currentContainers[this.containersIdx]; |
| 92 | + const currentEnv: EnvironmentVar | undefined = currentContainer.env?.find(env => env.name === this.envVariable.name); |
| 93 | + |
| 94 | + return !currentEnv || !deepEqual(this.envVariable, currentEnv); |
53 | 95 | } |
54 | 96 | } |
0 commit comments