mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-21 20:37:58 +00:00
Add CLI scaffold and config generators
This commit is contained in:
parent
b22d46ff84
commit
2af64efd4e
15 changed files with 349 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,5 +1,6 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.tmp/
|
||||||
dist/
|
dist/
|
||||||
coverage/
|
coverage/
|
||||||
.env
|
.env
|
||||||
|
|
|
||||||
|
|
@ -23,3 +23,12 @@ Phase 1 is split into these reviewable branches:
|
||||||
- Forgejo access is performed by a constrained service account with minimum PAT scopes.
|
- Forgejo access is performed by a constrained service account with minimum PAT scopes.
|
||||||
- A repo is shareable only if the service account can see it and Embargo has sharing enabled for it.
|
- A repo is shareable only if the service account can see it and Embargo has sharing enabled for it.
|
||||||
|
|
||||||
|
## Current CLI Surface
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter @embargo/cli build
|
||||||
|
node packages/cli/dist/index.js init --dir ./embargo
|
||||||
|
node packages/cli/dist/index.js status --dir ./embargo
|
||||||
|
node packages/cli/dist/index.js start --dir ./embargo
|
||||||
|
node packages/cli/dist/index.js stop --dir ./embargo
|
||||||
|
```
|
||||||
|
|
|
||||||
91
packages/cli/src/commands/init.ts
Normal file
91
packages/cli/src/commands/init.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
import { mkdir, writeFile } from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import {
|
||||||
|
generateAppIni,
|
||||||
|
generateDockerCompose,
|
||||||
|
generateEnvFile,
|
||||||
|
generateExampleCaddyfile,
|
||||||
|
generateExampleNginxConf
|
||||||
|
} from "../generators/index.js";
|
||||||
|
import { generateSecret } from "../utils/crypto.js";
|
||||||
|
import { assertDockerPrerequisites } from "../utils/docker.js";
|
||||||
|
import { getFlagValue } from "../utils/flags.js";
|
||||||
|
|
||||||
|
interface InitOptions {
|
||||||
|
domain: string;
|
||||||
|
forgejoAdminPassword: string;
|
||||||
|
forgejoAdminUser: string;
|
||||||
|
outputDir: string;
|
||||||
|
visibilityMode: "allowlist" | "team";
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseInitOptions(argv: string[]): InitOptions {
|
||||||
|
return {
|
||||||
|
domain: getFlagValue(argv, "--domain") ?? "localhost",
|
||||||
|
forgejoAdminPassword: getFlagValue(argv, "--forgejo-admin-password") ?? "change-me",
|
||||||
|
forgejoAdminUser: getFlagValue(argv, "--forgejo-admin-user") ?? "admin",
|
||||||
|
outputDir: getFlagValue(argv, "--dir") ?? "embargo",
|
||||||
|
visibilityMode: (getFlagValue(argv, "--visibility-mode") as InitOptions["visibilityMode"] | undefined) ?? "team"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function initCommand(argv: string[]): Promise<void> {
|
||||||
|
const options = parseInitOptions(argv);
|
||||||
|
|
||||||
|
if (options.visibilityMode !== "team" && options.visibilityMode !== "allowlist") {
|
||||||
|
throw new Error(`Unsupported visibility mode: ${options.visibilityMode}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertDockerPrerequisites();
|
||||||
|
|
||||||
|
const outputRoot = path.resolve(process.cwd(), options.outputDir);
|
||||||
|
const forgejoDir = path.join(outputRoot, "forgejo");
|
||||||
|
const examplesDir = path.join(outputRoot, "examples");
|
||||||
|
|
||||||
|
await mkdir(forgejoDir, { recursive: true });
|
||||||
|
await mkdir(examplesDir, { recursive: true });
|
||||||
|
|
||||||
|
const generatedSecrets = {
|
||||||
|
dbPassword: generateSecret(),
|
||||||
|
embargoAdminPassword: generateSecret(),
|
||||||
|
forgejoServicePassword: generateSecret(),
|
||||||
|
jwtSecret: generateSecret()
|
||||||
|
};
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
writeFile(
|
||||||
|
path.join(outputRoot, "docker-compose.yml"),
|
||||||
|
generateDockerCompose({ domain: options.domain }),
|
||||||
|
"utf8"
|
||||||
|
),
|
||||||
|
writeFile(
|
||||||
|
path.join(outputRoot, ".env"),
|
||||||
|
generateEnvFile({
|
||||||
|
domain: options.domain,
|
||||||
|
forgejoAdminPassword: options.forgejoAdminPassword,
|
||||||
|
forgejoAdminUser: options.forgejoAdminUser,
|
||||||
|
visibilityMode: options.visibilityMode,
|
||||||
|
...generatedSecrets
|
||||||
|
}),
|
||||||
|
"utf8"
|
||||||
|
),
|
||||||
|
writeFile(path.join(forgejoDir, "app.ini"), generateAppIni(), "utf8"),
|
||||||
|
writeFile(path.join(examplesDir, "Caddyfile"), generateExampleCaddyfile(options.domain), "utf8"),
|
||||||
|
writeFile(path.join(examplesDir, "nginx.conf"), generateExampleNginxConf(options.domain), "utf8")
|
||||||
|
]);
|
||||||
|
|
||||||
|
process.stdout.write(
|
||||||
|
[
|
||||||
|
`Generated Embargo scaffold in ${outputRoot}`,
|
||||||
|
`Visibility mode: ${options.visibilityMode}`,
|
||||||
|
"Generated files:",
|
||||||
|
"- docker-compose.yml",
|
||||||
|
"- .env",
|
||||||
|
"- forgejo/app.ini",
|
||||||
|
"- examples/Caddyfile",
|
||||||
|
"- examples/nginx.conf"
|
||||||
|
].join("\n") + "\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
12
packages/cli/src/commands/start.ts
Normal file
12
packages/cli/src/commands/start.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import { assertDockerPrerequisites, getComposeCommand } from "../utils/docker.js";
|
||||||
|
import { getFlagValue } from "../utils/flags.js";
|
||||||
|
|
||||||
|
export async function startCommand(argv: string[]): Promise<void> {
|
||||||
|
assertDockerPrerequisites();
|
||||||
|
|
||||||
|
const outputDir = path.resolve(process.cwd(), getFlagValue(argv, "--dir") ?? "embargo");
|
||||||
|
process.stdout.write(`${getComposeCommand(outputDir)} up -d\n`);
|
||||||
|
}
|
||||||
|
|
||||||
34
packages/cli/src/commands/status.ts
Normal file
34
packages/cli/src/commands/status.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { access } from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import { assertDockerPrerequisites } from "../utils/docker.js";
|
||||||
|
import { getFlagValue } from "../utils/flags.js";
|
||||||
|
|
||||||
|
const REQUIRED_FILES = [
|
||||||
|
"docker-compose.yml",
|
||||||
|
".env",
|
||||||
|
"forgejo/app.ini",
|
||||||
|
"examples/Caddyfile",
|
||||||
|
"examples/nginx.conf"
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export async function statusCommand(argv: string[]): Promise<void> {
|
||||||
|
assertDockerPrerequisites();
|
||||||
|
|
||||||
|
const outputDir = path.resolve(process.cwd(), getFlagValue(argv, "--dir") ?? "embargo");
|
||||||
|
const results = await Promise.all(
|
||||||
|
REQUIRED_FILES.map(async (file) => {
|
||||||
|
const filePath = path.join(outputDir, file);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await access(filePath);
|
||||||
|
return `OK ${file}`;
|
||||||
|
} catch {
|
||||||
|
return `MISS ${file}`;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
process.stdout.write([`Scaffold directory: ${outputDir}`, ...results].join("\n") + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
12
packages/cli/src/commands/stop.ts
Normal file
12
packages/cli/src/commands/stop.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import { assertDockerPrerequisites, getComposeCommand } from "../utils/docker.js";
|
||||||
|
import { getFlagValue } from "../utils/flags.js";
|
||||||
|
|
||||||
|
export async function stopCommand(argv: string[]): Promise<void> {
|
||||||
|
assertDockerPrerequisites();
|
||||||
|
|
||||||
|
const outputDir = path.resolve(process.cwd(), getFlagValue(argv, "--dir") ?? "embargo");
|
||||||
|
process.stdout.write(`${getComposeCommand(outputDir)} down\n`);
|
||||||
|
}
|
||||||
|
|
||||||
14
packages/cli/src/generators/app-ini.ts
Normal file
14
packages/cli/src/generators/app-ini.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
export function generateAppIni(): string {
|
||||||
|
return `[service]
|
||||||
|
DISABLE_REGISTRATION = true
|
||||||
|
DEFAULT_ORG_VISIBILITY = private
|
||||||
|
|
||||||
|
[repository]
|
||||||
|
DEFAULT_PRIVATE = private
|
||||||
|
FORCE_PRIVATE = true
|
||||||
|
|
||||||
|
[server]
|
||||||
|
HTTP_PORT = 3000
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
43
packages/cli/src/generators/docker-compose.ts
Normal file
43
packages/cli/src/generators/docker-compose.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
export function generateDockerCompose({ domain }: { domain: string }): string {
|
||||||
|
return `services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:16
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
forgejo:
|
||||||
|
image: codeberg.org/forgejo/forgejo:11.0.11
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
- "2222:22"
|
||||||
|
volumes:
|
||||||
|
- ./forgejo/app.ini:/data/gitea/conf/app.ini
|
||||||
|
|
||||||
|
embargo-service:
|
||||||
|
image: node:22-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
depends_on:
|
||||||
|
- forgejo
|
||||||
|
ports:
|
||||||
|
- "4000:4000"
|
||||||
|
environment:
|
||||||
|
EMBARGO_DOMAIN: ${domain}
|
||||||
|
volumes:
|
||||||
|
- embargo-data:/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
embargo-data:
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
28
packages/cli/src/generators/env.ts
Normal file
28
packages/cli/src/generators/env.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
interface EnvOptions {
|
||||||
|
dbPassword: string;
|
||||||
|
domain: string;
|
||||||
|
embargoAdminPassword: string;
|
||||||
|
forgejoAdminPassword: string;
|
||||||
|
forgejoAdminUser: string;
|
||||||
|
forgejoServicePassword: string;
|
||||||
|
jwtSecret: string;
|
||||||
|
visibilityMode: "allowlist" | "team";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateEnvFile(options: EnvOptions): string {
|
||||||
|
return `EMBARGO_DOMAIN=${options.domain}
|
||||||
|
EMBARGO_ADMIN_PASSWORD=${options.embargoAdminPassword}
|
||||||
|
JWT_SECRET=${options.jwtSecret}
|
||||||
|
|
||||||
|
FORGEJO_ADMIN_USERNAME=${options.forgejoAdminUser}
|
||||||
|
FORGEJO_ADMIN_PASSWORD=${options.forgejoAdminPassword}
|
||||||
|
FORGEJO_SERVICE_ACCOUNT=embargo-bot
|
||||||
|
FORGEJO_SERVICE_ACCOUNT_PASSWORD=${options.forgejoServicePassword}
|
||||||
|
FORGEJO_VISIBILITY_MODE=${options.visibilityMode}
|
||||||
|
|
||||||
|
POSTGRES_DB=forgejo
|
||||||
|
POSTGRES_USER=forgejo
|
||||||
|
POSTGRES_PASSWORD=${options.dbPassword}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
23
packages/cli/src/generators/examples.ts
Normal file
23
packages/cli/src/generators/examples.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
export function generateExampleCaddyfile(domain: string): string {
|
||||||
|
return `${domain} {
|
||||||
|
reverse_proxy /_embargo/* localhost:4000
|
||||||
|
reverse_proxy localhost:3000
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateExampleNginxConf(domain: string): string {
|
||||||
|
return `server {
|
||||||
|
server_name ${domain};
|
||||||
|
|
||||||
|
location /_embargo/ {
|
||||||
|
proxy_pass http://127.0.0.1:4000/;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://127.0.0.1:3000/;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
5
packages/cli/src/generators/index.ts
Normal file
5
packages/cli/src/generators/index.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
export * from "./app-ini.js";
|
||||||
|
export * from "./docker-compose.js";
|
||||||
|
export * from "./env.js";
|
||||||
|
export * from "./examples.js";
|
||||||
|
|
||||||
|
|
@ -1,14 +1,47 @@
|
||||||
import { EMBARGO_DEFAULT_FORGEJO_PORT, EMBARGO_DEFAULT_SERVICE_PORT } from "@embargo/shared";
|
#!/usr/bin/env node
|
||||||
|
|
||||||
function main(): void {
|
import { initCommand } from "./commands/init.js";
|
||||||
process.stdout.write(
|
import { startCommand } from "./commands/start.js";
|
||||||
[
|
import { statusCommand } from "./commands/status.js";
|
||||||
"Embargo CLI bootstrap",
|
import { stopCommand } from "./commands/stop.js";
|
||||||
`Forgejo default port: ${EMBARGO_DEFAULT_FORGEJO_PORT}`,
|
|
||||||
`Service default port: ${EMBARGO_DEFAULT_SERVICE_PORT}`
|
type CommandName = "init" | "start" | "status" | "stop";
|
||||||
].join("\n") + "\n"
|
|
||||||
);
|
const HELP_TEXT = `Embargo CLI
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
embargo <command> [options]
|
||||||
|
|
||||||
|
Commands:
|
||||||
|
init Generate the local Embargo stack scaffold
|
||||||
|
start Print the docker compose command used to start the stack
|
||||||
|
stop Print the docker compose command used to stop the stack
|
||||||
|
status Validate docker tooling and generated files
|
||||||
|
`;
|
||||||
|
|
||||||
|
async function main(): Promise<void> {
|
||||||
|
const [command, ...args] = process.argv.slice(2);
|
||||||
|
|
||||||
|
if (!command || command === "--help" || command === "-h") {
|
||||||
|
process.stdout.write(`${HELP_TEXT}\n`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const knownCommands: Record<CommandName, (argv: string[]) => Promise<void>> = {
|
||||||
|
init: initCommand,
|
||||||
|
start: startCommand,
|
||||||
|
status: statusCommand,
|
||||||
|
stop: stopCommand
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!(command in knownCommands)) {
|
||||||
|
process.stderr.write(`Unknown command: ${command}\n\n${HELP_TEXT}\n`);
|
||||||
|
process.exitCode = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await knownCommands[command as CommandName](args);
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
void main();
|
||||||
|
|
||||||
|
|
|
||||||
6
packages/cli/src/utils/crypto.ts
Normal file
6
packages/cli/src/utils/crypto.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { randomBytes } from "node:crypto";
|
||||||
|
|
||||||
|
export function generateSecret(bytes = 24): string {
|
||||||
|
return randomBytes(bytes).toString("hex");
|
||||||
|
}
|
||||||
|
|
||||||
19
packages/cli/src/utils/docker.ts
Normal file
19
packages/cli/src/utils/docker.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
|
||||||
|
function assertCommand(command: string, args: string[] = []): void {
|
||||||
|
const result = spawnSync(command, args, { stdio: "ignore" });
|
||||||
|
|
||||||
|
if (result.status !== 0) {
|
||||||
|
throw new Error(`Required command not available: ${[command, ...args].join(" ")}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function assertDockerPrerequisites(): void {
|
||||||
|
assertCommand("docker", ["--version"]);
|
||||||
|
assertCommand("docker", ["compose", "version"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getComposeCommand(outputDir: string): string {
|
||||||
|
return `docker compose --project-directory ${outputDir}`;
|
||||||
|
}
|
||||||
|
|
||||||
9
packages/cli/src/utils/flags.ts
Normal file
9
packages/cli/src/utils/flags.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
export function getFlagValue(argv: string[], flag: string): string | undefined {
|
||||||
|
const index = argv.indexOf(flag);
|
||||||
|
|
||||||
|
if (index === -1) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return argv[index + 1];
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue