From a6e133b4660a0d09e87746fc672ba72fce167bdb Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Mon, 5 Feb 2024 17:11:22 +0000 Subject: [PATCH] feat: add --standalone option to 'init' command --- main.ts | 4 ++++ src/cmd/down.ts | 9 ++++++++- src/cmd/init.ts | 24 ++++++++++++++++++++---- src/cmd/logs.ts | 7 ++++++- src/cmd/shell.ts | 7 ++++++- src/cmd/up.ts | 4 ++++ 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/main.ts b/main.ts index ddd72f5..75a7fbf 100644 --- a/main.ts +++ b/main.ts @@ -29,6 +29,10 @@ async function main() { "-t, --template ", "Create a workspace from a template" ) + .option( + "-s, --standalone [standalone:boolean]", + "Create a standalone workspace from a template" + ) .action(function (options, name) { init(options, name); }) diff --git a/src/cmd/down.ts b/src/cmd/down.ts index cbc7f30..f943e2e 100644 --- a/src/cmd/down.ts +++ b/src/cmd/down.ts @@ -1,5 +1,6 @@ import { pkgx, Logger } from "../../deps.ts"; import * as workspaces from "../workspaces.ts"; +import { existsSync } from "node:fs"; async function down({ ask }: { ask?: boolean }, workspace?: string) { const logger = new Logger(); @@ -20,9 +21,15 @@ async function down({ ask }: { ask?: boolean }, workspace?: string) { workdir = result.path; } + if (existsSync(`${workdir}/.pocketenv`)) { + workdir = `${workdir}/.pocketenv`; + } + await pkgx.run(`terraform destroy ${args.join(" ")}`, "inherit", workdir); - const result = await workspaces.get(workspace || workdir); + const result = await workspaces.get( + workspace || workdir.replace(/\/.pocketenv$/, "") + ); if (!result) { logger.warn("Workspace not found"); diff --git a/src/cmd/init.ts b/src/cmd/init.ts index 70f8582..e5873bd 100644 --- a/src/cmd/init.ts +++ b/src/cmd/init.ts @@ -14,7 +14,10 @@ import { existsSync } from "node:fs"; import * as workspaces from "../workspaces.ts"; import { getDefaultGithubBranch } from "../lib.ts"; -async function init({ template }: { template?: string }, name?: string) { +async function init( + { template, standalone }: { template?: string; standalone?: boolean }, + name?: string +) { if (!name) { console.log(`${cyan("?")} Workspace name: `); @@ -35,7 +38,7 @@ async function init({ template }: { template?: string }, name?: string) { template = template.trim(); } - await downloadFromGithub(template); + await downloadFromGithub(template, standalone); await workspaces.save(Deno.cwd(), { containerId: null, @@ -50,7 +53,17 @@ async function init({ template }: { template?: string }, name?: string) { console.log(`Run ${brightGreen("`pocketenv up`")} to start the workspace.`); } -async function downloadFromGithub(template: string) { +async function downloadFromGithub(template: string, standalone?: boolean) { + if (!standalone) { + if (existsSync(".pocketenv")) { + console.log( + `🚨 ${brightGreen( + ".pocketenv" + )} directory already exists. Please remove it and try again.` + ); + Deno.exit(1); + } + } const terminalSpinner = new TerminalSpinner({ text: `Downloading ${green(template)} template ...`, spinner: SpinnerTypes.dots, @@ -98,7 +111,10 @@ async function downloadFromGithub(template: string) { ); } - await copyDir(`${cacheDir}/${template.split("/").pop()}-${branch}`, `.`); + await copyDir( + `${cacheDir}/${template.split("/").pop()}-${branch}`, + standalone ? "." : ".pocketenv" + ); } async function copyDir(src: string, dest: string) { diff --git a/src/cmd/logs.ts b/src/cmd/logs.ts index dcf5deb..586ac1a 100644 --- a/src/cmd/logs.ts +++ b/src/cmd/logs.ts @@ -1,5 +1,6 @@ import { pkgx } from "../../deps.ts"; import { spawn } from "../lib.ts"; +import { existsSync } from "node:fs"; import * as workspaces from "../workspaces.ts"; async function logs({ follow }: { follow?: boolean }, workspace?: string) { @@ -8,12 +9,16 @@ async function logs({ follow }: { follow?: boolean }, workspace?: string) { if (workspace) { const result = await workspaces.get(workspace); if (!result) { - console.error(`Workspace ${workspace} not found.`); + console.error(`🚨 Workspace ${workspace} not found.`); Deno.exit(1); } workdir = result.path; } + if (existsSync(`${workdir}/.pocketenv`)) { + workdir = `${workdir}/.pocketenv`; + } + const containerId = await spawn( "sh", ["-c", 'pkgx terraform output -json | pkgx jq -r ".container_id.value"'], diff --git a/src/cmd/shell.ts b/src/cmd/shell.ts index aff8332..f788c63 100644 --- a/src/cmd/shell.ts +++ b/src/cmd/shell.ts @@ -1,5 +1,6 @@ import { pkgx } from "../../deps.ts"; import { spawn } from "../lib.ts"; +import { existsSync } from "node:fs"; import * as workspaces from "../workspaces.ts"; async function shell(workspace?: string) { @@ -8,12 +9,16 @@ async function shell(workspace?: string) { if (workspace) { const result = await workspaces.get(workspace); if (!result) { - console.error(`Workspace ${workspace} not found.`); + console.error(`🚨 Workspace ${workspace} not found.`); Deno.exit(1); } workdir = result.path; } + if (existsSync(`${workdir}/.pocketenv`)) { + workdir = `${workdir}/.pocketenv`; + } + const containerId = await spawn( "sh", ["-c", 'pkgx terraform output -json | pkgx jq -r ".container_id.value"'], diff --git a/src/cmd/up.ts b/src/cmd/up.ts index 57dea3e..17493e0 100644 --- a/src/cmd/up.ts +++ b/src/cmd/up.ts @@ -33,6 +33,10 @@ async function up( workdir = result.path; } + if (existsSync(`${workdir}/.pocketenv`)) { + workdir = `${workdir}/.pocketenv`; + } + if (!existsSync(`${workdir}/.terraform`)) { await pkgx.run(`terraform init`, "inherit", workdir); }