diff --git a/src/lib/styra-install.ts b/src/lib/styra-install.ts index a18fe8a..92c9ad1 100644 --- a/src/lib/styra-install.ts +++ b/src/lib/styra-install.ts @@ -45,12 +45,12 @@ export class StyraInstall { } static async checkCliInstallation(): Promise { - if (await StyraInstall.styraCmdExists()) { + if (await this.styraCmdExists()) { infoDebug('Styra CLI is installed'); return true; } info('Styra CLI is not installed'); - return await StyraInstall.promptForInstall('is not installed', 'installation'); + return await this.promptForInstall('is not installed', 'installation'); } private static async promptForInstall(description: string, operation: string): Promise { @@ -60,13 +60,18 @@ export class StyraInstall { `Styra CLI ${description}. Would you like to install it now?`, 'Install'); if (selection === 'Install') { + const tempFile = path.join(os.homedir(), this.BinaryFile); info('Installing Styra CLI. This may take a few minutes...'); - let trials = 0; + try { + await this.downloadBinary(tempFile); + } catch (err) { + teeError(`CLI ${operation} failed: ${(err as Error).message}`); + return false; + } // eslint-disable-next-line no-constant-condition while (true) { try { - const tempFile = await this.installStyra(trials++); - await StyraInstall.installOnPath(tempFile); + await this.installOnPath(tempFile); teeInfo(`CLI ${operation} completed.`); return true; } catch (err) { @@ -97,7 +102,7 @@ export class StyraInstall { const available = await DAS.runQuery('/v1/system/version') as VersionType; const installedVersion = await this.getInstalledCliVersion(); if (compare(available.cliVersion, installedVersion) === 1) { - await StyraInstall.promptForInstall( + await this.promptForInstall( `has an update available (installed=${installedVersion}, available=${available.cliVersion})`, 'update'); } } catch (err) { @@ -111,7 +116,7 @@ export class StyraInstall { const versionInfo = await DAS.runQuery('/v1/system/version') as VersionType; infoDebug(`DAS release: ${versionInfo.release} `); infoDebug(`DAS edition: ${versionInfo.dasEdition} `); - const cliVersion = await StyraInstall.getInstalledCliVersion(); + const cliVersion = await this.getInstalledCliVersion(); infoDebug(`CLI version: ${cliVersion} `); if (cliVersion !== versionInfo.cliVersion) { infoDebug(`(Latest CLI version: ${versionInfo.cliVersion})`); @@ -172,9 +177,8 @@ export class StyraInstall { : `${prefix}/linux/amd64/styra`; } - private static async installStyra(trials = 0): Promise { + private static async downloadBinary(tempFileLocation: string): Promise { - const tempFileLocation = path.join(os.homedir(), this.BinaryFile); const url = this.getDownloadUrl(); await IDE.withProgress({ @@ -182,15 +186,12 @@ export class StyraInstall { title: 'Installing Styra CLI', cancellable: false }, async () => { - if (trials === 0) { - await this.getBinary(url, tempFileLocation); - info(` Platform: ${process.platform}`); - info(` Architecture: ${process.arch}`); - info(` Executable: ${this.ExeFile}`); - fs.chmodSync(tempFileLocation, '755'); - } + await this.getBinary(url, tempFileLocation); + info(` Platform: ${process.platform}`); + info(` Architecture: ${process.arch}`); + info(` Executable: ${this.ExeFile}`); + fs.chmodSync(tempFileLocation, '755'); }); - return tempFileLocation; } private static async installOnPath(tempFileLocation: string) { @@ -277,7 +278,7 @@ export class StyraInstall { value: state.pwd ?? '', prompt: `Enter admin password to install into ${STD_LINUX_INSTALL_DIR}`, validate: validateNoop, - shouldResume + shouldResume // TODO: override and delete temp file }); } } diff --git a/src/test-jest/lib/styra-install.test.ts b/src/test-jest/lib/styra-install.test.ts index d32b6ba..452a0cd 100644 --- a/src/test-jest/lib/styra-install.test.ts +++ b/src/test-jest/lib/styra-install.test.ts @@ -38,7 +38,7 @@ describe('StyraInstall', () => { } beforeEach(() => { - setPrivateMock('installStyra', jest.fn().mockResolvedValue('')); + setPrivateMock('downloadBinary', jest.fn().mockResolvedValue('')); setPrivateMock('installOnPath', jest.fn().mockResolvedValue('')); StyraInstall.styraCmdExists = jest.fn().mockResolvedValue(false); IDE.getConfigValue = mockVSCodeSettings(); @@ -95,9 +95,9 @@ describe('StyraInstall', () => { }); }); - test('returns true and succeeds if installStyra gets a bad pwd then a good pwd', async () => { + test('returns true and succeeds if get bad pwd then a good pwd', async () => { IDE.showInformationMessageModal = jest.fn().mockReturnValue('Install'); - setPrivateMock('installStyra', jest.fn() + setPrivateMock('installOnPath', jest.fn() .mockRejectedValueOnce({message: 'Sorry, try again. Bad password'}) .mockResolvedValueOnce('')); @@ -106,9 +106,9 @@ describe('StyraInstall', () => { expect(spy.content).toMatch(/CLI installation completed/); }); - test('returns false and fails if installStyra gets bad pwd, then some other error', async () => { + test('returns false and fails if get bad pwd, then some other error', async () => { IDE.showInformationMessageModal = jest.fn().mockReturnValue('Install'); - setPrivateMock('installStyra', jest.fn() + setPrivateMock('installOnPath', jest.fn() .mockRejectedValueOnce({message: 'Sorry, try again. Bad password'}) .mockRejectedValue({message: 'some error'})); @@ -118,9 +118,9 @@ describe('StyraInstall', () => { expect(spy.content).toMatch(/some error/); }); - test('returns false and fails if installStyra throws an error other than bad pwd', async () => { + test('returns false and fails if throws an error other than bad pwd', async () => { IDE.showInformationMessageModal = jest.fn().mockReturnValue('Install'); - setPrivateMock('installStyra', jest.fn().mockRejectedValue({message: 'some error'})); + setPrivateMock('downloadBinary', jest.fn().mockRejectedValue({message: 'some error'})); expect(await StyraInstall.checkCliInstallation()).toBe(false); expect(spy.content).toMatch(/CLI installation failed/);