From 9c6b50f997889017d7a10df9433b7a7a6a630801 Mon Sep 17 00:00:00 2001 From: snehapar9 Date: Mon, 18 Sep 2023 14:43:13 -0700 Subject: [PATCH] Refactor - await method to get/create rg --- dist/index.js | 83 +++++++++++++++++++++++---------------- index.js | 33 ++++++++++------ index.ts | 6 +-- src/CommandHelper.js | 48 ++++++++++++---------- src/CommandHelper.ts | 11 +++--- src/ContainerAppHelper.js | 2 +- src/ContainerAppHelper.ts | 2 +- 7 files changed, 107 insertions(+), 78 deletions(-) diff --git a/dist/index.js b/dist/index.js index c3a5b7ae..7e99dfe9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -266,18 +266,27 @@ var azurecontainerapps = /** @class */ (function () { * @returns The name of the resource group to use for the task. */ azurecontainerapps.getOrCreateResourceGroup = function (containerAppName, location) { - // Get the resource group to deploy to if it was provided, or generate it from the Container App name - var resourceGroup = core.getInput('resourceGroup', { required: false }); - if (util.isNullOrEmpty(resourceGroup)) { - resourceGroup = containerAppName + "-rg"; - console.log("Default resource group name: " + resourceGroup); - // Ensure that the resource group that the Container App will be created in exists - var resourceGroupExists = this.appHelper.doesResourceGroupExist(resourceGroup); - if (!resourceGroupExists) { - this.appHelper.createResourceGroup(resourceGroup, location); - } - } - return resourceGroup; + return __awaiter(this, void 0, void 0, function () { + var resourceGroup, resourceGroupExists; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + resourceGroup = core.getInput('resourceGroup', { required: false }); + if (!util.isNullOrEmpty(resourceGroup)) return [3 /*break*/, 3]; + resourceGroup = containerAppName + "-rg"; + console.log("Default resource group name: " + resourceGroup); + return [4 /*yield*/, this.appHelper.doesResourceGroupExist(resourceGroup)]; + case 1: + resourceGroupExists = _a.sent(); + if (!!resourceGroupExists) return [3 /*break*/, 3]; + return [4 /*yield*/, this.appHelper.createResourceGroup(resourceGroup, location)]; + case 2: + _a.sent(); + _a.label = 3; + case 3: return [2 /*return*/, resourceGroup]; + } + }); + }); }; /** * Gets the name of the Container App Environment to use for the task. If the 'containerAppEnvironment' argument @@ -4601,7 +4610,6 @@ exports.CommandHelper = void 0; var os = __nccwpck_require__(2037); var core = __nccwpck_require__(3195); var exec = __nccwpck_require__(9714); -var io = __nccwpck_require__(9529); var CommandHelper = /** @class */ (function () { function CommandHelper() { } @@ -4612,10 +4620,21 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/, os.platform() == 'win32' ? - this.execPwshCommandAsync(command) : - this.execBashCommandAsync(command)]; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!(os.platform() == 'win32')) return [3 /*break*/, 2]; + return [4 /*yield*/, this.execPwshCommandAsync(command)]; + case 1: + _a = _b.sent(); + return [3 /*break*/, 4]; + case 2: return [4 /*yield*/, this.execBashCommandAsync(command)]; + case 3: + _a = _b.sent(); + _b.label = 4; + case 4: return [2 /*return*/, _a]; + } }); }); }; @@ -4625,7 +4644,7 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execBashCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - var bashOutput, options, pathToTool, err_1; + var bashOutput, options, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: @@ -4647,19 +4666,18 @@ var CommandHelper = /** @class */ (function () { }; _a.label = 1; case 1: - _a.trys.push([1, 4, , 5]); - return [4 /*yield*/, io.which('bash', true)]; + _a.trys.push([1, 3, , 4]); + // const pathToTool = await io.which('bash', true) + return [4 /*yield*/, exec.exec('bash', ['-c', command], options)]; case 2: - pathToTool = _a.sent(); - return [4 /*yield*/, exec.exec(pathToTool, ['-c', command], options)]; - case 3: + // const pathToTool = await io.which('bash', true) _a.sent(); return [2 /*return*/, bashOutput.trim()]; - case 4: + case 3: err_1 = _a.sent(); core.error('Unable to run provided bash command ${command}'); throw err_1; - case 5: return [2 /*return*/]; + case 4: return [2 /*return*/]; } }); }); @@ -4671,7 +4689,7 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execPwshCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - var pwshOutput, options, pathToTool, err_2; + var pwshOutput, options, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: @@ -4693,19 +4711,16 @@ var CommandHelper = /** @class */ (function () { }; _a.label = 1; case 1: - _a.trys.push([1, 4, , 5]); - return [4 /*yield*/, io.which('pwsh', true)]; + _a.trys.push([1, 3, , 4]); + return [4 /*yield*/, exec.exec('pwsh', [command], options)]; case 2: - pathToTool = _a.sent(); - return [4 /*yield*/, exec.exec(pathToTool, [command], options)]; - case 3: _a.sent(); return [2 /*return*/, pwshOutput.trim()]; - case 4: + case 3: err_2 = _a.sent(); core.error('Unable to run provided PowerShell command ${command}'); throw err_2; - case 5: return [2 /*return*/]; + case 4: return [2 /*return*/]; } }); }); @@ -4800,7 +4815,7 @@ var ContainerAppHelper = /** @class */ (function () { optionalCmdArgs.forEach(function (val) { command_1 += " " + val; }); - return [4 /*yield*/, cpExec("" + command_1)]; + return [4 /*yield*/, cpExec({ command: command_1 })]; case 2: _a.sent(); return [3 /*break*/, 4]; diff --git a/index.js b/index.js index 5715a0e2..e40573ca 100644 --- a/index.js +++ b/index.js @@ -259,18 +259,27 @@ var azurecontainerapps = /** @class */ (function () { * @returns The name of the resource group to use for the task. */ azurecontainerapps.getOrCreateResourceGroup = function (containerAppName, location) { - // Get the resource group to deploy to if it was provided, or generate it from the Container App name - var resourceGroup = core.getInput('resourceGroup', { required: false }); - if (util.isNullOrEmpty(resourceGroup)) { - resourceGroup = containerAppName + "-rg"; - console.log("Default resource group name: " + resourceGroup); - // Ensure that the resource group that the Container App will be created in exists - var resourceGroupExists = this.appHelper.doesResourceGroupExist(resourceGroup); - if (!resourceGroupExists) { - this.appHelper.createResourceGroup(resourceGroup, location); - } - } - return resourceGroup; + return __awaiter(this, void 0, void 0, function () { + var resourceGroup, resourceGroupExists; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + resourceGroup = core.getInput('resourceGroup', { required: false }); + if (!util.isNullOrEmpty(resourceGroup)) return [3 /*break*/, 3]; + resourceGroup = containerAppName + "-rg"; + console.log("Default resource group name: " + resourceGroup); + return [4 /*yield*/, this.appHelper.doesResourceGroupExist(resourceGroup)]; + case 1: + resourceGroupExists = _a.sent(); + if (!!resourceGroupExists) return [3 /*break*/, 3]; + return [4 /*yield*/, this.appHelper.createResourceGroup(resourceGroup, location)]; + case 2: + _a.sent(); + _a.label = 3; + case 3: return [2 /*return*/, resourceGroup]; + } + }); + }); }; /** * Gets the name of the Container App Environment to use for the task. If the 'containerAppEnvironment' argument diff --git a/index.ts b/index.ts index dd328b66..477afb1b 100644 --- a/index.ts +++ b/index.ts @@ -238,7 +238,7 @@ export class azurecontainerapps { * @param location - The location to deploy resources to. * @returns The name of the resource group to use for the task. */ - private static getOrCreateResourceGroup(containerAppName: string, location: string): string { + private static async getOrCreateResourceGroup(containerAppName: string, location: string): string { // Get the resource group to deploy to if it was provided, or generate it from the Container App name let resourceGroup: string = core.getInput('resourceGroup', {required: false}); if (util.isNullOrEmpty(resourceGroup)) { @@ -246,9 +246,9 @@ export class azurecontainerapps { console.log(`Default resource group name: ${resourceGroup}`); // Ensure that the resource group that the Container App will be created in exists - const resourceGroupExists = this.appHelper.doesResourceGroupExist(resourceGroup); + const resourceGroupExists = await this.appHelper.doesResourceGroupExist(resourceGroup); if (!resourceGroupExists) { - this.appHelper.createResourceGroup(resourceGroup, location); + await this.appHelper.createResourceGroup(resourceGroup, location); } } diff --git a/src/CommandHelper.js b/src/CommandHelper.js index be0c364c..3e36f855 100644 --- a/src/CommandHelper.js +++ b/src/CommandHelper.js @@ -40,7 +40,6 @@ exports.CommandHelper = void 0; var os = require("os"); var core = require("@actions/core"); var exec = require("@actions/exec"); -var io = require("@actions/io"); var CommandHelper = /** @class */ (function () { function CommandHelper() { } @@ -51,10 +50,21 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/, os.platform() == 'win32' ? - this.execPwshCommandAsync(command) : - this.execBashCommandAsync(command)]; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!(os.platform() == 'win32')) return [3 /*break*/, 2]; + return [4 /*yield*/, this.execPwshCommandAsync(command)]; + case 1: + _a = _b.sent(); + return [3 /*break*/, 4]; + case 2: return [4 /*yield*/, this.execBashCommandAsync(command)]; + case 3: + _a = _b.sent(); + _b.label = 4; + case 4: return [2 /*return*/, _a]; + } }); }); }; @@ -64,7 +74,7 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execBashCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - var bashOutput, options, pathToTool, err_1; + var bashOutput, options, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: @@ -86,19 +96,18 @@ var CommandHelper = /** @class */ (function () { }; _a.label = 1; case 1: - _a.trys.push([1, 4, , 5]); - return [4 /*yield*/, io.which('bash', true)]; + _a.trys.push([1, 3, , 4]); + // const pathToTool = await io.which('bash', true) + return [4 /*yield*/, exec.exec('bash', ['-c', command], options)]; case 2: - pathToTool = _a.sent(); - return [4 /*yield*/, exec.exec(pathToTool, ['-c', command], options)]; - case 3: + // const pathToTool = await io.which('bash', true) _a.sent(); return [2 /*return*/, bashOutput.trim()]; - case 4: + case 3: err_1 = _a.sent(); core.error('Unable to run provided bash command ${command}'); throw err_1; - case 5: return [2 /*return*/]; + case 4: return [2 /*return*/]; } }); }); @@ -110,7 +119,7 @@ var CommandHelper = /** @class */ (function () { */ CommandHelper.prototype.execPwshCommandAsync = function (command) { return __awaiter(this, void 0, void 0, function () { - var pwshOutput, options, pathToTool, err_2; + var pwshOutput, options, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: @@ -132,19 +141,16 @@ var CommandHelper = /** @class */ (function () { }; _a.label = 1; case 1: - _a.trys.push([1, 4, , 5]); - return [4 /*yield*/, io.which('pwsh', true)]; + _a.trys.push([1, 3, , 4]); + return [4 /*yield*/, exec.exec('pwsh', [command], options)]; case 2: - pathToTool = _a.sent(); - return [4 /*yield*/, exec.exec(pathToTool, [command], options)]; - case 3: _a.sent(); return [2 /*return*/, pwshOutput.trim()]; - case 4: + case 3: err_2 = _a.sent(); core.error('Unable to run provided PowerShell command ${command}'); throw err_2; - case 5: return [2 /*return*/]; + case 4: return [2 /*return*/]; } }); }); diff --git a/src/CommandHelper.ts b/src/CommandHelper.ts index 2568081d..cd1d4105 100644 --- a/src/CommandHelper.ts +++ b/src/CommandHelper.ts @@ -12,8 +12,8 @@ export class CommandHelper { */ public async execCommandAsync(command: string): Promise { return os.platform() == 'win32' ? - this.execPwshCommandAsync(command) : - this.execBashCommandAsync(command); + await this.execPwshCommandAsync(command) : + await this.execBashCommandAsync(command); } /** @@ -39,8 +39,8 @@ export class CommandHelper { outStream: process.stdout, } try { - const pathToTool = await io.which('bash', true) - await exec.exec(pathToTool,['-c', command], options); + // const pathToTool = await io.which('bash', true) + await exec.exec('bash',['-c', command], options); return bashOutput.trim(); } catch (err) { core.error('Unable to run provided bash command ${command}'); @@ -71,8 +71,7 @@ export class CommandHelper { outStream: process.stdout, } try { - const pathToTool = await io.which('pwsh', true) - await exec.exec(pathToTool,[command], options); + await exec.exec('pwsh',[command], options); return pwshOutput.trim(); } catch (err) { core.error('Unable to run provided PowerShell command ${command}'); diff --git a/src/ContainerAppHelper.js b/src/ContainerAppHelper.js index 57ce0153..0daf3b94 100644 --- a/src/ContainerAppHelper.js +++ b/src/ContainerAppHelper.js @@ -77,7 +77,7 @@ var ContainerAppHelper = /** @class */ (function () { optionalCmdArgs.forEach(function (val) { command_1 += " " + val; }); - return [4 /*yield*/, cpExec("" + command_1)]; + return [4 /*yield*/, cpExec({ command: command_1 })]; case 2: _a.sent(); return [3 /*break*/, 4]; diff --git a/src/ContainerAppHelper.ts b/src/ContainerAppHelper.ts index 9f23db66..545124cb 100644 --- a/src/ContainerAppHelper.ts +++ b/src/ContainerAppHelper.ts @@ -42,7 +42,7 @@ export class ContainerAppHelper { optionalCmdArgs.forEach(function (val: string) { command += ` ${val}`; }); - await cpExec(`${command}`); + await cpExec({command}); } catch (err) { core.error(err.message); throw err;