-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEnvFileListStep.ts
More file actions
180 lines (154 loc) · 9.21 KB
/
EnvFileListStep.ts
File metadata and controls
180 lines (154 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type EnvironmentVar } from "@azure/arm-appcontainers";
import { ActivityChildItem, ActivityChildType, AzExtFsExtra, AzureWizardPromptStep, activityInfoContext, activityInfoIcon, activitySuccessContext, activitySuccessIcon, createContextValue, type ConfirmationViewProperty } from "@microsoft/vscode-azext-utils";
import { parse, type DotenvParseOutput } from "dotenv";
import { RelativePattern, workspace, type Uri, type WorkspaceFolder } from "vscode";
import { ImageSource, envFileGlobPattern } from "../../../constants";
import { ext } from "../../../extensionVariables";
import { type EnvironmentVariableTelemetryProps as TelemetryProps } from "../../../telemetry/ImageSourceTelemetryProps";
import { type SetTelemetryProps } from "../../../telemetry/SetTelemetryProps";
import { prependOrInsertAfterLastInfoChild } from "../../../utils/activityUtils";
import { localize } from "../../../utils/localize";
import { selectWorkspaceFile } from "../../../utils/workspaceUtils";
import { type EnvironmentVariablesContext } from "../../environmentVariables/EnvironmentVariablesContext";
export interface EnvFileListStepOptions {
suppressSkipPick?: boolean;
}
export enum SetEnvironmentVariableOption {
NoDotEnv = 'noDotEnv',
SkipForNow = 'skipForNow',
ProvideFile = 'provideFile',
UseExisting = 'useExisting'
}
type EnvFileListContext = EnvironmentVariablesContext & SetTelemetryProps<TelemetryProps>;
const allEnvFilesGlobPattern: string = `**/${envFileGlobPattern}`;
const envFileListStepContext: string = 'envFileListStepItem';
export class EnvFileListStep<T extends EnvFileListContext> extends AzureWizardPromptStep<T> {
private _setEnvironmentVariableOption?: SetEnvironmentVariableOption;
private hasLogged: boolean = false;
constructor(public readonly options?: EnvFileListStepOptions) {
super();
}
public async configureBeforePrompt(context: T): Promise<void> {
if (context.environmentVariables?.length === 0) {
context.telemetry.properties.environmentVariableFileCount = '0';
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.NoDotEnv;
}
if (this._setEnvironmentVariableOption && !this.hasLogged) {
this.outputLogs(context, this._setEnvironmentVariableOption);
}
}
public async prompt(context: T): Promise<void> {
const existingData = context.template?.containers?.[context.containersIdx ?? 0].env ?? context.containerApp?.template?.containers?.[context.containersIdx ?? 0].env;
context.envPath ??= await this.promptForEnvPath(context, !!existingData /** showHasExistingData */);
if (context.envPath) {
context.environmentVariables = await this.parseEnvironmentVariablesFromEnvPath(context.envPath);
} else if (existingData) {
context.environmentVariables = existingData;
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.UseExisting;
} else {
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.SkipForNow;
}
if (this._setEnvironmentVariableOption) {
this.outputLogs(context, this._setEnvironmentVariableOption);
}
}
public shouldPrompt(context: T): boolean {
return context.imageSource !== ImageSource.QuickstartImage && context.environmentVariables === undefined;
}
public confirmationViewProperty(context: T): ConfirmationViewProperty {
return {
name: localize('environmentVariables', 'Environment Variables'),
value: context.envPath ?? localize('useExisting', 'Use existing configuration'),
contextPropertyName: 'envPath'
};
}
private async promptForEnvPath(context: T, showHasExistingData?: boolean): Promise<string | undefined> {
const placeHolder: string = localize('setEnvVar', 'Select a {0} file to set the environment variables for the container instance', '.env');
const skipLabel: string | undefined = showHasExistingData ? localize('useExisting', 'Use existing configuration') : undefined;
return await selectWorkspaceFile(context, placeHolder,
{ filters: { 'env file': ['env', 'env.*'] }, allowSkip: !this.options?.suppressSkipPick, skipLabel }, allEnvFilesGlobPattern);
}
private async parseEnvironmentVariablesFromEnvPath(envPath: string | undefined): Promise<EnvironmentVar[]> {
if (!envPath || !await AzExtFsExtra.pathExists(envPath)) {
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.SkipForNow;
return [];
}
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.ProvideFile;
return await EnvFileListStep.parseEnvironmentVariablesFromEnvPath(envPath);
}
public static async parseEnvironmentVariablesFromEnvPath(envPath: string | undefined): Promise<EnvironmentVar[]> {
if (!envPath || !await AzExtFsExtra.pathExists(envPath)) {
return [];
}
const data: string = await AzExtFsExtra.readFile(envPath);
const envData: DotenvParseOutput = parse(data);
return Object.keys(envData).map(name => { return { name, value: envData[name] } });
}
public static async workspaceHasEnvFile(rootFolder?: WorkspaceFolder): Promise<boolean> {
let envFileUris: Uri[];
if (rootFolder) {
const relativePattern: RelativePattern = new RelativePattern(rootFolder, allEnvFilesGlobPattern);
envFileUris = await workspace.findFiles(relativePattern);
} else {
envFileUris = await workspace.findFiles(allEnvFilesGlobPattern);
}
return !!envFileUris.length;
}
private outputLogs(context: T, setEnvironmentVariableOption: SetEnvironmentVariableOption): void {
if (this.hasLogged) {
// Todo: Handle this with the undo method (not currently exposed in utils type def file)
// This path indicates user clicked the back button, so we need to undo the previous logs
context.activityChildren?.pop();
ext.outputChannel.appendLog(localize('resetEnv', 'User chose to go back a step - resetting environment variables.'));
}
context.telemetry.properties.setEnvironmentVariableOption = setEnvironmentVariableOption;
if (
setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ||
setEnvironmentVariableOption === SetEnvironmentVariableOption.SkipForNow
) {
context.activityChildren?.push(
new ActivityChildItem({
label: localize('skipEnvVarsLabel',
'Skip environment variable configuration' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' (no .env files found)' : '')
),
description: '0s',
contextValue: createContextValue([envFileListStepContext, setEnvironmentVariableOption, activitySuccessContext]),
activityType: ActivityChildType.Success,
iconPath: activitySuccessIcon,
})
);
const logMessage: string = localize('skippedEnvVarsMessage',
'Skipped environment variable configuration for the container app' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' because no .env files were detected.' : '.')
);
ext.outputChannel.appendLog(logMessage);
} else if (setEnvironmentVariableOption === SetEnvironmentVariableOption.ProvideFile) {
context.activityChildren?.push(
new ActivityChildItem({
label: localize('saveEnvVarsFileLabel', 'Save environment variables from provided .env file'),
description: '0s',
contextValue: createContextValue([envFileListStepContext, activitySuccessContext]),
activityType: ActivityChildType.Success,
iconPath: activitySuccessIcon,
})
);
ext.outputChannel.appendLog(localize('savedEnvVarsFileMessage', 'Saved environment variables using provided .env file "{0}".', context.envPath));
} else if (setEnvironmentVariableOption === SetEnvironmentVariableOption.UseExisting) {
prependOrInsertAfterLastInfoChild(context,
new ActivityChildItem({
label: localize('useExistingEnvVarsLabel', 'Use existing environment variable configuration'),
contextValue: createContextValue([envFileListStepContext, activityInfoContext]),
activityType: ActivityChildType.Info,
iconPath: activityInfoIcon,
})
);
ext.outputChannel.appendLog(localize('useExistingEnvVarsMessage', 'Used existing environment variable configuration.'));
}
this.hasLogged = true;
}
}