Skip to content

Commit

Permalink
Refactor - await method to get/create rg
Browse files Browse the repository at this point in the history
  • Loading branch information
snehapar9 committed Sep 18, 2023
1 parent a211aa7 commit 9c6b50f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 78 deletions.
83 changes: 49 additions & 34 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
}
Expand All @@ -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];
}
});
});
};
Expand All @@ -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:
Expand All @@ -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*/];
}
});
});
Expand All @@ -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:
Expand All @@ -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*/];
}
});
});
Expand Down Expand Up @@ -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];
Expand Down
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@ 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)) {
resourceGroup = `${containerAppName}-rg`;
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);
}
}

Expand Down
48 changes: 27 additions & 21 deletions src/CommandHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand All @@ -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];
}
});
});
};
Expand All @@ -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:
Expand All @@ -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*/];
}
});
});
Expand All @@ -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:
Expand All @@ -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*/];
}
});
});
Expand Down
11 changes: 5 additions & 6 deletions src/CommandHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export class CommandHelper {
*/
public async execCommandAsync(command: string): Promise<string> {
return os.platform() == 'win32' ?
this.execPwshCommandAsync(command) :
this.execBashCommandAsync(command);
await this.execPwshCommandAsync(command) :
await this.execBashCommandAsync(command);
}

/**
Expand All @@ -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}');
Expand Down Expand Up @@ -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}');
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerAppHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerAppHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9c6b50f

Please sign in to comment.