Skip to content

Commit

Permalink
Merge pull request #26 from fluentci-io/feat/verify-required-deps
Browse files Browse the repository at this point in the history
feat: verify required dependencies before each `fluentci run`
  • Loading branch information
tsirysndr authored Feb 24, 2024
2 parents 37b487d + bad1e7e commit 0eb3ad1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cmd/docs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { green } from "../../deps.ts";
import { BASE_URL } from "../consts.ts";
import { verifyRequiredDependencies } from "../utils.ts";

async function docs(
pipeline = ".",
Expand All @@ -12,6 +13,7 @@ async function docs(
} = {}
) {
// verify if glow is installed
await verifyRequiredDependencies(["glow"]);

if (pipeline === ".") {
try {
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { BlobWriter, green, load, walk, ZipWriter, dayjs } from "../../deps.ts";
import { BASE_URL, FLUENTCI_WS_URL, RUNNER_URL } from "../consts.ts";
import { getCommitInfos } from "../git.ts";
import { getAccessToken, isLogged, extractVersion } from "../utils.ts";
import {
getAccessToken,
isLogged,
extractVersion,
verifyRequiredDependencies,
} from "../utils.ts";

/**
* Runs a Fluent CI pipeline.
Expand All @@ -15,6 +20,8 @@ async function run(
jobs: [string, ...Array<string>] | string[] = [],
options: Record<string, string | number | boolean | undefined> = {}
) {
await verifyRequiredDependencies();

await load({
envPath: ".fluentci/.env",
examplePath: ".fluentci/.env_required",
Expand Down
87 changes: 86 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dir } from "../deps.ts";
import { dir, brightGreen } from "../deps.ts";

export async function isLogged(): Promise<boolean> {
if (Deno.env.get("FLUENTCI_ACCESS_TOKEN")) {
Expand Down Expand Up @@ -84,3 +84,88 @@ export async function fluentciDirExists(): Promise<boolean> {
return false;
}
}

export async function verifyRequiredDependencies(
dependencies = ["deno", "dagger", "docker"]
) {
for (const dependency of dependencies) {
const command = new Deno.Command("sh", {
args: ["-c", `type ${dependency}`],
stdout: "piped",
stderr: "piped",
});
const process = await command.spawn();
const { code } = await process.status;

if (code !== 0) {
switch (dependency) {
case "deno":
await installDeno();
break;
case "dagger":
await installDagger();
break;
default:
console.log(
`${brightGreen(
dependency
)} is not installed. Please install it before running this command.`
);
Deno.exit(1);
}
}
}
}

async function installDeno() {
console.log(`${brightGreen("Deno")} is not installed`);
console.log("Downloading and installing Deno...");

const command = new Deno.Command("sh", {
args: ["-c", "curl -fsSL https://deno.land/install.sh | sh"],
stdout: "inherit",
stderr: "inherit",
});

const process = await command.spawn();
const { code } = await process.status;

if (code !== 0) {
console.log("Failed to install Deno.");
Deno.exit(1);
}

Deno.env.set("DENO_INSTALL", `${Deno.env.get("HOME")}/.deno`);
Deno.env.set(
"PATH",
`${Deno.env.get("DENO_INSTALL")}/bin:${Deno.env.get("PATH")}`
);
console.log("Deno installed successfully");
}

async function installDagger() {
console.log(`${brightGreen("Dagger")} is not installed. Installing...`);
console.log("Downloading and installing Dagger...");

const command = new Deno.Command("sh", {
args: [
"-c",
`curl -L https://dl.dagger.io/dagger/install.sh | DAGGER_VERSION=0.9.11 sh &&
if ! command -v sudo >/dev/null 2>&1; then mv bin/dagger /usr/local/bin; else sudo mv bin/dagger /usr/local/bin; fi &&
rmdir bin || true
`,
],
stdout: "inherit",
stderr: "inherit",
});

const process = await command.spawn();
const { code } = await process.status;

if (code !== 0) {
console.log("Failed to install Dagger.");
Deno.exit(1);
}

console.log("Dagger installed successfully");
}

0 comments on commit 0eb3ad1

Please sign in to comment.