Compare commits

..

No commits in common. "140841783b8310fe82e4b624b25b121ef6bc5395" and "d6b9677ca589aa55e40f48c06de9d95c867d185a" have entirely different histories.

13 changed files with 304 additions and 490 deletions

View file

@ -2,7 +2,7 @@ FROM node:22-alpine
WORKDIR /app
COPY package*.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY package*.json ./
COPY packages/service/package*.json ./packages/service/
COPY packages/shared/package*.json ./packages/shared/
COPY tsconfig.base.json ./
@ -19,6 +19,4 @@ ENV PORT=3000
EXPOSE 3000
USER node
CMD ["pnpm", "--filter", "@myst/service", "start"]

View file

@ -46,10 +46,10 @@ HTMX provides interactivity (filtering, form submissions) with minimal JavaScrip
## Quick Start
### 1. Run `myst config init`
### 1. Run `myst init`
```bash
myst config init
myst init
```
Prompts for:
@ -92,12 +92,12 @@ GRANT_TOKEN_SECRET="your-secret-here"
## Phase 1 Progress
- [x] Project renamed from Embargo to Myst
- [x] CLI scaffold (`myst config init`)
- [x] CLI scaffold (`myst init`)
- [x] Config generation (`.env`)
- [ ] Service foundation (FastAPI + PostgreSQL)
- [x] `myst config verify` — verify Forgejo connectivity
- [x] `myst config verify --diagnostic` — comprehensive diagnostics
- [x] `myst config view` — show current config
- [ ] `myst forgejo bootstrap` — verify Forgejo connectivity
- [ ] `myst doctor` — validate configuration
- [ ] `myst config print` — show current config
- [ ] Forgejo session authentication
- [ ] Repo ownership verification
- [ ] Private link creation (email-verified)

View file

@ -1,69 +0,0 @@
# agents.md
This file is the single source of truth for codebase obstacles, oddities, and their resolutions. Do not scatter this knowledge in comments, commit messages, or memory.
---
## Rules for agents
1. **Before solving a problem**, check this file. If the problem is documented, use the recorded solution or workaround. Do not re-derive it.
2. **After solving a new problem**, document it here before moving on. Keep entries concise.
3. **Do not deviate** from a recorded solution without first confirming with the user. Once confirmed, update this file with the new approach and reason before proceeding.
---
## Entry format
```text
### [Short problem title]
**Area:** [file, module, or system this affects]
**Obstacle:** What the problem is and where it appears.
**Solution/Workaround:** What was done to fix or work around it.
**Preference:** If multiple approaches exist, which was chosen and why.
```
---
## Documented obstacles
### TypeScript composite builds fail with no output
**Area:** packages/cli/tsconfig.json, tsconfig.base.json
**Obstacle:** When `composite: true` is set in tsconfig, TypeScript requires proper project references to build. The CLI package was building but producing no output files, causing "cannot find module" errors.
**Solution:** Remove `composite: true` from packages/cli/tsconfig.json. Also removed `noUncheckedIndexedAccess` and `exactOptionalPropertyTypes` from tsconfig.base.json as they caused strict type issues with dynamic config objects.
**Preference:** Use simple tsconfig without composite mode for CLI package.
### verify.ts overly complex abstraction
**Area:** packages/cli/src/commands/verify.ts
**Obstacle:** Code had too many layers: collectResults → addResult (inner function) → displayDiagnosticResults/verifyResults. Made it hard to trace and debug.
**Solution:** Rewrote to be flatter: verifyCommand → runDiagnosticMode/runVerifyMode. Used simple inline logic instead of adding functions for everything.
**Preference:** KISS - keep functions small but avoid over-abstraction.
### Error messages in forgejo-checks too generic
**Area:** packages/cli/src/utils/forgejo-checks.ts
**Obstacle:** Network errors showed "fetch failed" with no context. HTTP errors showed just status codes. Not actionable for users.
**Solution:** Added getErrorMessage() helper to detect: timeouts, connection refused, host not found. Improved HTTP error messages to include status text. Added context prefixes like "cannot reach server", "cannot connect to API".
**Preference:** Actionable messages that tell users what to fix.
### ENV_MAPPING duplicated inline in verify.ts
**Area:** packages/cli/src/commands/verify.ts
**Obstacle:** Had duplicate object with same keys as ENV_MAPPING from config.ts. Two sources of truth.
**Solution:** Import ENV_MAPPING and use it. Change display to show env var names (SNAKE_CASE) instead of internal keys.
**Preference:** Single source of truth in config.ts.
### Display verbiage redundancy
**Area:** packages/cli/src/commands/verify.ts
**Obstacle:** Displayed "MYST_PUBLIC_URL set" - "set" is redundant, icon already shows status.
**Solution:** Just show env var name, remove "set". Shows "MYST_PUBLIC_URL configured" or "MYST_PUBLIC_URL missing".
**Preference:** Cleaner, let icon indicate status.
### loadConfig returning wrong type for error case
**Area:** packages/cli/src/commands/verify.ts
**Obstacle:** loadConfig() threw error when .env not found, but caught and re-threw in verifyCommand. This caused process to exit with stack trace instead of clean message.
**Solution:** Return null on ENOENT, handle null in verifyCommand. Return type: `Record<string, string> | null`.
**Preference:** Explicit null handling over exceptions for user-facing errors.
### process.exit(0) prevents finally blocks
**Area:** CLI commands using process.exit()
**Obstacle:** process.exit(0) stops execution immediately, never runs cleanup code.
**Solution:** Use process.exitCode = 1 and return instead of process.exit().
**Preference:** Set exitCode and return to allow cleanup.

