mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-21 04:17:59 +00:00
feat(cli): add config print, verify, and doctor commands for config validation
- myst config print: show current config with redacted secrets - myst verify: verify Forgejo connectivity and API token - myst doctor: comprehensive validation of config and connectivity - add start script to service package.json
This commit is contained in:
parent
31c3105e34
commit
a3c202a662
7 changed files with 626 additions and 1 deletions
14
.dockerignore
Normal file
14
.dockerignore
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
node_modules
|
||||
.git
|
||||
*.log
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
dist
|
||||
tsconfig.tsbuildinfo
|
||||
*.tsbuildinfo
|
||||
coverage
|
||||
.vscode
|
||||
.idea
|
||||
*.md
|
||||
!README.md
|
||||
22
Dockerfile
Normal file
22
Dockerfile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
FROM node:22-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json ./
|
||||
COPY packages/service/package*.json ./packages/service/
|
||||
COPY packages/shared/package*.json ./packages/shared/
|
||||
COPY tsconfig.base.json ./
|
||||
|
||||
RUN npm install -g pnpm
|
||||
|
||||
RUN pnpm install --frozen-lockfile
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN pnpm -r --filter @myst/service build
|
||||
|
||||
ENV PORT=3000
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
CMD ["pnpm", "--filter", "@myst/service", "start"]
|
||||
96
packages/cli/src/commands/config.ts
Normal file
96
packages/cli/src/commands/config.ts
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import * as c from "yoctocolors";
|
||||
|
||||
interface MystConfig {
|
||||
publicUrl?: string;
|
||||
adminUrl?: string;
|
||||
databaseUrl?: string;
|
||||
forgejoBaseUrl?: string;
|
||||
forgejoBotUsername?: string;
|
||||
forgejoPat?: string;
|
||||
grantTokenSecret?: string;
|
||||
}
|
||||
|
||||
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("****");
|
||||
}
|
||||
|
||||
function parseEnvFile(envPath: string): MystConfig {
|
||||
const config: MystConfig = {};
|
||||
|
||||
const content = fs.readFileSync(envPath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
|
||||
const [key, ...valueParts] = trimmed.split("=");
|
||||
if (!key) continue;
|
||||
|
||||
const value = valueParts.join("=").trim();
|
||||
|
||||
switch (key) {
|
||||
case "MYST_PUBLIC_URL":
|
||||
config.publicUrl = value;
|
||||
break;
|
||||
case "MYST_ADMIN_URL":
|
||||
config.adminUrl = value;
|
||||
break;
|
||||
case "DATABASE_URL":
|
||||
config.databaseUrl = value;
|
||||
break;
|
||||
case "FORGEJO_BASE_URL":
|
||||
config.forgejoBaseUrl = value;
|
||||
break;
|
||||
case "FORGEJO_BOT_USERNAME":
|
||||
config.forgejoBotUsername = value;
|
||||
break;
|
||||
case "FORGEJO_PAT":
|
||||
config.forgejoPat = value;
|
||||
break;
|
||||
case "GRANT_TOKEN_SECRET":
|
||||
config.grantTokenSecret = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
export async function configCommand(): Promise<void> {
|
||||
const envPath = path.join(process.cwd(), ".env");
|
||||
|
||||
let config: MystConfig;
|
||||
|
||||
try {
|
||||
config = parseEnvFile(envPath);
|
||||
} catch (error) {
|
||||
const err = error as NodeJS.ErrnoException;
|
||||
if (err.code === "ENOENT") {
|
||||
process.stderr.write(
|
||||
`${c.red("Error:")} .env file not found in ${process.cwd()}\n`,
|
||||
);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
process.stdout.write(c.bold("Myst Configuration\n"));
|
||||
process.stdout.write(`${c.dim("─".repeat(40))}\n`);
|
||||
|
||||
process.stdout.write(`${c.bold("MYST_PUBLIC_URL:")} ${config.publicUrl || c.dim("not set")}\n`);
|
||||
process.stdout.write(`${c.bold("MYST_ADMIN_URL:")} ${config.adminUrl || c.dim("not set")}\n`);
|
||||
process.stdout.write(`${c.bold("DATABASE_URL:")} ${config.databaseUrl || c.dim("not set")}\n`);
|
||||
process.stdout.write(`${c.bold("FORGEJO_BASE_URL:")} ${config.forgejoBaseUrl || c.dim("not set")}\n`);
|
||||
process.stdout.write(`${c.bold("FORGEJO_BOT_USERNAME:")} ${config.forgejoBotUsername || c.dim("not set")}\n`);
|
||||
process.stdout.write(`${c.bold("FORGEJO_PAT:")} ${redactSecret(config.forgejoPat)}\n`);
|
||||
process.stdout.write(`${c.bold("GRANT_TOKEN_SECRET:")} ${redactSecret(config.grantTokenSecret)}\n`);
|
||||
|
||||
process.exitCode = 0;
|
||||
}
|
||||
226
packages/cli/src/commands/doctor.ts
Normal file
226
packages/cli/src/commands/doctor.ts
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import * as c from "yoctocolors";
|
||||
import * as p from "@clack/prompts";
|
||||
|
||||
interface MystConfig {
|
||||
publicUrl?: string;
|
||||
adminUrl?: string;
|
||||
databaseUrl?: string;
|
||||
forgejoBaseUrl?: string;
|
||||
forgejoBotUsername?: string;
|
||||
forgejoPat?: string;
|
||||
grantTokenSecret?: string;
|
||||
}
|
||||
|
||||
function parseEnvFile(envPath: string): MystConfig {
|
||||
const config: MystConfig = {};
|
||||
|
||||
const content = fs.readFileSync(envPath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
|
||||
const [key, ...valueParts] = trimmed.split("=");
|
||||
if (!key) continue;
|
||||
|
||||
const value = valueParts.join("=").trim();
|
||||
|
||||
switch (key) {
|
||||
case "MYST_PUBLIC_URL":
|
||||
config.publicUrl = value;
|
||||
break;
|
||||
case "MYST_ADMIN_URL":
|
||||
config.adminUrl = value;
|
||||
break;
|
||||
case "DATABASE_URL":
|
||||
config.databaseUrl = value;
|
||||
break;
|
||||
case "FORGEJO_BASE_URL":
|
||||
config.forgejoBaseUrl = value;
|
||||
break;
|
||||
case "FORGEJO_BOT_USERNAME":
|
||||
config.forgejoBotUsername = value;
|
||||
break;
|
||||
case "FORGEJO_PAT":
|
||||
config.forgejoPat = value;
|
||||
break;
|
||||
case "GRANT_TOKEN_SECRET":
|
||||
config.grantTokenSecret = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
type CheckResult = {
|
||||
name: string;
|
||||
success: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
async function checkForgejoReachability(baseUrl: string): Promise<CheckResult> {
|
||||
try {
|
||||
const response = await fetch(baseUrl, { method: "GET" });
|
||||
if (response.ok || response.status === 200) {
|
||||
return { name: "Forgejo reachability", success: true };
|
||||
}
|
||||
return {
|
||||
name: "Forgejo reachability",
|
||||
success: false,
|
||||
message: `HTTP ${response.status}`,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return {
|
||||
name: "Forgejo reachability",
|
||||
success: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function checkForgejoApi(
|
||||
baseUrl: string,
|
||||
pat: string,
|
||||
): Promise<CheckResult> {
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/v1/user`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${pat}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message: "Invalid or expired token",
|
||||
};
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message: `HTTP ${response.status}`,
|
||||
};
|
||||
}
|
||||
|
||||
const user = (await response.json()) as { login: string };
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: true,
|
||||
message: `Authenticated as ${c.green(user.login)}`,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function doctorCommand(): Promise<void> {
|
||||
p.intro(c.bold("myst doctor"));
|
||||
|
||||
const envPath = path.join(process.cwd(), ".env");
|
||||
const results: CheckResult[] = [];
|
||||
|
||||
let config: MystConfig;
|
||||
|
||||
try {
|
||||
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;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
function addResult(name: string, success: boolean, message?: string) {
|
||||
results.push({ name, success, ...(message && { message }) });
|
||||
}
|
||||
|
||||
addResult("Config file exists", true, envPath);
|
||||
|
||||
const requiredFields = [
|
||||
{ key: "publicUrl", name: "MYST_PUBLIC_URL" },
|
||||
{ key: "adminUrl", name: "MYST_ADMIN_URL" },
|
||||
{ key: "databaseUrl", name: "DATABASE_URL" },
|
||||
{ key: "forgejoBaseUrl", name: "FORGEJO_BASE_URL" },
|
||||
{ key: "forgejoBotUsername", name: "FORGEJO_BOT_USERNAME" },
|
||||
{ key: "forgejoPat", name: "FORGEJO_PAT" },
|
||||
{ key: "grantTokenSecret", name: "GRANT_TOKEN_SECRET" },
|
||||
];
|
||||
|
||||
for (const field of requiredFields) {
|
||||
const value = config[field.key as keyof MystConfig];
|
||||
addResult(`${field.name} set`, !!value, value ? "configured" : "missing");
|
||||
}
|
||||
|
||||
if (config.databaseUrl) {
|
||||
try {
|
||||
const url = new URL(config.databaseUrl);
|
||||
const valid = url.protocol === "postgres:" ||
|
||||
url.protocol === "postgresql:";
|
||||
addResult("Database URL format", valid, valid ? "valid" : "invalid");
|
||||
} catch {
|
||||
addResult("Database URL format", false, "invalid format");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.forgejoBaseUrl && config.forgejoPat) {
|
||||
const reachability = await checkForgejoReachability(config.forgejoBaseUrl);
|
||||
results.push(reachability);
|
||||
|
||||
if (reachability.success) {
|
||||
const apiToken = await checkForgejoApi(
|
||||
config.forgejoBaseUrl,
|
||||
config.forgejoPat,
|
||||
);
|
||||
results.push(apiToken);
|
||||
}
|
||||
}
|
||||
|
||||
const s = p.spinner();
|
||||
|
||||
try {
|
||||
s.start("Running diagnostics");
|
||||
|
||||
s.stop("Diagnostics complete");
|
||||
|
||||
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"));
|
||||
} catch (error) {
|
||||
s.stop();
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
p.cancel(`${c.red("Error:")} ${message}`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
260
packages/cli/src/commands/verify.ts
Normal file
260
packages/cli/src/commands/verify.ts
Normal file
|
|
@ -0,0 +1,260 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import * as c from "yoctocolors";
|
||||
import * as p from "@clack/prompts";
|
||||
|
||||
interface MystConfig {
|
||||
forgejoBaseUrl?: string;
|
||||
forgejoBotUsername?: string;
|
||||
forgejoPat?: string;
|
||||
}
|
||||
|
||||
function parseEnvFile(envPath: string): MystConfig {
|
||||
const config: MystConfig = {};
|
||||
|
||||
const content = fs.readFileSync(envPath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
|
||||
const [key, ...valueParts] = trimmed.split("=");
|
||||
if (!key) continue;
|
||||
|
||||
const value = valueParts.join("=").trim();
|
||||
|
||||
switch (key) {
|
||||
case "FORGEJO_BASE_URL":
|
||||
config.forgejoBaseUrl = value;
|
||||
break;
|
||||
case "FORGEJO_BOT_USERNAME":
|
||||
config.forgejoBotUsername = value;
|
||||
break;
|
||||
case "FORGEJO_PAT":
|
||||
config.forgejoPat = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
type CheckResult = {
|
||||
name: string;
|
||||
success: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
async function checkForgejoReachability(baseUrl: string): Promise<CheckResult> {
|
||||
try {
|
||||
const response = await fetch(baseUrl, { method: "GET" });
|
||||
if (response.ok || response.status === 200) {
|
||||
return { name: "Forgejo reachability", success: true };
|
||||
}
|
||||
return {
|
||||
name: "Forgejo reachability",
|
||||
success: false,
|
||||
message: `HTTP ${response.status}`,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return {
|
||||
name: "Forgejo reachability",
|
||||
success: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function checkForgejoApi(
|
||||
baseUrl: string,
|
||||
pat: string,
|
||||
): Promise<CheckResult> {
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/v1/user`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${pat}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message: "Invalid or expired token",
|
||||
};
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message: `HTTP ${response.status}`,
|
||||
};
|
||||
}
|
||||
|
||||
const user = (await response.json()) as { login: string };
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: true,
|
||||
message: `Authenticated as ${c.green(user.login)}`,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return {
|
||||
name: "Forgejo API token",
|
||||
success: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function checkRepoAccess(
|
||||
baseUrl: string,
|
||||
pat: string,
|
||||
username: string,
|
||||
): Promise<CheckResult> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${baseUrl}/api/v1/users/${username}/repos?limit=1`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${pat}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
name: "Repository access",
|
||||
success: false,
|
||||
message: `HTTP ${response.status}`,
|
||||
};
|
||||
}
|
||||
|
||||
const repos = (await response.json()) as Array<{ name: string }>;
|
||||
if (repos.length > 0) {
|
||||
const firstRepo = repos[0]!;
|
||||
return {
|
||||
name: "Repository access",
|
||||
success: true,
|
||||
message: `Can access repos (sample: ${c.green(firstRepo.name)})`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
name: "Repository access",
|
||||
success: true,
|
||||
message: "Token valid but no repos found",
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
return {
|
||||
name: "Repository access",
|
||||
success: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyCommand(): Promise<void> {
|
||||
p.intro(c.bold("myst verify"));
|
||||
|
||||
const envPath = path.join(process.cwd(), ".env");
|
||||
|
||||
let config: MystConfig;
|
||||
|
||||
try {
|
||||
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;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
const missing: string[] = [];
|
||||
|
||||
if (!config.forgejoBaseUrl) {
|
||||
missing.push("FORGEJO_BASE_URL");
|
||||
}
|
||||
if (!config.forgejoPat) {
|
||||
missing.push("FORGEJO_PAT");
|
||||
}
|
||||
if (!config.forgejoBotUsername) {
|
||||
missing.push("FORGEJO_BOT_USERNAME");
|
||||
}
|
||||
|
||||
if (missing.length > 0) {
|
||||
p.cancel(
|
||||
`${c.red("Error:")} Missing required config: ${missing.join(", ")}`,
|
||||
);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const s = p.spinner();
|
||||
|
||||
try {
|
||||
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("Forgejo verification passed"));
|
||||
} catch (error) {
|
||||
s.stop();
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
p.cancel(`${c.red("Error:")} ${message}`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,14 +5,20 @@ import * as c from "yoctocolors";
|
|||
import pkg from "../package.json" with { type: "json" };
|
||||
|
||||
import { initCommand } from "./commands/init.js";
|
||||
import { configCommand } from "./commands/config.js";
|
||||
import { verifyCommand } from "./commands/verify.js";
|
||||
import { doctorCommand } from "./commands/doctor.js";
|
||||
import { getHelpText } from "./help.js";
|
||||
|
||||
type CommandName = "init";
|
||||
type CommandName = "init" | "config" | "verify" | "doctor";
|
||||
|
||||
type CommandHandler = (args?: string[]) => Promise<void>;
|
||||
|
||||
const commandHandlers: Record<CommandName, CommandHandler> = {
|
||||
init: initCommand,
|
||||
config: configCommand,
|
||||
verify: verifyCommand,
|
||||
doctor: doctorCommand,
|
||||
};
|
||||
|
||||
async function main(): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"start": "node dist/index.js",
|
||||
"check": "tsc -b tsconfig.json",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue