Enable specifying extraction destination

This commit is contained in:
Henry Mercer
2024-11-06 19:47:23 +00:00
parent af49565b85
commit be26fe61b5
6 changed files with 29 additions and 24 deletions

View File

@@ -105,13 +105,16 @@ export async function extract(
// Defensively continue to call the toolcache API as requesting a gzipped
// bundle may be a fallback option.
return await toolcache.extractTar(tarPath);
case "zstd":
case "zstd": {
if (!tarVersion) {
throw new Error(
"Could not determine tar version, which is required to extract a Zstandard archive.",
);
}
return await extractTarZst(tarPath, tarVersion, logger);
const dest = await createExtractFolder();
await extractTarZst(tarPath, dest, tarVersion, logger);
return dest;
}
}
}
@@ -119,15 +122,14 @@ export async function extract(
* Extract a compressed tar archive
*
* @param tar tar stream, or path to the tar
* @param dest destination directory. Optional.
* @returns path to the destination directory
* @param dest destination directory
*/
export async function extractTarZst(
tar: stream.Readable | string,
dest: string,
tarVersion: TarVersion,
logger: Logger,
): Promise<string> {
const dest = await createExtractFolder();
): Promise<void> {
logger.debug(
`Extracting to ${dest}.${
tar instanceof stream.Readable
@@ -184,15 +186,13 @@ export async function extractTarZst(
resolve();
});
});
return dest;
} catch (e) {
await cleanUpGlob(dest, "extraction destination directory", logger);
throw e;
}
}
async function createExtractFolder(): Promise<string> {
export async function createExtractFolder(): Promise<string> {
const dest = path.join(getTemporaryDirectory(), uuidV4());
fs.mkdirSync(dest, { recursive: true });
return dest;

View File

@@ -99,8 +99,10 @@ export async function downloadAndExtract(
logger.info(`Streaming the extraction of the CodeQL bundle.`);
const toolsInstallStart = performance.now();
const extractedBundlePath = await downloadAndExtractZstdWithStreaming(
const extractedBundlePath = await tar.createExtractFolder();
await downloadAndExtractZstdWithStreaming(
codeqlURL,
extractedBundlePath,
authorization,
headers,
tarVersion!,
@@ -180,11 +182,12 @@ export async function downloadAndExtract(
async function downloadAndExtractZstdWithStreaming(
codeqlURL: string,
dest: string,
authorization: string | undefined,
headers: OutgoingHttpHeaders,
tarVersion: tar.TarVersion,
logger: Logger,
): Promise<string> {
): Promise<void> {
headers = Object.assign(
{ "User-Agent": "CodeQL Action" },
authorization ? { authorization } : {},
@@ -207,7 +210,7 @@ async function downloadAndExtractZstdWithStreaming(
);
}
return await tar.extractTarZst(response, tarVersion, logger);
await tar.extractTarZst(response, dest, tarVersion, logger);
}
function sanitizeUrlForStatusReport(url: string): string {