Skip to content

Commit

Permalink
change to AzPSUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
MoChilia committed Dec 28, 2023
1 parent d2ec8a1 commit 25ca50a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
9 changes: 4 additions & 5 deletions __tests__/PowerShell/AzPSLogin.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as os from 'os';

import { AzPSLogin } from '../../src/PowerShell/AzPSLogin';
import AzPSConfig from '../../src/PowerShell/AzPSConfig';
import { LoginConfig } from '../../src/common/LoginConfig';
import AzPSConstants from '../../src/PowerShell/AzPSConstants';
import { AzPSConstants, AzPSUtils } from '../../src/PowerShell/AzPSUtils';

let azpsLogin: AzPSLogin;
jest.setTimeout(30000);
Expand Down Expand Up @@ -37,7 +36,7 @@ describe('Testing login', () => {

describe('Testing set module path', () => {
test('setDefaultPSModulePath should work', () => {
AzPSConfig.setPSModulePathForGitHubRunner();
AzPSUtils.setPSModulePathForGitHubRunner();
const runner: string = process.env.RUNNER_OS || os.type();
if(runner.toLowerCase() === "linux"){
expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX);
Expand All @@ -64,7 +63,7 @@ describe('Testing runPSScript', () => {
}
return ConvertTo-Json $output`;

let psVersion: string = await AzPSConfig.runPSScript(script);
let psVersion: string = await AzPSUtils.runPSScript(script);
expect(psVersion === null).toBeFalsy();
});

Expand All @@ -83,7 +82,7 @@ describe('Testing runPSScript', () => {
return ConvertTo-Json $output`;

try{
await AzPSConfig.runPSScript(script);
await AzPSUtils.runPSScript(script);
throw new Error("The last step should fail.");
}catch(error){
expect(error.message.includes("Azure PowerShell login failed with error: You cannot call a method on a null-valued expression.")).toBeTruthy();
Expand Down
7 changes: 0 additions & 7 deletions src/PowerShell/AzPSConstants.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/PowerShell/AzPSLogin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';

import AzPSScriptBuilder from './AzPSScriptBuilder';
import AzPSConfig from './AzPSConfig';
import { AzPSUtils } from './AzPSUtils';
import { LoginConfig } from '../common/LoginConfig';

export class AzPSLogin {
Expand All @@ -13,12 +13,12 @@ export class AzPSLogin {

async login() {
core.info(`Running Azure PowerShell Login.`);
AzPSConfig.setPSModulePathForGitHubRunner();
await AzPSConfig.importLatestAzAccounts();
AzPSUtils.setPSModulePathForGitHubRunner();
await AzPSUtils.importLatestAzAccounts();
const [loginMethod, loginScript] = await AzPSScriptBuilder.getAzPSLoginScript(this.loginConfig);
core.info(`Attempting Azure PowerShell login by using ${loginMethod}...`);
core.debug(`Azure PowerShell Login Script: ${loginScript}`);
await AzPSConfig.runPSScript(loginScript);
await AzPSUtils.runPSScript(loginScript);
console.log(`Running Azure PowerShell Login successfully.`);
}
}
1 change: 0 additions & 1 deletion src/PowerShell/AzPSScriptBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AzPSConstants from "./AzPSConstants";
import { LoginConfig } from '../common/LoginConfig';

export default class AzPSScriptBuilder {
Expand Down
18 changes: 12 additions & 6 deletions src/PowerShell/AzPSConfig.ts → src/PowerShell/AzPSUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as os from 'os';
import * as path from 'path';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import AzPSConstants from './AzPSConstants';
import AzPSScriptBuilder from './AzPSScriptBuilder';

interface PSResultType {
Expand All @@ -12,16 +11,23 @@ interface PSResultType {
Error: string;
}

export default class AzPSConfig {
export class AzPSConstants {
static readonly DEFAULT_AZ_PATH_ON_LINUX: string = '/usr/share';
static readonly DEFAULT_AZ_PATH_ON_WINDOWS: string = 'C:\\Modules';
static readonly AzAccounts: string = "Az.Accounts";
static readonly PowerShell_CmdName = "pwsh";
}

export class AzPSUtils {
static async setPSModulePathForGitHubRunner() {
const runner: string = process.env.RUNNER_OS || os.type();
switch (runner.toLowerCase()) {
case "linux":
AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX);
AzPSUtils.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX);
break;
case "windows":
case "windows_nt":
AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS);
AzPSUtils.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS);
break;
case "macos":
case "darwin":
Expand All @@ -41,7 +47,7 @@ export default class AzPSConfig {
static async importLatestAzAccounts() {
let importLatestAccountsScript: string = AzPSScriptBuilder.getImportLatestModuleScript(AzPSConstants.AzAccounts);
core.debug(`The script to import the latest Az.Accounts: ${importLatestAccountsScript}`);
let azAccountsPath: string = await AzPSConfig.runPSScript(importLatestAccountsScript);
let azAccountsPath: string = await AzPSUtils.runPSScript(importLatestAccountsScript);
core.debug(`The latest Az.Accounts used: ${azAccountsPath}`);
}

Expand Down Expand Up @@ -76,4 +82,4 @@ export default class AzPSConfig {
}
return result.Result;
}
}
}
7 changes: 3 additions & 4 deletions src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as crypto from 'crypto';
import AzPSConstants from '../PowerShell/AzPSConstants';
import AzPSConfig from '../PowerShell/AzPSConfig';
import { AzPSConstants, AzPSUtils } from '../PowerShell/AzPSUtils';

export function setUserAgent(): void {
let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
Expand All @@ -29,8 +28,8 @@ export async function cleanupAzPSAccounts(): Promise<void> {
}
core.debug(`PowerShell path: ${psPath}`);
core.debug("Importing Azure PowerShell module.");
AzPSConfig.setPSModulePathForGitHubRunner();
await AzPSConfig.importLatestAzAccounts();
AzPSUtils.setPSModulePathForGitHubRunner();
await AzPSUtils.importLatestAzAccounts();
core.info("Clearing azure powershell accounts from the local cache.");
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "Process"]);
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "CurrentUser", "-Force", "-ErrorAction", "SilentlyContinue"]);
Expand Down

0 comments on commit 25ca50a

Please sign in to comment.