Skip to content

Commit 26dc552

Browse files
authored
Merge pull request #40 from GitGuardian/jgriffe/-/move-run-ggshield-command-to-own-file
chore: move runGGShieldCommand to its own file
2 parents 60aa5df + c6df1b7 commit 26dc552

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

src/lib/ggshield-api.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,16 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22
import {
33
SpawnOptionsWithoutStdio,
4-
SpawnSyncOptionsWithStringEncoding,
5-
SpawnSyncReturns,
64
spawn,
7-
spawnSync,
85
} from "child_process";
9-
import { workspace, window, WebviewView } from "vscode";
6+
import { window, WebviewView } from "vscode";
107
import axios from 'axios';
118
import { GGShieldConfiguration } from "./ggshield-configuration";
129
import { GGShieldScanResults } from "./api-types";
1310
import * as os from "os";
1411
import { apiToDashboard, dasboardToApi } from "../utils";
12+
import { runGGShieldCommand } from "./run-ggshield";
1513

16-
/**
17-
* Run ggshield CLI application with specified arguments
18-
*
19-
* @param configuration ggshield configuration
20-
* @param args arguments
21-
* @returns
22-
*/
23-
export function runGGShieldCommand(
24-
configuration: GGShieldConfiguration,
25-
args: string[]
26-
): SpawnSyncReturns<string> {
27-
const { ggshieldPath, apiUrl, apiKey } = configuration;
28-
let env: {
29-
GITGUARDIAN_API_URL: string;
30-
GG_USER_AGENT: string;
31-
GITGUARDIAN_DONT_LOAD_ENV: string;
32-
GITGUARDIAN_API_KEY?: string;
33-
} = {
34-
GITGUARDIAN_API_URL: apiUrl,
35-
GG_USER_AGENT: "gitguardian-vscode",
36-
GITGUARDIAN_DONT_LOAD_ENV: "true",
37-
};
38-
39-
if (apiKey) {
40-
env = {
41-
...env,
42-
// eslint-disable-next-line @typescript-eslint/naming-convention
43-
GITGUARDIAN_API_KEY: apiKey,
44-
};
45-
}
46-
47-
let options: SpawnSyncOptionsWithStringEncoding = {
48-
cwd: os.tmpdir(),
49-
env: env,
50-
encoding: "utf-8",
51-
windowsHide: true,
52-
};
53-
54-
// If the command is executed in a worskpace, execute ggshield from the root folder so .gitguardian.yaml is used
55-
if (workspace.workspaceFolders?.length || 0 > 0) {
56-
options["cwd"] = workspace.workspaceFolders![0].uri.fsPath;
57-
}
58-
let proc = spawnSync(ggshieldPath, args, options);
59-
60-
return proc;
61-
}
6214

6315
/**
6416
* Display API quota

src/lib/ggshield-resolver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import * as vscode from "vscode";
22
import {
3-
getConfiguration,
43
GGShieldConfiguration,
54
} from "./ggshield-configuration";
6-
import { runGGShieldCommand } from "./ggshield-api";
5+
import { runGGShieldCommand } from "./run-ggshield";
76
import { window } from "vscode";
8-
import * as os from "os";
97

108
export class GGShieldResolver {
119
channel: vscode.OutputChannel;

src/lib/run-ggshield.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { spawnSync, SpawnSyncOptionsWithStringEncoding, SpawnSyncReturns } from "child_process";
2+
import { GGShieldConfiguration } from "./ggshield-configuration";
3+
import { workspace } from "vscode";
4+
5+
import * as os from "os";
6+
/**
7+
* Run ggshield CLI application with specified arguments
8+
*
9+
* @param configuration ggshield configuration
10+
* @param args arguments
11+
* @returns
12+
*/
13+
export function runGGShieldCommand(
14+
configuration: GGShieldConfiguration,
15+
args: string[]
16+
): SpawnSyncReturns<string> {
17+
const { ggshieldPath, apiUrl, apiKey } = configuration;
18+
let env: {
19+
GITGUARDIAN_API_URL: string;
20+
GG_USER_AGENT: string;
21+
GITGUARDIAN_DONT_LOAD_ENV: string;
22+
GITGUARDIAN_API_KEY?: string;
23+
} = {
24+
GITGUARDIAN_API_URL: apiUrl,
25+
GG_USER_AGENT: "gitguardian-vscode",
26+
GITGUARDIAN_DONT_LOAD_ENV: "true",
27+
};
28+
29+
if (apiKey) {
30+
env = {
31+
...env,
32+
// eslint-disable-next-line @typescript-eslint/naming-convention
33+
GITGUARDIAN_API_KEY: apiKey,
34+
};
35+
}
36+
37+
let options: SpawnSyncOptionsWithStringEncoding = {
38+
cwd: os.tmpdir(),
39+
env: env,
40+
encoding: "utf-8",
41+
windowsHide: true,
42+
};
43+
44+
// If the command is executed in a worskpace, execute ggshield from the root folder so .gitguardian.yaml is used
45+
if (workspace.workspaceFolders?.length || 0 > 0) {
46+
options["cwd"] = workspace.workspaceFolders![0].uri.fsPath;
47+
}
48+
let proc = spawnSync(ggshieldPath, args, options);
49+
50+
return proc;
51+
}

0 commit comments

Comments
 (0)