Remove import cycle for CLI server

This commit is contained in:
Koen Vlaswinkel
2024-01-05 17:00:18 +01:00
parent 2ce6fd3bb1
commit 31c90a5068
3 changed files with 53 additions and 48 deletions

View File

@@ -0,0 +1,50 @@
import { execFile } from "child_process";
import { promisify } from "util";
import type { BaseLogger } from "../common/logging";
import type { ProgressReporter } from "../common/logging/vscode";
import { getChildProcessErrorMessage } from "../common/helpers-pure";
/**
* Flags to pass to all cli commands.
*/
export const LOGGING_FLAGS = ["-v", "--log-to-stderr"];
/**
* Runs a CodeQL CLI command without invoking the CLI server, returning the output as a string.
* @param codeQlPath The path to the CLI.
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
* @param commandArgs The arguments to pass to the `codeql` command.
* @param description Description of the action being run, to be shown in log and error messages.
* @param logger Logger to write command log messages, e.g. to an output channel.
* @param progressReporter Used to output progress messages, e.g. to the status bar.
* @returns The contents of the command's stdout, if the command succeeded.
*/
export async function runCodeQlCliCommand(
codeQlPath: string,
command: string[],
commandArgs: string[],
description: string,
logger: BaseLogger,
progressReporter?: ProgressReporter,
): Promise<string> {
// Add logging arguments first, in case commandArgs contains positional parameters.
const args = command.concat(LOGGING_FLAGS).concat(commandArgs);
const argsString = args.join(" ");
try {
if (progressReporter !== undefined) {
progressReporter.report({ message: description });
}
void logger.log(
`${description} using CodeQL CLI: ${codeQlPath} ${argsString}...`,
);
const result = await promisify(execFile)(codeQlPath, args);
void logger.log(result.stderr);
void logger.log("CLI command succeeded.");
return result.stdout;
} catch (err) {
throw new Error(
`${description} failed: ${getChildProcessErrorMessage(err)}`,
);
}
}

View File

@@ -1,6 +1,6 @@
import type { SemVer } from "semver";
import { parse } from "semver";
import { runCodeQlCliCommand } from "./cli";
import { runCodeQlCliCommand } from "./cli-command";
import type { Logger } from "../common/logging";
import { getErrorMessage } from "../common/helpers-pure";

View File

@@ -1,14 +1,13 @@
import { EOL } from "os";
import { spawn } from "child-process-promise";
import type { ChildProcessWithoutNullStreams } from "child_process";
import { execFile, spawn as spawnChildProcess } from "child_process";
import { spawn as spawnChildProcess } from "child_process";
import { readFile } from "fs-extra";
import { delimiter, dirname, join } from "path";
import type { Log } from "sarif";
import { SemVer } from "semver";
import type { Readable } from "stream";
import tk from "tree-kill";
import { promisify } from "util";
import type { CancellationToken, Disposable, Uri } from "vscode";
import type {
@@ -21,7 +20,6 @@ import type { DistributionProvider } from "./distribution";
import { FindDistributionResultKind } from "./distribution";
import {
assertNever,
getChildProcessErrorMessage,
getErrorMessage,
getErrorStack,
} from "../common/helpers-pure";
@@ -35,6 +33,7 @@ import type { App } from "../common/app";
import { QueryLanguage } from "../common/query-language";
import { LINE_ENDINGS, splitStreamAtSeparators } from "../common/split-stream";
import type { Position } from "../query-server/messages";
import { LOGGING_FLAGS } from "./cli-command";
/**
* The version of the SARIF format that we are using.
@@ -46,11 +45,6 @@ const SARIF_FORMAT = "sarifv2.1.0";
*/
const CSV_FORMAT = "csv";
/**
* Flags to pass to all cli commands.
*/
const LOGGING_FLAGS = ["-v", "--log-to-stderr"];
/**
* The expected output of `codeql resolve queries --format bylanguage`.
*/
@@ -1632,45 +1626,6 @@ export function spawnServer(
return child;
}
/**
* Runs a CodeQL CLI command without invoking the CLI server, returning the output as a string.
* @param codeQlPath The path to the CLI.
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
* @param commandArgs The arguments to pass to the `codeql` command.
* @param description Description of the action being run, to be shown in log and error messages.
* @param logger Logger to write command log messages, e.g. to an output channel.
* @param progressReporter Used to output progress messages, e.g. to the status bar.
* @returns The contents of the command's stdout, if the command succeeded.
*/
export async function runCodeQlCliCommand(
codeQlPath: string,
command: string[],
commandArgs: string[],
description: string,
logger: Logger,
progressReporter?: ProgressReporter,
): Promise<string> {
// Add logging arguments first, in case commandArgs contains positional parameters.
const args = command.concat(LOGGING_FLAGS).concat(commandArgs);
const argsString = args.join(" ");
try {
if (progressReporter !== undefined) {
progressReporter.report({ message: description });
}
void logger.log(
`${description} using CodeQL CLI: ${codeQlPath} ${argsString}...`,
);
const result = await promisify(execFile)(codeQlPath, args);
void logger.log(result.stderr);
void logger.log("CLI command succeeded.");
return result.stdout;
} catch (err) {
throw new Error(
`${description} failed: ${getChildProcessErrorMessage(err)}`,
);
}
}
/**
* Log a text stream to a `Logger` interface.
* @param stream The stream to log.