Skip to content

Commit

Permalink
Handling empty version string, improved messages (metalbear-co#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Razz4780 authored Aug 16, 2024
1 parent 5db9e6a commit fa9d3f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/144.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue where `mirrord.autoUpdate` set to an empty string prevented the extension from downloading mirrord binary. Improved notifications related to auto update.
58 changes: 34 additions & 24 deletions src/binaryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function getExtensionMirrordPath(): Uri {
/**
* Tries to find local mirrord in path or in extension storage.
* @param version If specified, then the version of the binary is checked and matched path is returned.
* @returns Path to mirrord binary or null if not found
* @returns (path to mirrord binary, whether it was found in $PATH) or null if not found
*/
export async function getLocalMirrordBinary(version: string | null): Promise<string | null> {
export async function getLocalMirrordBinary(version: string | null): Promise<[string, boolean] | null> {
try {
const mirrordPath = await which("mirrord");
if (version) {
Expand All @@ -35,29 +35,31 @@ export async function getLocalMirrordBinary(version: string | null): Promise<str
// if we are running on the release CI.
if ((process.env.CI_BUILD_PLUGIN === "true" && installedVersion && semver.gte(installedVersion, version)) ||
(!process.env.CI_BUILD_PLUGIN && installedVersion === version)) {
return mirrordPath;
return [mirrordPath, true];
}
} else {
return mirrordPath;
return [mirrordPath, true];
}
} catch (e) {
console.debug("couldn't find mirrord in path");
}

try {
const mirrordPath = getExtensionMirrordPath();
await workspace.fs.stat(mirrordPath);
if (version) {
const api = new MirrordAPI(mirrordPath.fsPath);
const installedVersion = await api.getBinaryVersion();
if (installedVersion === version) {
return mirrordPath.fsPath;
return [mirrordPath.fsPath, false];
}
} else {
return mirrordPath.fsPath;
return [mirrordPath.fsPath, false];
}
} catch (e) {
console.log("couldn't find mirrord in extension storage");
}

return null;
}

Expand Down Expand Up @@ -110,8 +112,8 @@ async function getConfiguredMirrordBinary(background: boolean, latestVersion: st
* @returns Path to mirrord binary
*/
export async function getMirrordBinary(background: boolean): Promise<string | null> {
let latestVersion;
let wantedVersion = null;
let latestVersion: string | null;
let wantedVersion: string | null = null;

try {
latestVersion = await getLatestSupportedVersion(background);
Expand All @@ -129,55 +131,63 @@ export async function getMirrordBinary(background: boolean): Promise<string | nu
const autoUpdateConfigured = vscode.workspace.getConfiguration().get("mirrord.autoUpdate");

// values for `mirrord.autoUpdate` can be:
// - true: auto-update is enabled
// - true or empty string: auto-update is enabled
// - false: auto-update is disabled
// - string: version to download
// - non-empty string: version to download
// example: "mirrord.autoUpdate": "3.70.1" or "mirrord.autoUpdate": false or "mirrord.autoUpdate": true

// check the type can be only null, string or boolean
if (typeof autoUpdateConfigured !== 'boolean' && typeof autoUpdateConfigured !== 'string') {
vscode.window.showErrorMessage(`Invalid autoUpdate setting ${autoUpdateConfigured}: must be a boolean or a string`);
vscode.window.showErrorMessage(
`Invalid value of mirrord.autoUpdate setting: \`${autoUpdateConfigured}\` (must be a boolean or a string)`
);
return null;
}

if (typeof autoUpdateConfigured === 'string') {
if (autoUpdateConfigured === true || autoUpdateConfigured === '') {
wantedVersion = latestVersion;
} else if (typeof autoUpdateConfigured === 'string') {
if (!semver.valid(autoUpdateConfigured)) {
vscode.window.showErrorMessage(`Invalid version format ${autoUpdateConfigured}: must follow semver format`);
vscode.window.showErrorMessage(
`Invalid value of mirrord.autoUpdate setting: \`${autoUpdateConfigured}\` (string must follow semver format)`
);
return null;
}
wantedVersion = autoUpdateConfigured;
} else if (autoUpdateConfigured === true) {
wantedVersion = latestVersion;
} else {
// any version will do
wantedVersion = null;
}

let foundLocal = await getLocalMirrordBinary(wantedVersion);

const foundLocal = await getLocalMirrordBinary(wantedVersion);
if (foundLocal) {
vscode.window.showInformationMessage(`Using mirrord binary found in path: ${foundLocal} of version ${wantedVersion}`);
return foundLocal;
const message = `Using mirrord binary found in ${foundLocal[1] ? 'path' : 'extension storage'}: \
${foundLocal[0]}${wantedVersion ? ` of version ${wantedVersion}` : ''}`;
vscode.window.showInformationMessage(message);
return foundLocal[0];
}

if (!wantedVersion) {
let anyVersion = await getLocalMirrordBinary(null);
if (anyVersion) {
vscode.window.showInformationMessage(`Version check not available/allowed and no wanted version set. Using mirrord binary found in path: ${anyVersion}`);
return anyVersion;
const message = `Version check not available/allowed and no wanted version set. \
Using mirrord binary found in ${anyVersion[1] ? 'path' : 'extension storage'}: ${anyVersion[0]}`;
vscode.window.showInformationMessage(message);
return anyVersion[0];
}

vscode.window.showErrorMessage(`Failed to find mirrord binary in path and failed to check latest supported version of mirrord binary to download`);
return null;
}

// now wantedVersion is true, meaning we haven't found it yet locally so we have to download it{
if (background) {
downloadMirrordBinary(getExtensionMirrordPath(), wantedVersion);
} else {
await downloadMirrordBinary(getExtensionMirrordPath(), wantedVersion);
}

return await getLocalMirrordBinary(wantedVersion);

const downloaded = await getLocalMirrordBinary(wantedVersion);
return downloaded ? downloaded[0] : null;
}

/**
Expand Down

0 comments on commit fa9d3f4

Please sign in to comment.