-
Notifications
You must be signed in to change notification settings - Fork 475
Create wrapper runCliJson
#3981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -514,16 +514,13 @@ async function getCodeQLForCmd( | |
| async getVersion() { | ||
| let result = util.getCachedCodeQlVersion(cmd); | ||
| if (result === undefined) { | ||
| const output = await runCli(cmd, ["version", "--format=json"], { | ||
| noStreamStdout: true, | ||
| }); | ||
| try { | ||
| result = JSON.parse(output) as VersionInfo; | ||
| } catch { | ||
| throw Error( | ||
| `Invalid JSON output from \`version --format=json\`: ${output}`, | ||
| ); | ||
| } | ||
| result = await runCliJson<VersionInfo>( | ||
| cmd, | ||
| ["version", "--format=json"], | ||
| { | ||
| noStreamStdout: true, | ||
| }, | ||
| ); | ||
| util.cacheCodeQlVersion(cmd, result); | ||
| } | ||
| return result; | ||
|
|
@@ -731,26 +728,20 @@ async function getCodeQLForCmd( | |
| filterToLanguagesWithQueries: boolean; | ||
| } = { filterToLanguagesWithQueries: false }, | ||
| ) { | ||
| const codeqlArgs = [ | ||
| return runCliJson<ResolveLanguagesOutput>(cmd, [ | ||
| "resolve", | ||
| "languages", | ||
| "--format=betterjson", | ||
| "--extractor-options-verbosity=4", | ||
| "--extractor-include-aliases", | ||
| // TODO: Unconditionally include `--filter-to-languages-with-queries` | ||
| // once CODEQL_MINIMUM_VERSION is at least v2.23.0 | ||
| // — the first version to support this flag. | ||
|
Comment on lines
+737
to
+739
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elaborating on my comment on the other PR, what does this comment have to do with the changes here? Just a drive-by improvement?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nothing, really—a "drive-by improvement" is a nice way to word it. But, it seemed more relevant here than in #3950. I'm fine to delete it, but I think it adds some value. |
||
| ...(filterToLanguagesWithQueries | ||
| ? ["--filter-to-languages-with-queries"] | ||
| : []), | ||
| ...getExtraOptionsFromEnv(["resolve", "languages"]), | ||
| ]; | ||
| const output = await runCli(cmd, codeqlArgs); | ||
|
|
||
| try { | ||
| return JSON.parse(output) as ResolveLanguagesOutput; | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Unexpected output from codeql resolve languages with --format=betterjson: ${e}`, | ||
| ); | ||
| } | ||
| ]); | ||
| }, | ||
| async resolveBuildEnvironment( | ||
| workingDir: string | undefined, | ||
|
|
@@ -766,15 +757,7 @@ async function getCodeQLForCmd( | |
| if (workingDir !== undefined) { | ||
| codeqlArgs.push("--working-dir", workingDir); | ||
| } | ||
| const output = await runCli(cmd, codeqlArgs); | ||
|
|
||
| try { | ||
| return JSON.parse(output) as ResolveBuildEnvironmentOutput; | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Unexpected output from codeql resolve build-environment: ${e} in\n${output}`, | ||
| ); | ||
| } | ||
| return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs); | ||
| }, | ||
| async databaseRunQueries( | ||
| databasePath: string, | ||
|
|
@@ -976,15 +959,9 @@ async function getCodeQLForCmd( | |
| ...getExtraOptionsFromEnv(["resolve", "queries"]), | ||
| ...queries, | ||
| ]; | ||
| const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true }); | ||
|
|
||
| try { | ||
| return JSON.parse(output) as string[]; | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Unexpected output from codeql resolve queries --format=startingpacks: ${e}`, | ||
| ); | ||
| } | ||
| return await runCliJson<string[]>(cmd, codeqlArgs, { | ||
| noStreamStdout: true, | ||
| }); | ||
| }, | ||
| async resolveDatabase( | ||
| databasePath: string, | ||
|
|
@@ -996,15 +973,9 @@ async function getCodeQLForCmd( | |
| "--format=json", | ||
| ...getExtraOptionsFromEnv(["resolve", "database"]), | ||
| ]; | ||
| const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true }); | ||
|
|
||
| try { | ||
| return JSON.parse(output) as ResolveDatabaseOutput; | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Unexpected output from codeql resolve database --format=json: ${e}`, | ||
| ); | ||
| } | ||
| return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, { | ||
| noStreamStdout: true, | ||
| }); | ||
| }, | ||
| async mergeResults( | ||
| sarifFiles: string[], | ||
|
|
@@ -1160,6 +1131,20 @@ async function runCli( | |
| } | ||
| } | ||
|
|
||
| async function runCliJson<T>( | ||
|
mario-campos marked this conversation as resolved.
|
||
| cmd: string, | ||
| args: string[] = [], | ||
| opts: { stdin?: string; noStreamStdout?: boolean } = {}, | ||
| ): Promise<T> { | ||
|
mario-campos marked this conversation as resolved.
|
||
| const output = await runCli(cmd, args, opts); | ||
| try { | ||
| return JSON.parse(output) as T; | ||
| } catch { | ||
| const command = [cmd, ...args].join(" "); | ||
| throw Error(`Invalid JSON output from \`${command}\`: ${output}`); | ||
|
mario-campos marked this conversation as resolved.
Outdated
|
||
| } | ||
|
mario-campos marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Writes the code scanning configuration that is to be used by the CLI. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the
--format=jsonor--format=betterjsonargument be added automatically byrunCliJson?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be nice! Unfortunately, there doesn't seem to be consistency between all of the calls of
runCliJson:getVersion,resolveDatabaseuse--format=jsonresolveLanguagesuses--format=betterjsonresolveBuildEnvironmentuses nothing??resolveQueriesStartingPacksuses--format=startingpacks