-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathuploadAppSettings.ts
More file actions
101 lines (88 loc) · 5.66 KB
/
uploadAppSettings.ts
File metadata and controls
101 lines (88 loc) · 5.66 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { StringDictionary } from "@azure/arm-appservice";
import { confirmOverwriteSettings } from "@microsoft/vscode-azext-azureappservice";
import { AppSettingsTreeItem, IAppSettingsClient } from "@microsoft/vscode-azext-azureappsettings";
import { AzExtFsExtra, IActionContext } from "@microsoft/vscode-azext-utils";
import * as vscode from 'vscode';
import { ConnectionKey, functionFilter, localEventHubsEmulatorConnectionRegExp, localSettingsFileName, localStorageEmulatorConnectionString } from "../../constants";
import { viewOutput } from "../../constants-nls";
import { ext } from "../../extensionVariables";
import { ILocalSettingsJson } from "../../funcConfig/local.settings";
import { localize } from "../../localize";
import * as api from '../../vscode-azurefunctions.api';
import { decryptLocalSettings } from "./localSettings/decryptLocalSettings";
import { encryptLocalSettings } from "./localSettings/encryptLocalSettings";
import { getLocalSettingsFile } from "./localSettings/getLocalSettingsFile";
export async function uploadAppSettings(context: IActionContext, node?: AppSettingsTreeItem, _nodes?: [], workspaceFolder?: vscode.WorkspaceFolder, exclude?: (RegExp | string)[]): Promise<void> {
context.telemetry.eventVersion = 2;
if (!node) {
node = await ext.rgApi.pickAppResource<AppSettingsTreeItem>(context, {
filter: functionFilter,
expectedChildContextValue: new RegExp(AppSettingsTreeItem.contextValue)
});
}
const client: IAppSettingsClient = await node.clientProvider.createClient(context);
await node.runWithTemporaryDescription(context, localize('uploading', 'Uploading...'), async () => {
await uploadAppSettingsInternal(context, client, workspaceFolder, exclude);
});
}
export async function uploadAppSettingsInternal(context: IActionContext, client: api.IAppSettingsClient, workspaceFolder?: vscode.WorkspaceFolder, exclude?: (RegExp | string)[]): Promise<void> {
const message: string = localize('selectLocalSettings', 'Select the local settings file to upload.');
const localSettingsPath: string = await getLocalSettingsFile(context, message, workspaceFolder);
const localSettingsUri: vscode.Uri = vscode.Uri.file(localSettingsPath);
let localSettings: ILocalSettingsJson = <ILocalSettingsJson>await AzExtFsExtra.readJSON(localSettingsPath);
if (localSettings.IsEncrypted) {
await decryptLocalSettings(context, localSettingsUri);
try {
localSettings = await AzExtFsExtra.readJSON<ILocalSettingsJson>(localSettingsPath);
} finally {
await encryptLocalSettings(context, localSettingsUri);
}
}
if (localSettings.Values) {
const remoteSettings: StringDictionary = await client.listApplicationSettings();
if (!remoteSettings.properties) {
remoteSettings.properties = {};
}
const excludedAppSettings: string[] = [];
// Local emulator connections should not be uploaded to the cloud - exclude them (https://github.com/microsoft/vscode-azurefunctions/issues/3298)
if (localSettings.Values[ConnectionKey.Storage] === localStorageEmulatorConnectionString) {
delete localSettings.Values?.[ConnectionKey.Storage];
excludedAppSettings.push(ConnectionKey.Storage);
}
if (localEventHubsEmulatorConnectionRegExp.test(localSettings.Values[ConnectionKey.EventHubs])) {
delete localSettings.Values?.[ConnectionKey.EventHubs];
excludedAppSettings.push(ConnectionKey.EventHubs);
}
if (exclude) {
Object.keys(localSettings.Values).forEach((settingName) => {
if (exclude.some((exclusion) => typeof exclusion === 'string' ? settingName.toLowerCase() === exclusion.toLowerCase() : settingName.match(new RegExp(exclusion, 'i')))) {
delete localSettings.Values?.[settingName];
excludedAppSettings.push(settingName);
}
});
}
const uploadSettings: string = localize('uploadingSettings', 'Uploading settings...');
ext.outputChannel.appendLog(uploadSettings, { resourceName: client.fullName });
await confirmOverwriteSettings(context, localSettings.Values, remoteSettings.properties, client.fullName);
if (excludedAppSettings.length) {
ext.outputChannel.appendLog(localize('excludedSettings', 'Excluded the following settings:'));
excludedAppSettings.forEach((key) => ext.outputChannel.appendLine(`- ${key}`));
}
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: localize('uploadingSettingsTo', 'Uploading settings to "{0}"...', client.fullName) }, async () => {
await client.updateApplicationSettings(remoteSettings);
});
ext.outputChannel.appendLog(localize('uploadedSettings', 'Successfully uploaded settings.'), { resourceName: client.fullName });
// don't wait
void vscode.window.showInformationMessage(localize('uploadedSettingsTo', 'Successfully uploaded settings to "{0}".', client.fullName), viewOutput).then(result => {
if (result === viewOutput) {
ext.outputChannel.show();
}
});
} else {
throw new Error(localize('noSettings', 'No settings found in "{0}".', localSettingsFileName));
}
}