View file

@ -45,7 +45,7 @@ Myst is a companion service deployed beside an existing Forgejo instance. It own
v v
Myst Database Forgejo API
(Postgres) (existing instance)
```ascii
```
Deployment boundaries:

View file

@ -8,7 +8,6 @@ interface OptionSpec {
type: "string" | "boolean";
short?: string;
default?: string | boolean | string[] | boolean[];
description?: string;
}
type OptionsSpec = Record<string, OptionSpec>;
@ -107,57 +106,28 @@ function showCommandHelp(
lines.push("");
}
const hasSubcommands = cmdConf.subcommands && Object.keys(cmdConf.subcommands).length > 0;
const usageCmd = subcommand ? `${command} ${subcommand}` : command;
const usageLine = hasSubcommands && !subcommand
? `myst ${command} <subcommand> [options]`
: `myst ${usageCmd} [options]`;
lines.push(c.bold("Usage:"));
lines.push(`${INDENT}${usageLine}`);
lines.push(`${INDENT}myst ${usageCmd} [options]`);
lines.push("");
if (hasSubcommands && !subcommand) {
const subcommandRows: HelpRow[] = [];
for (const [name, conf] of Object.entries(cmdConf.subcommands!)) {
subcommandRows.push({ name, desc: conf.description ?? "" });
}
const colWidth =
Math.max(...subcommandRows.map((r) => r.name.length)) + PADDING;
lines.push(c.bold("Subcommands:"));
for (const row of subcommandRows) {
if (row.desc) {
lines.push(`${INDENT}${row.name.padEnd(colWidth)}${c.dim(row.desc)}`);
} else {
lines.push(`${INDENT}${row.name}`);
}
}
lines.push("");
}
if (targetConf.options && Object.keys(targetConf.options).length > 0) {
const optionRows: HelpRow[] = [];
for (const [optName, optSpec] of Object.entries(targetConf.options)) {
const short = optSpec.short ? `, -${optSpec.short}` : "";
const typeHint = optSpec.type === "string" ? " <value>" : "";
const desc = optSpec.description ?? "";
optionRows.push({
name: `--${optName}${short}${typeHint}`,
desc,
desc: "",
});
}
const colWidth =
Math.max(...optionRows.map((r) => r.name.length)) + PADDING;
const colWidth = Math.max(...optionRows.map((r) => r.name.length)) + PADDING;
lines.push(c.bold("Options:"));
for (const row of optionRows) {
if (row.desc) {
lines.push(`${INDENT}${row.name.padEnd(colWidth)}${c.dim(row.desc)}`);
} else {
lines.push(`${INDENT}${row.name}`);
}
lines.push(`${INDENT}${row.name.padEnd(colWidth)}`);
}
lines.push("");
}
@ -183,8 +153,8 @@ export async function runCli(
const isVersionFlag = (arg: string) => arg === "--version" || arg === "-v";
// Check for version flag (early exit)
if (argv.some(isVersionFlag)) {
process.stdout.write(version + "\n");
if (argv.length > 0 && argv.every(isVersionFlag)) {
process.stdout.write(version);
return;
}
@ -214,12 +184,7 @@ export async function runCli(
// If help is requested for a specific command, show command-specific help
if (wantsCommandHelp) {
const subcommands = cmdConf.subcommands;
const actualSubcommand =
subcommand && subcommands && subcommand in subcommands
? subcommand
: undefined;
showCommandHelp(command, cmdConf, actualSubcommand);
showCommandHelp(command, cmdConf, subcommand || undefined);
return;
}
@ -228,7 +193,7 @@ export async function runCli(
// Check if subcommand was passed and if it's a valid subcommand (not an option flag)
const subcommands = cmdConf.subcommands;
const isSubcommandAnOption = subcommand && subcommand.startsWith("-");
if (isSubcommandAnOption) {
// Subcommand looks like an option (e.g., --diagnostic), treat as option for parent
targetCmdConf = cmdConf.handler ? cmdConf : undefined;
@ -256,26 +221,18 @@ export async function runCli(
}
// Build args for parseArgs - if subcommand is an option, include it
const argsForOptions = isSubcommandAnOption
? [subcommand, ...remainingArgs]
const argsForOptions = isSubcommandAnOption
? [subcommand, ...remainingArgs]
: remainingArgs;
// Parse target command options
const { values: cmdOpts } = parseArgs({
args: argsForOptions,
options: targetCmdConf.options || {},
allowPositionals: true,
});
try {
// Parse target command options
const { values: cmdOpts, positionals } = parseArgs({
args: argsForOptions,
options: targetCmdConf.options || {},
allowPositionals: true,
});
if (positionals.length > 0) {
process.stderr.write(
`${c.red("Error:")} Unexpected argument: ${positionals[0]}\n`,
);
process.exitCode = 1;
return;
}
// Execute the target command handler
await targetCmdConf.handler(cmdOpts);
} catch (error) {

View file

@ -8,7 +8,7 @@ export const commandRegistry = {
subcommands: {
init: configInit.commandConfig,
view: configView.commandConfig,
verify: verify.commandConfig,
},
},
verify: verify.commandConfig,
};

View file

@ -5,61 +5,70 @@ import * as c from "yoctocolors";
import * as p from "@clack/prompts";
import { generateSecret } from "../../utils/crypto.js";
import {
generateEnvFile,
SECRET_ENV_KEYS,
} from "../../utils/config.js";
import { generateEnvFile, DATABASE_PROTOCOLS } from "../../utils/config.js";
import { CommandConfig } from "../../cli-router.js";
function normalizeHttpUrl(value: string): string {
const trimmed = value.trim();
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
const isNotHttp = !/^https?:\/\//i.test(trimmed);
if (hasProtocol && isNotHttp) {
throw new Error("URLs must use http:// or https://");
}
return hasProtocol ? trimmed : `https://${trimmed}`;
}
function validateHttpUrlOrHost(
value: string | undefined,
label: string,
): string | undefined {
if (!value?.trim()) return `${label} is required`;
try {
const normalized = normalizeHttpUrl(value);
const url = new URL(normalized);
if (!url.hostname) {
return `${label} must include a domain or hostname`;
}
} catch {
return `${label} must be a valid URL or hostname`;
}
return undefined;
}
function validatePostgresUrl(value: string | undefined): string | undefined {
if (!value?.trim()) {
return "Database URL is required";
}
try {
const url = new URL(value);
if (url.protocol !== "postgres:" && url.protocol !== "postgresql:") {
return "Database URL must start with postgres:// or postgresql://";
}
} catch {
return "Database URL must be a valid Postgres connection string";
}
return undefined;
}
async function init(opts: { output?: string }) {
// Helper functions scoped to init
function normalizeHttpUrl(value: string): string {
const trimmed = value.trim();
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
const isNotHttp = !/^https?:\/\//i.test(trimmed);
if (hasProtocol && isNotHttp) {
throw new Error("URLs must use http:// or https://");
}
return hasProtocol ? trimmed : `https://${trimmed}`;
}
function validateHttpUrlOrHost(
value: string | undefined,
label: string,
): string | undefined {
if (!value?.trim()) return `${label} is required`;
try {
const normalized = normalizeHttpUrl(value);
const url = new URL(normalized);
if (!url.hostname) {
return `${label} must include a domain or hostname`;
}
} catch {
return `${label} must be a valid URL or hostname`;
}
return undefined;
}
function validatePostgresUrl(value: string | undefined): string | undefined {
if (!value?.trim()) {
return "Database URL is required";
}
try {
const url = new URL(value);
if (!DATABASE_PROTOCOLS.includes(url.protocol as "postgres:" | "postgresql:")) {
return "Database URL must start with postgres:// or postgresql://";
}
} catch {
return "Database URL must be a valid Postgres connection string";
}
return undefined;
}
async function writePrivateFile(
filePath: string,
content: string,
): Promise<void> {
await fs.writeFile(filePath, content, {
encoding: "utf8",
flag: "wx",
mode: 0o600,
});
}
p.intro(c.bold("myst config init"));
p.note(
[
@ -80,9 +89,9 @@ async function init(opts: { output?: string }) {
c.yellow("Before you start:"),
);
const envPath = opts.output || path.join(process.cwd(), ".env");
const outDir = opts.output ? path.dirname(opts.output) : process.cwd();
const envPath = opts.output || path.join(outDir, ".env");
// Check if .env file already exists
let envExists = false;
try {
await fs.stat(envPath);
@ -91,7 +100,6 @@ async function init(opts: { output?: string }) {
// .env file does not exist
}
// Handle existing .env file
if (envExists) {
while (true) {
const choice = await p.select({
@ -107,27 +115,17 @@ async function init(opts: { output?: string }) {
if (p.isCancel(choice) || choice === "cancel") {
p.cancel("\nOperation cancelled.");
process.exitCode = 0;
return;
process.exit(0);
}
if (choice === "view") {
const envContent = await fs.readFile(envPath, { encoding: "utf-8" });
const maskedEnvContent = envContent
.split("\n")
.map((line) => {
if (!line.includes("=")) return line;
const [key] = line.split("=");
return SECRET_ENV_KEYS.includes(key as typeof SECRET_ENV_KEYS[number])
? `${key}=****`
: line;
})
.join("\n");
p.note(c.dim(maskedEnvContent), c.yellow("Current .env"));
p.note(c.dim(envContent), c.yellow("Current .env"));
continue;
}
if (choice === "overwrite") {
await fs.unlink(envPath);
break;
}
}
@ -185,7 +183,7 @@ async function init(opts: { output?: string }) {
{
onCancel: () => {
p.cancel("\nOperation cancelled.");
process.exitCode = 0;
process.exit(0);
},
},
);
@ -211,23 +209,7 @@ async function init(opts: { output?: string }) {
publicUrl,
});
if (envExists) {
const tempPath = envPath + ".tmp";
await fs.writeFile(tempPath, envContent, {
encoding: "utf8",
flag: "wx",
mode: 0o600,
});
await fs.unlink(envPath).catch(() => {});
await fs.rename(tempPath, envPath);
} else {
await fs.writeFile(envPath, envContent, {
encoding: "utf8",
flag: "wx",
mode: 0o600,
});
}
await writePrivateFile(envPath, envContent);
s.stop(c.yellow(`.env created at ${envPath}`));
p.note(
@ -258,4 +240,4 @@ export const commandConfig: CommandConfig = {
output: { type: "string", short: "o" },
},
handler: init,
};
};

View file

@ -6,10 +6,15 @@ import {
ENV_MAPPING,
MystEnvironmentVariables,
parseEnvFile,
SECRET_KEYS,
} from "../../utils/config.js";
import { CommandConfig } from "../../cli-router.js";
function redactSecret(value: string | undefined): string {
if (!value) return c.dim("not set");
if (value.length <= 4) return c.dim("****");
return value.slice(0, 4) + c.dim("****");
}
export const commandConfig: CommandConfig = {
description: "Display current configuration",
handler: async () => {
@ -36,14 +41,13 @@ export const commandConfig: CommandConfig = {
for (const [envKey, configKey] of Object.entries(ENV_MAPPING)) {
const value = config[configKey];
const displayValue = SECRET_KEYS.includes(
configKey as (typeof SECRET_KEYS)[number],
)
? c.dim("****")
: value || c.dim("not set");
const displayValue =
configKey === "forgejoPat" || configKey === "grantTokenSecret"
? redactSecret(value)
: value || c.dim("not set");
process.stdout.write(`${c.bold(envKey + ":")} ${displayValue}\n`);
}
process.exitCode = 0;
},
};
};

View file

@ -3,10 +3,16 @@ import path from "node:path";
import * as c from "yoctocolors";
import * as p from "@clack/prompts";
import { ENV_MAPPING, parseEnvFile, REQUIRED_KEYS } from "../utils/config.js";
import {
ENV_MAPPING,
MystEnvironmentVariables,
parseEnvFile,
validateDatabaseUrl,
REQUIRED_KEYS,
} from "../utils/config.js";
import {
checkForgejoReachability,
checkForgejoApiAuthentication,
checkForgejoApi,
checkRepoAccess,
} from "../utils/forgejo-checks.js";
import { CommandConfig } from "../cli-router.js";
@ -15,190 +21,179 @@ interface VerifyOptions {
diagnostic?: boolean;
}
interface CheckResult {
name: string;
success: boolean;
message?: string;
}
async function verifyCommand(opts: VerifyOptions): Promise<void> {
const diagnostic = opts.diagnostic ?? false;
p.intro(c.bold("myst config verify"));
function loadConfig(): Record<string, string> | null {
const envPath = path.join(process.cwd(), ".env");
let config: Partial<MystEnvironmentVariables>;
try {
return parseEnvFile(envPath);
config = parseEnvFile(envPath);
} catch (error) {
const err = error as NodeJS.ErrnoException;
if (err.code === "ENOENT") {
p.cancel(`${c.red("Error:")} .env file not found in ${process.cwd()}`);
process.exitCode = 1;
return null;
return;
}
throw error;
}
}
function getMissingKeys(config: Record<string, string>): string[] {
const missing: string[] = [];
for (const key of REQUIRED_KEYS) {
if (!config[key]) {
missing.push(key.replace(/(?<=[a-z])(?=[A-Z])/g, "_").toUpperCase());
const results: { name: string; success: boolean; message?: string }[] = [];
function addResult(name: string, success: boolean, message?: string) {
results.push({ name, success, ...(message !== undefined && { message }) });
}
if (diagnostic) {
addResult("Config file exists", true, envPath);
for (const [envKey, configKey] of Object.entries(ENV_MAPPING)) {
const value = config[configKey];
addResult(`${envKey} set`, !!value, value ? "configured" : "missing");
}
if (config.databaseUrl) {
const valid = validateDatabaseUrl(config.databaseUrl);
addResult("Database URL format", valid, valid ? "valid" : "invalid");
}
}
return missing;
}
async function runDiagnosticMode(
config: Record<string, string>,
missing: string[],
): Promise<void> {
const results: CheckResult[] = [];
const forgejoConfigured =
config.forgejoBaseUrl && config.forgejoPat && config.forgejoBotUsername;
if (missing.length > 0) {
results.push({
name: "Required config",
success: false,
message: `Missing: ${missing.join(", ")}`,
});
}
if (forgejoConfigured && config.forgejoBaseUrl && config.forgejoPat) {
const reachability = await checkForgejoReachability(config.forgejoBaseUrl);
results.push(reachability);
for (const [envKey, configKey] of Object.entries(ENV_MAPPING)) {
const value = config[configKey];
results.push({
name: envKey,
success: !!value,
message: value ? "configured" : "missing",
});
}
if (reachability.success) {
const apiToken = await checkForgejoApi(
config.forgejoBaseUrl,
config.forgejoPat,
diagnostic,
);
results.push(apiToken);
const forgejoResults = await runForgejoChecks(config);
const allResults = [...results, ...forgejoResults];
const lines = allResults.map((r) => {
const icon = r.success ? c.green("✓") : c.red("✗");
const msg = r.message ? ` ${r.message}` : "";
return `${icon} ${r.name}${msg}`;
});
p.note(lines.join("\n"), c.yellow("Results"));
const failures = allResults.filter((r) => !r.success);
if (failures.length > 0) {
p.outro(c.red(`${failures.length} issue(s) found`));
process.exitCode = 1;
return;
}
p.outro(c.green("All checks passed"));
}
async function runVerifyMode(
config: Record<string, string>,
missing: string[],
): Promise<void> {
if (missing.length > 0) {
p.cancel(
`${c.red("Error:")} Missing required env variables: ${missing.join(", ")}`,
);
process.exitCode = 1;
return;
if (apiToken.success && config.forgejoBotUsername) {
const repoAccess = await checkRepoAccess(
config.forgejoBaseUrl,
config.forgejoPat,
config.forgejoBotUsername,
);
results.push(repoAccess);
}
}
}
const s = p.spinner();
s.start("Verifying Forgejo configuration");
const forgejoResults = await runForgejoChecks(config);
try {
if (diagnostic) {
s.start("Running diagnostics");
s.stop("Diagnostics complete");
const firstFailure = forgejoResults.find((r) => !r.success);
if (firstFailure) {
s.stop(firstFailure.message);
p.cancel(`${c.red("✗")} ${firstFailure.name}: ${firstFailure.message}`);
const lines = results.map((r) => {
const icon = r.success ? c.green("✓") : c.red("✗");
const msg = r.message ? ` ${r.message}` : "";
return `${icon} ${r.name}${msg}`;
});
p.note(lines.join("\n"), c.yellow("Results"));
const failures = results.filter((r) => !r.success);
if (failures.length > 0) {
p.outro(c.red(`${failures.length} issue(s) found`));
process.exitCode = 1;
return;
}
p.outro(c.green("All checks passed"));
} else {
const missing: string[] = [];
for (const key of REQUIRED_KEYS) {
if (!config[key]) {
const envKey = Object.entries({
forgejoBaseUrl: "FORGEJO_BASE_URL",
forgejoBotUsername: "FORGEJO_BOT_USERNAME",
forgejoPat: "FORGEJO_PAT",
}).find(([, v]) => v === key)?.[0];
if (envKey) missing.push(envKey);
}
}
if (missing.length > 0) {
p.cancel(
`${c.red("Error:")} Missing required config: ${missing.join(", ")}`,
);
process.exitCode = 1;
return;
}
s.start("Verifying Forgejo configuration");
const reachability = await checkForgejoReachability(
config.forgejoBaseUrl!,
);
if (!reachability.success) {
s.stop(reachability.message);
p.cancel(`${c.red("✗")} ${reachability.name}: ${reachability.message}`);
process.exitCode = 1;
return;
}
const apiToken = await checkForgejoApi(
config.forgejoBaseUrl!,
config.forgejoPat!,
);
if (!apiToken.success) {
s.stop(apiToken.message);
p.cancel(`${c.red("✗")} ${apiToken.name}: ${apiToken.message}`);
process.exitCode = 1;
return;
}
const repoAccess = await checkRepoAccess(
config.forgejoBaseUrl!,
config.forgejoPat!,
config.forgejoBotUsername!,
);
s.stop("Verification complete");
p.note(
[
`${c.green("✓")} ${reachability.name}`,
`${c.green("✓")} ${apiToken.name}: ${apiToken.message}`,
`${c.green(repoAccess.success ? "✓" : "✗")} ${repoAccess.name}: ${repoAccess.message}`,
].join("\n"),
c.yellow("Results"),
);
if (!repoAccess.success) {
process.exitCode = 1;
return;
}
p.outro(c.green("Verification passed"));
}
} catch (error) {
s.stop();
const message = error instanceof Error ? error.message : "Unknown error";
p.cancel(`${c.red("Error:")} ${message}`);
process.exitCode = 1;
return;
}
if (forgejoResults.length === 0) {
s.stop("No Forgejo configuration");
p.cancel(`${c.red("Error:")} Forgejo not configured`);
process.exitCode = 1;
return;
}
s.stop("Verification complete");
const [reachability, auth, repoAccess] = forgejoResults;
p.note(
[
`${c.green("✓")} ${reachability.name}`,
`${c.green("✓")} ${auth.name}: ${auth.message}`,
`${c.green(repoAccess.success ? "✓" : "✗")} ${repoAccess.name}: ${repoAccess.message}`,
].join("\n"),
c.yellow("Results"),
);
p.outro(c.green("Verification passed"));
}
async function runForgejoChecks(
config: Record<string, string>,
): Promise<CheckResult[]> {
const { forgejoBaseUrl, forgejoPat, forgejoBotUsername } = config;
if (!forgejoBaseUrl || !forgejoPat || !forgejoBotUsername) {
return [];
}
const results: CheckResult[] = [];
const reachability = await checkForgejoReachability(forgejoBaseUrl);
results.push(reachability);
if (!reachability.success) {
return results;
}
const auth = await checkForgejoApiAuthentication(forgejoBaseUrl, forgejoPat);
results.push(auth);
if (!auth.success) {
return results;
}
const repoAccess = await checkRepoAccess(
forgejoBaseUrl,
forgejoPat,
forgejoBotUsername,
);
results.push(repoAccess);
return results;
}
async function verifyCommand(opts: VerifyOptions): Promise<void> {
const isDiagnostic = opts.diagnostic ?? false;
p.intro(c.bold("myst verify"));
const config = loadConfig();
if (!config) {
return;
}
const missing = getMissingKeys(config);
if (isDiagnostic) {
await runDiagnosticMode(config, missing);
} else {
await runVerifyMode(config, missing);
}
}
export const commandConfig: CommandConfig = {
description: "Verify Myst configuration and connection to Forgejo",
options: {
diagnostic: {
type: "boolean",
description: "Show diagnostic information for all checks",
},
diagnostic: { type: "boolean" },
},
handler: verifyCommand,
};

View file

@ -28,20 +28,6 @@ export const REQUIRED_KEYS = [
export const DATABASE_PROTOCOLS = ["postgres:", "postgresql:"] as const;
export const SECRET_KEYS = [
"forgejoPat",
"grantTokenSecret",
"databaseUrl",
"adminUrl",
] as const;
export const SECRET_ENV_KEYS = [
"DATABASE_URL",
"FORGEJO_PAT",
"GRANT_TOKEN_SECRET",
"MYST_ADMIN_URL",
] as const;
export function parseEnvFile(
envPath: string,
): Partial<MystEnvironmentVariables> {
@ -71,32 +57,38 @@ export function validateDatabaseUrl(url: string | undefined): boolean {
if (!url) return false;
try {
const parsed = new URL(url);
return DATABASE_PROTOCOLS.includes(
parsed.protocol as "postgres:" | "postgresql:",
);
return DATABASE_PROTOCOLS.includes(parsed.protocol as "postgres:" | "postgresql:");
} catch {
return false;
}
}
function escapeEnvValue(value: string): string {
return `"${value
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\r?\n/g, "\\n")
.replace(/\$/g, "\\$")}"`;
}
export function generateEnvFile(options: MystEnvironmentVariables): string {
const lines = [
`# Public URL used for share links.`,
`MYST_PUBLIC_URL=${options.publicUrl}`,
`${ENV_MAPPING.MYST_PUBLIC_URL}=${escapeEnvValue(options.publicUrl)}`,
``,
`# Private operator URL for the admin UI. Protect this upstream.`,
`MYST_ADMIN_URL=${options.adminUrl}`,
`${ENV_MAPPING.MYST_ADMIN_URL}=${escapeEnvValue(options.adminUrl)}`,
``,
`# Myst's own Postgres database.`,
`DATABASE_URL=${options.databaseUrl}`,
`${ENV_MAPPING.DATABASE_URL}=${escapeEnvValue(options.databaseUrl)}`,
``,
`# Existing Forgejo instance and operator-created PAT for the dedicated Forgejo user.`,
`FORGEJO_BASE_URL=${options.forgejoBaseUrl}`,
`FORGEJO_BOT_USERNAME=${options.forgejoBotUsername}`,
`FORGEJO_PAT=${options.forgejoPat}`,
`${ENV_MAPPING.FORGEJO_BASE_URL}=${escapeEnvValue(options.forgejoBaseUrl)}`,
`${ENV_MAPPING.FORGEJO_BOT_USERNAME}=${escapeEnvValue(options.forgejoBotUsername)}`,
`${ENV_MAPPING.FORGEJO_PAT}=${escapeEnvValue(options.forgejoPat)}`,
``,
`# Secret used to sign or derive grant tokens.`,
`GRANT_TOKEN_SECRET=${options.grantTokenSecret}`,
`${ENV_MAPPING.GRANT_TOKEN_SECRET}=${escapeEnvValue(options.grantTokenSecret)}`,
];
return lines.join("\n");

View file

@ -1,5 +1,3 @@
const TIMEOUT = 5000;
export type CheckResult = {
name: string;
success: boolean;
@ -18,57 +16,32 @@ export function createCheckResult(
return result;
}
async function fetchWithTimeout(
url: string,
init: RequestInit,
): Promise<Response> {
return fetch(url, { ...init, signal: AbortSignal.timeout(TIMEOUT) });
}
function getErrorMessage(error: unknown, context: string): string {
if (error instanceof Error) {
if (error.name === "TimeoutError") {
return `${context}: connection timed out`;
}
if (error.message.includes("ECONNREFUSED")) {
return `${context}: connection refused`;
}
if (error.message.includes("ENOTFOUND") || error.message.includes("getaddrinfo")) {
return `${context}: host not found`;
}
return `${context}: ${error.message}`;
}
return `${context}: unknown error`;
}
export async function checkForgejoReachability(
baseUrl: string,
): Promise<CheckResult> {
try {
const response = await fetchWithTimeout(baseUrl, { method: "GET" });
if (response.ok) {
return createCheckResult("Forgejo reachability", true, "connected");
const response = await fetch(baseUrl, { method: "GET" });
if (response.ok || response.status === 200) {
return createCheckResult("Forgejo reachability", true);
}
return createCheckResult(
"Forgejo reachability",
false,
`server returned ${response.status} ${response.statusText}`,
`HTTP ${response.status}`,
);
} catch (error) {
return createCheckResult(
"Forgejo reachability",
false,
getErrorMessage(error, "cannot reach server"),
);
const message = error instanceof Error ? error.message : "Unknown error";
return createCheckResult("Forgejo reachability", false, message);
}
}
export async function checkForgejoApiAuthentication(
export async function checkForgejoApi(
baseUrl: string,
pat: string,
colorize = false,
): Promise<CheckResult> {
try {
const response = await fetchWithTimeout(`${baseUrl}/api/v1/user`, {
const response = await fetch(`${baseUrl}/api/v1/user`, {
method: "GET",
headers: {
Authorization: `Bearer ${pat}`,
@ -79,15 +52,7 @@ export async function checkForgejoApiAuthentication(
return createCheckResult(
"Forgejo API token",
false,
"token is invalid or expired",
);
}
if (response.status === 403) {
return createCheckResult(
"Forgejo API token",
false,
"token lacks sufficient permissions",
"Invalid or expired token",
);
}
@ -95,22 +60,24 @@ export async function checkForgejoApiAuthentication(
return createCheckResult(
"Forgejo API token",
false,
`server returned ${response.status}`,
`HTTP ${response.status}`,
);
}
const user = (await response.json()) as { login: string };
let login = user.login;
if (colorize) {
const { default: c } = await import("yoctocolors");
login = c.green(user.login);
}
return createCheckResult(
"Forgejo API token",
true,
`authenticated as ${user.login}`,
`Authenticated as ${login}`,
);
} catch (error) {
return createCheckResult(
"Forgejo API token",
false,
getErrorMessage(error, "cannot connect to API"),
);
const message = error instanceof Error ? error.message : "Unknown error";
return createCheckResult("Forgejo API token", false, message);
}
}
@ -120,7 +87,7 @@ export async function checkRepoAccess(
username: string,
): Promise<CheckResult> {
try {
const response = await fetchWithTimeout(
const response = await fetch(
`${baseUrl}/api/v1/users/${username}/repos?limit=1`,
{
method: "GET",
@ -130,20 +97,8 @@ export async function checkRepoAccess(
},
);
if (response.status === 401 || response.status === 403) {
return createCheckResult(
"Repo access",
false,
"token lacks permission to list repositories",
);
}
if (!response.ok) {
return createCheckResult(
"Repo access",
false,
`server returned ${response.status}`,
);
return createCheckResult("Repo access", false, `HTTP ${response.status}`);
}
const repos = (await response.json()) as { name: string }[];
@ -151,20 +106,17 @@ export async function checkRepoAccess(
return createCheckResult(
"Repo access",
true,
`found ${repos.length} repository`,
`Found ${repos.length} repo(s)`,
);
}
return createCheckResult(
"Repo access",
false,
"no repositories found for user",
true,
"No repos found (service account may not have repo access)",
);
} catch (error) {
return createCheckResult(
"Repo access",
false,
getErrorMessage(error, "cannot list repositories"),
);
const message = error instanceof Error ? error.message : "Unknown error";
return createCheckResult("Repo access", false, message);
}
}
}

View file

@ -1,9 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
},
"references": [{ "path": "../shared" }],
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts"],
}

View file

@ -9,6 +9,8 @@
"@myst/shared": ["packages/shared/src/index.ts"]
},
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"skipLibCheck": true,
"declaration": true,
"sourceMap": true,