Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "https://deno.land/std@0.133.0/path/mod.ts";
import { SupportedOs } from "../types/platforms.ts";
import { darwinUnpack, unzip } from "./unpacker.ts";
import { chomod } from "../utils/os.ts";
import osUtils from "../utils/os.ts";

export class Downloader {
private os: string;
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Downloader {
}
private async setExecutable(binaryPath: string) {
const allowExecOctal = 0o755;
await chomod(binaryPath, allowExecOctal);
await osUtils.chomod(binaryPath, allowExecOctal);
}
/**
* Setup stackql binary, check if binary exists, if not download it
Expand Down
58 changes: 54 additions & 4 deletions src/stackql.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { assertStringIncludes } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { StackQL } from "./stackql.ts";
import { removeStackQLDownload, startStackQLServer } from "../testing/utils.ts";
import { startStackQLServer } from "../testing/utils.ts";
import {
assertEquals,
assertExists,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
import { Downloader } from "./services/downloader.ts";
import {
assertSpyCall,
spy,
} from "https://deno.land/std@0.207.0/testing/mock.ts";
import osUtils from "./utils/os.ts";
import { assert } from "https://deno.land/std@0.133.0/_util/assert.ts";

const downloader = new Downloader();

const setupStackQL = async () => {
await downloader.setupStackQL();
};

Deno.test("StackQL CLI run query", async () => {
await setupStackQL();

Deno.test("StackQL runQuery - Successful Execution", async () => {
// Arrange
await removeStackQLDownload();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });

const pullQuery = "REGISTRY PULL github;";
const providerQuery = "SHOW PROVIDERS";
const githubTestQuery =
Expand All @@ -28,7 +43,42 @@ Deno.test("StackQL runQuery - Successful Execution", async () => {
assertStringIncludes(githubResult, "stackql");
});

Deno.test("StackQL runServerQuery - Successful Execution", async () => {
Deno.test("Set properties from configs", async () => {
await setupStackQL();
const runCliSpy = spy(osUtils, "runCommand");
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
maxResults: 100,
pageLimit: 10,
maxDepth: 5,
apiTimeout: 5000,
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;

await stackQL.runQuery(githubTestQuery);

const params = stackQL.getParams();
assertEquals(params.length, 8);
assertEquals(params, [
"--http.response.maxResults",
"100",
"--http.response.pageLimit",
"10",
"--indirect.depth.max",
"5",
"--apirequesttimeout",
"5000",
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCliSpy, 0, {
args: [binaryPath, ["exec", githubTestQuery, ...params]],
});
});

Deno.test("StackQL runServerQuery", async () => {
const { closeProcess } = await startStackQLServer();
const stackQL = new StackQL();

Expand Down
64 changes: 47 additions & 17 deletions src/stackql.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./services/downloader.ts";
import { fileExists } from "./utils/os.ts";
import osUtils from "./utils/os.ts";
import { Server } from "./services/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/client.ts";

export interface StackQLConfig {
binaryPath?: string;
serverMode?: boolean;
connectionString?: string;
maxResults?: number;
pageLimit?: number;
maxDepth?: number;
apiTimeout?: number;
}

export class StackQL {
Expand All @@ -19,40 +23,66 @@ export class StackQL {
private params: string[] = [];
constructor() {
}

getParams() {
return this.params;
}

getBinaryPath() {
return this.binaryPath;
}

public async initialize(config: StackQLConfig) {
this.binaryPath = config.binaryPath;
this.serverMode = config.serverMode || false;
if (this.serverMode) {
await this.setupConnection(config.connectionString);
return;
}
if (this.binaryPath && fileExists(this.binaryPath)) {
if (this.binaryExist()) {
return;
}
this.binaryPath = await this.downloader.setupStackQL();
this.setProperties(config);
}
private binaryExist() {
return !!this.binaryPath && osUtils.fileExists(this.binaryPath);
}

private setProperties(config: StackQLConfig): void {
if (config.maxResults !== undefined) {
this.params.push("--http.response.maxResults");
this.params.push(config.maxResults.toString());
}

if (config.pageLimit !== undefined) {
this.params.push("--http.response.pageLimit");
this.params.push(config.pageLimit.toString());
}

if (config.maxDepth !== undefined) {
this.params.push("--indirect.depth.max");
this.params.push(config.maxDepth.toString());
}

if (config.apiTimeout !== undefined) {
this.params.push("--apirequesttimeout");
this.params.push(config.apiTimeout.toString());
}
}

public async runQuery(query: string) {
assertExists(this.binaryPath);
const args = ["exec", query].concat(this.params);
const process = new Deno.Command(this.binaryPath, {
args,
stdout: "piped",
stderr: "piped",
});

const { code, stdout, stderr } = await process.output();

if (code === 0) {
const output = stdout;
const result = new TextDecoder().decode(output);
try {
const result = await osUtils.runCommand(this.binaryPath, args);
return result;
} else {
const errorOutput = stderr;
const errorMessage = new TextDecoder().decode(errorOutput);
throw new Error(`StackQL query failed: ${errorMessage}`);
} catch (error) {
console.error(error);
throw new Error(`StackQL query failed: ${error.message}`);
}
}

//////////////////////Server mode related methods
private async queryObjectFormat(query: string) {
assertExists(this.connection);
Expand Down
24 changes: 24 additions & 0 deletions src/utils/os.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
assertRejects,
assertStringIncludes,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
import osUtils from "./os.ts";

Deno.test("osUtils.runCommand: Test Successful Execution", async () => {
const command = Deno.build.os === "windows" ? "cmd" : "echo";
const args = Deno.build.os === "windows"
? ["/c", "echo", "Hello, World!"]
: ["Hello, World!"];
const result = await osUtils.runCommand(command, args);
assertStringIncludes(result.trim(), "Hello, World!");
});

Deno.test("osUtils.runCommand: Test Failed Execution", () => {
const command = "invalid";
const args = Deno.build.os === "windows"
? ["/c", "echo", "Hello, World!"]
: ["Hello, World!"];
assertRejects(async () => {
await osUtils.runCommand(command, args);
});
});
32 changes: 30 additions & 2 deletions src/utils/os.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const fileExists = (path?: string) => {
const fileExists = (path?: string) => {
if (!path) {
return false;
}
Expand All @@ -13,8 +13,36 @@ export const fileExists = (path?: string) => {
}
};

export const chomod = async (path: string, mode: number) => {
const chomod = async (path: string, mode: number) => {
if (Deno.build.os !== "windows") {
await Deno.chmod(path, mode);
}
};

const runCommand = async (path: string, args: string[]) => {
const process = new Deno.Command(path, {
args,
stdout: "piped",
stderr: "piped",
});

const { code, stdout, stderr } = await process.output();

if (code === 0) {
const output = stdout;
const result = new TextDecoder().decode(output);
return result;
} else {
const errorOutput = stderr;
const errorMessage = new TextDecoder().decode(errorOutput);
throw new Error(errorMessage);
}
};

const osUtils = {
fileExists,
chomod,
runCommand,
};

export default osUtils;