-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEnvironmentVariablesListStep.ts
More file actions
97 lines (83 loc) · 5.45 KB
/
EnvironmentVariablesListStep.ts
File metadata and controls
97 lines (83 loc) · 5.45 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzExtFsExtra, AzureWizardPromptStep, GenericTreeItem, activitySuccessContext, activitySuccessIcon } from "@microsoft/vscode-azext-utils";
import { parse, type DotenvParseOutput } from "dotenv";
import { workspace, type Uri } from "vscode";
import { ImageSource, SetEnvironmentVariableOption, envFileGlobPattern } from "../../../constants";
import { ext } from "../../../extensionVariables";
import { type EnvironmentVariableTelemetryProps as TelemetryProps } from "../../../telemetry/ImageSourceTelemetryProps";
import { type SetTelemetryProps } from "../../../telemetry/SetTelemetryProps";
import { createActivityChildContext } from "../../../utils/activity/activityUtils";
import { localize } from "../../../utils/localize";
import { selectWorkspaceFile } from "../../../utils/workspaceUtils";
import { type ImageSourceBaseContext } from "./ImageSourceContext";
type EnvironmentVariablesContext = ImageSourceBaseContext & SetTelemetryProps<TelemetryProps>;
const allEnvFilesGlobPattern: string = `**/${envFileGlobPattern}`;
export class EnvironmentVariablesListStep extends AzureWizardPromptStep<EnvironmentVariablesContext> {
public async prompt(context: EnvironmentVariablesContext): Promise<void> {
const envData: DotenvParseOutput | undefined = await this.selectEnvironmentSettings(context);
if (!envData) {
context.environmentVariables = [];
this.outputLogs(context, SetEnvironmentVariableOption.SkipForNow);
} else {
context.environmentVariables = Object.keys(envData).map(name => { return { name, value: envData[name] } });
this.outputLogs(context, SetEnvironmentVariableOption.ProvideFile);
}
}
public async configureBeforePrompt(context: EnvironmentVariablesContext): Promise<void> {
if (context.environmentVariables?.length === 0) {
context.telemetry.properties.environmentVariableFileCount = '0';
this.outputLogs(context, SetEnvironmentVariableOption.NoDotEnv);
}
}
public shouldPrompt(context: EnvironmentVariablesContext): boolean {
return context.imageSource !== ImageSource.QuickStartImage && context.environmentVariables === undefined;
}
private async selectEnvironmentSettings(context: EnvironmentVariablesContext): Promise<DotenvParseOutput | undefined> {
const placeHolder: string = localize('setEnvVar', 'Select a {0} file to set the environment variables for the container instance', '.env');
const envFileFsPath: string | undefined = await selectWorkspaceFile(context, placeHolder,
{ filters: { 'env file': ['env', 'env.*'] }, allowSkip: true }, allEnvFilesGlobPattern);
if (!envFileFsPath) {
return undefined;
}
const data = await AzExtFsExtra.readFile(envFileFsPath);
return parse(data);
}
public static async workspaceHasEnvFile(): Promise<boolean> {
const envFileUris: Uri[] = await workspace.findFiles(allEnvFilesGlobPattern);
return !!envFileUris.length;
}
// Todo: It might be nice to add a direct command to update just the environment variables rather than having to suggest to re-run the entire command again
private outputLogs(context: EnvironmentVariablesContext, setEnvironmentVariableOption: SetEnvironmentVariableOption): void {
context.telemetry.properties.setEnvironmentVariableOption = setEnvironmentVariableOption;
if (setEnvironmentVariableOption !== SetEnvironmentVariableOption.ProvideFile) {
context.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['environmentVariablesListStep', setEnvironmentVariableOption, activitySuccessContext]),
label: localize('skipEnvVarsLabel',
'Skip environment variable configuration' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' (no .env files found)' : '')
),
iconPath: activitySuccessIcon
})
);
const logMessage: string = localize('skippedEnvVarsMessage',
'Skipped environment variable configuration for the container app' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' because no .env files were detected. ' : '. ') +
'If you would like to update your environment variables later, try re-running the container app update or deploy command.'
);
ext.outputChannel.appendLog(logMessage);
} else {
context.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createActivityChildContext(['environmentVariablesListStep', setEnvironmentVariableOption, activitySuccessContext]),
label: localize('saveEnvVarsLabel', 'Save environment variable configuration'),
iconPath: activitySuccessIcon
})
);
ext.outputChannel.appendLog(localize('savedEnvVarsMessage', 'Saved environment variable configuration.'));
}
}
}