Skip to content

Commit

Permalink
styra-link: cleanup
Browse files Browse the repository at this point in the history
Duh; do not need to include the binary download in the loop at all.
  • Loading branch information
msorens committed Apr 17, 2024
1 parent f0674d4 commit f3a5753
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
37 changes: 19 additions & 18 deletions src/lib/styra-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export class StyraInstall {
}

static async checkCliInstallation(): Promise<boolean> {
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<boolean> {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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})`);
Expand Down Expand Up @@ -172,25 +177,21 @@ export class StyraInstall {
: `${prefix}/linux/amd64/styra`;
}

private static async installStyra(trials = 0): Promise<string> {
private static async downloadBinary(tempFileLocation: string): Promise<void> {

const tempFileLocation = path.join(os.homedir(), this.BinaryFile);
const url = this.getDownloadUrl();

await IDE.withProgress({
location: IDE.ProgressLocation.Notification,
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) {
Expand Down Expand Up @@ -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
});
}
}
14 changes: 7 additions & 7 deletions src/test-jest/lib/styra-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(''));

Expand All @@ -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'}));

Expand All @@ -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/);
Expand Down

0 comments on commit f3a5753

Please sign in to comment.