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
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test

on:
push:

jobs:
test:
name: Test on Multiple OS
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Run tests
run: deno test --allow-net --allow-read --allow-write --allow-env --allow-run
6 changes: 2 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { bold } from "./deps.ts";
import { StackQL } from "./src/stackql.ts";

export function getHelloWorld(): string {
return bold("Hello World");
}
export { StackQL };
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./downloader.ts";
import { removeStackQLDownload } from "../../tests/utils.ts";

Deno.test("Downloader setupStackQL Integration Test", async () => {
// Arrange
await removeStackQLDownload();
const downloader = new Downloader();
let binaryPath: string;

// Act
try {
const binaryPath = await downloader.setupStackQL();
binaryPath = await downloader.setupStackQL();

// Assert
assertExists(binaryPath);
// Check if the binary exists after setupStackQL is called

console.log(
"Test passed: setupStackQL completed without errors and binary exists.",
"Test passed: setupStackQL completed without errors and binary exists."
);
} catch (error) {
console.error("Test failed:", error);
Expand Down
42 changes: 27 additions & 15 deletions src/utils/downloader.ts → src/services/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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.ts";

export class Downloader {
private os: string;
Expand All @@ -12,11 +13,11 @@ export class Downloader {
this.arch = Deno.build.arch; // 'x86_64', 'arm64', etc.

this.urlMap = {
"linux-x86_64":
[SupportedOs.Linux]:
"https://releases.stackql.io/stackql/latest/stackql_linux_amd64.zip",
"windows-x86_64":
[SupportedOs.Windows]:
"https://releases.stackql.io/stackql/latest/stackql_windows_amd64.zip",
"darwin-x86_64":
[SupportedOs.Darwin]:
"https://storage.googleapis.com/stackql-public-releases/latest/stackql_darwin_multiarch.pkg",
// Additional OS-architecture combinations can be added here
};
Expand All @@ -28,7 +29,7 @@ export class Downloader {

try {
await res.body?.pipeTo(file.writable).finally(
() => file.close(), //TODO: fix bad resource id when closing file
() => file.close() //TODO: fix bad resource id when closing file
);
} catch (error) {
console.error(`ERROR: [downloadFile] ${error.message}`);
Expand All @@ -38,11 +39,11 @@ export class Downloader {
}

private getUrl(): string {
const key = `${this.os}-${this.arch}`;
const key = `${this.os}`;
const url = this.urlMap[key];

if (!url) {
throw new Error(`Unsupported OS type: ${this.os} ${this.arch}`);
throw new Error(`Unsupported OS type: ${this.os}`);
}

return url;
Expand Down Expand Up @@ -110,7 +111,20 @@ export class Downloader {
throw error;
}
}
private async installStackQL(downloadDir: string) {
const url = this.getUrl();

const archiveFileName = `${downloadDir}/${url.split("/").pop()}`;
await this.downloadFile(url, archiveFileName);

console.log("Unpacking stackql binary");
const unpacker = Deno.build.os === "darwin" ? darwinUnpack : unzip;
await unpacker({ downloadDir, archiveFileName });
}
private async setExecutable(binaryPath: string) {
const allowExecOctal = 0o755;
await chomod(binaryPath, allowExecOctal);
}
/**
* Setup stackql binary, check if binary exists, if not download it
*/
Expand All @@ -119,20 +133,18 @@ export class Downloader {

try {
const binaryName = this.getBinaryName();
const url = this.getUrl();
const downloadDir = await this.getDownloadDir();
const binaryPath = join(downloadDir, binaryName);
if (this.binaryExists(binaryName, downloadDir)) {
console.log("stackql is already installed");
return join(downloadDir, binaryName);
await this.setExecutable(binaryPath);
return binaryPath;
}
console.log("Downloading stackql binary");
const archiveFileName = `${downloadDir}/${url.split("/").pop()}`;
await this.downloadFile(url, archiveFileName);

console.log("Unpacking stackql binary");
const unpacker = Deno.build.os === "darwin" ? darwinUnpack : unzip;
await unpacker({ downloadDir, archiveFileName });
return join(downloadDir, binaryName);
console.log("Downloading stackql binary");
await this.installStackQL(downloadDir);
await this.setExecutable(binaryPath);
return binaryPath;
} catch (error) {
console.error(`ERROR: [setup] ${error.message}`);
Deno.exit(1);
Expand Down
37 changes: 37 additions & 0 deletions src/services/unpacker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { decompress } from "https://deno.land/x/zip/mod.ts";

type UnpackParams = {
downloadDir: string;
archiveFileName: string;
};

export const darwinUnpack = async (params: UnpackParams) => {
console.log("darwinUnpack");
const { downloadDir, archiveFileName } = params;
const unpackedFileName = `${downloadDir}/stackql`;
const commandPath = "pkgutil";
const commandArgs = ["--expand-full", archiveFileName, unpackedFileName];
const process = new Deno.Command(commandPath, {
args: commandArgs,
stdout: "piped",
stderr: "piped",
});

const { code, stdout, stderr } = await process.output();
if (code !== 0) {
const output = new TextDecoder().decode(stdout);
const errorOutput = new TextDecoder().decode(stderr);
console.error("Error executing pkgutil:", output, errorOutput);
throw new Error("Failed to unpack stackql");
}
};

export const unzip = async (params: UnpackParams) => {
console.log("unzip");
try {
await decompress(params.archiveFileName, params.downloadDir);
} catch (error) {
console.log("[unzip] error:", error);
throw new Error("Failed to unpack stackql");
}
};
18 changes: 6 additions & 12 deletions src/stackql.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./utils/downloader.ts";
import { Downloader } from "./services/downloader.ts";

export class StackQL {
private binaryPath?: string;
private downloader: Downloader = new Downloader();
constructor() {
}
constructor() {}
private async initialize() {
this.binaryPath = await this.downloader.setupStackQL();
}
Expand All @@ -16,20 +15,15 @@ export class StackQL {
assertExists(this.binaryPath);
}

const process = Deno.run({
cmd: [this.binaryPath, "exec", query], // Ensure this command is correct
const process = new Deno.Command(this.binaryPath, {
args: ["exec", query], // Ensure this command is correct
stdout: "piped",
stderr: "piped",
});

const [status, stdout, stderr] = await Promise.all([
process.status(),
process.output(),
process.stderrOutput(),
]);
process.close();
const { code, stdout, stderr } = await process.output();

if (status.code === 0) {
if (code === 0) {
const output = stdout;
const result = new TextDecoder().decode(output);
return result;
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const fileExists = (path: string) => {
try {
Deno.statSync(path);
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
}
throw error;
}
};

export const chomod = async (path: string, mode: number) => {
if (Deno.build.os !== "windows") {
await Deno.chmod(path, mode);
}
};
33 changes: 0 additions & 33 deletions src/utils/unpacker.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/tests/stackql.test.ts → tests/stackql.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { assertStringIncludes } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { StackQL } from "../stackql.ts";
import { StackQL } from "../src/stackql.ts";
import { removeStackQLDownload } from "./utils.ts";

Deno.test("StackQL runQuery - Successful Execution", async () => {
// Arrange
await removeStackQLDownload();
const stackQL = new StackQL();
const pullQuery = "REGISTRY PULL okta;";
const testQuery = "SHOW PROVIDERS"; // Replace with a valid query for your context
Expand Down
13 changes: 13 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { existsSync } from "https://deno.land/std/fs/mod.ts";
import { join } from "https://deno.land/std@0.133.0/path/mod.ts";

export const removeStackQLDownload = async () => {
const projectDir = Deno.cwd();
const stackqlPath = join(projectDir, ".stackql");
console.log("stackqlPath", stackqlPath);
console.log("existsSync(stackqlPath)", existsSync(stackqlPath));
if (existsSync(stackqlPath)) {
console.log("Removing .stackql directory");
await Deno.remove(stackqlPath, { recursive: true });
}
};