mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-22 21:07:57 +00:00
refactor(verify): simplify by extracting functions and removing duplication
This commit is contained in:
parent
18d3b175de
commit
6588fad461
1 changed files with 154 additions and 140 deletions
|
|
@ -12,7 +12,7 @@ import {
|
||||||
} from "../utils/config.js";
|
} from "../utils/config.js";
|
||||||
import {
|
import {
|
||||||
checkForgejoReachability,
|
checkForgejoReachability,
|
||||||
checkForgejoApi,
|
checkForgejoApiAuthentication,
|
||||||
checkRepoAccess,
|
checkRepoAccess,
|
||||||
} from "../utils/forgejo-checks.js";
|
} from "../utils/forgejo-checks.js";
|
||||||
import { CommandConfig } from "../cli-router.js";
|
import { CommandConfig } from "../cli-router.js";
|
||||||
|
|
@ -21,84 +21,126 @@ interface VerifyOptions {
|
||||||
diagnostic?: boolean;
|
diagnostic?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CheckResult {
|
||||||
|
name: string;
|
||||||
|
success: boolean;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
async function verifyCommand(opts: VerifyOptions): Promise<void> {
|
async function verifyCommand(opts: VerifyOptions): Promise<void> {
|
||||||
const diagnostic = opts.diagnostic ?? false;
|
const isDiagnostic = opts.diagnostic ?? false;
|
||||||
p.intro(c.bold("myst config verify"));
|
p.intro(c.bold("myst config verify"));
|
||||||
|
|
||||||
|
const config = loadConfig();
|
||||||
|
const results: CheckResult[] = collectResults(config);
|
||||||
|
|
||||||
|
if (isDiagnostic) {
|
||||||
|
await displayDiagnosticResults(results, config);
|
||||||
|
} else {
|
||||||
|
await verifyResults(results, config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadConfig(): Partial<MystEnvironmentVariables> {
|
||||||
const envPath = path.join(process.cwd(), ".env");
|
const envPath = path.join(process.cwd(), ".env");
|
||||||
|
|
||||||
let config: Partial<MystEnvironmentVariables>;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
config = parseEnvFile(envPath);
|
return parseEnvFile(envPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const err = error as NodeJS.ErrnoException;
|
const err = error as NodeJS.ErrnoException;
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
p.cancel(`${c.red("Error:")} .env file not found in ${process.cwd()}`);
|
p.cancel(`${c.red("Error:")} .env file not found in ${process.cwd()}`);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const results: { name: string; success: boolean; message?: string }[] = [];
|
function collectResults(
|
||||||
|
config: Partial<MystEnvironmentVariables>,
|
||||||
|
): CheckResult[] {
|
||||||
|
const results: CheckResult[] = [];
|
||||||
|
|
||||||
function addResult(name: string, success: boolean, message?: string) {
|
function addResult(name: string, success: boolean, message?: string) {
|
||||||
results.push({ name, success, ...(message !== undefined && { message }) });
|
results.push({ name, success, ...(message !== undefined && { message }) });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (diagnostic) {
|
const missing: string[] = [];
|
||||||
addResult("Config file exists", true, envPath);
|
for (const key of REQUIRED_KEYS) {
|
||||||
|
if (!config[key]) {
|
||||||
|
missing.push(key.replace(/(?<=[a-z])(?=[A-Z])/g, "_").toUpperCase());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const [envKey, configKey] of Object.entries(ENV_MAPPING)) {
|
if (missing.length > 0) {
|
||||||
|
addResult("Required config", false, `Missing: ${missing.join(", ")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [, configKey] of Object.entries(ENV_MAPPING)) {
|
||||||
const value = config[configKey];
|
const value = config[configKey];
|
||||||
addResult(`${configKey} set`, !!value, value ? "configured" : "missing");
|
addResult(`${configKey} set`, !!value, value ? "configured" : "missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.databaseUrl) {
|
if (config.databaseUrl) {
|
||||||
const valid = validateDatabaseUrl(config.databaseUrl);
|
const isDbValid = validateDatabaseUrl(config.databaseUrl);
|
||||||
addResult("Database URL format", valid, valid ? "valid" : "invalid");
|
addResult("Database URL format", isDbValid, isDbValid ? "valid" : "invalid");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const forgejoConfigured =
|
const isConfigured =
|
||||||
config.forgejoBaseUrl && config.forgejoPat && config.forgejoBotUsername;
|
config.forgejoBaseUrl && config.forgejoPat && config.forgejoBotUsername;
|
||||||
|
|
||||||
if (forgejoConfigured) {
|
if (!isConfigured) {
|
||||||
const forgejoBaseUrl = config.forgejoBaseUrl!;
|
return results;
|
||||||
const forgejoPat = config.forgejoPat!;
|
}
|
||||||
const forgejoBotUsername = config.forgejoBotUsername!;
|
|
||||||
|
const { forgejoBaseUrl, forgejoPat, forgejoBotUsername } = config;
|
||||||
|
|
||||||
|
addResult("Forgejo configured", true);
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runForgejoChecks(config: Partial<MystEnvironmentVariables>): Promise<CheckResult[]> {
|
||||||
|
const results: CheckResult[] = [];
|
||||||
|
|
||||||
|
const { forgejoBaseUrl, forgejoPat, forgejoBotUsername } = config;
|
||||||
|
|
||||||
|
if (!forgejoBaseUrl || !forgejoPat || !forgejoBotUsername) {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
const reachability = await checkForgejoReachability(forgejoBaseUrl);
|
const reachability = await checkForgejoReachability(forgejoBaseUrl);
|
||||||
results.push(reachability);
|
results.push(reachability);
|
||||||
|
|
||||||
if (reachability.success) {
|
if (!reachability.success) {
|
||||||
const apiToken = await checkForgejoApi(
|
return results;
|
||||||
forgejoBaseUrl,
|
}
|
||||||
forgejoPat,
|
|
||||||
diagnostic,
|
const auth = await checkForgejoApiAuthentication(forgejoBaseUrl, forgejoPat);
|
||||||
);
|
results.push(auth);
|
||||||
results.push(apiToken);
|
|
||||||
|
if (!auth.success) {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
if (apiToken.success) {
|
|
||||||
const repoAccess = await checkRepoAccess(
|
const repoAccess = await checkRepoAccess(
|
||||||
forgejoBaseUrl,
|
forgejoBaseUrl,
|
||||||
forgejoPat,
|
forgejoPat,
|
||||||
forgejoBotUsername,
|
forgejoBotUsername,
|
||||||
);
|
);
|
||||||
results.push(repoAccess);
|
results.push(repoAccess);
|
||||||
}
|
|
||||||
}
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
const s = p.spinner();
|
async function displayDiagnosticResults(
|
||||||
|
results: CheckResult[],
|
||||||
|
config: Partial<MystEnvironmentVariables>,
|
||||||
|
): Promise<void> {
|
||||||
|
const forgejoResults = await runForgejoChecks(config);
|
||||||
|
const allResults = [...results, ...forgejoResults];
|
||||||
|
|
||||||
try {
|
const lines = allResults.map((r) => {
|
||||||
if (diagnostic) {
|
|
||||||
s.start("Running diagnostics");
|
|
||||||
s.stop("Diagnostics complete");
|
|
||||||
|
|
||||||
const lines = results.map((r) => {
|
|
||||||
const icon = r.success ? c.green("✓") : c.red("✗");
|
const icon = r.success ? c.green("✓") : c.red("✗");
|
||||||
const msg = r.message ? ` ${r.message}` : "";
|
const msg = r.message ? ` ${r.message}` : "";
|
||||||
return `${icon} ${r.name}${msg}`;
|
return `${icon} ${r.name}${msg}`;
|
||||||
|
|
@ -106,7 +148,7 @@ async function verifyCommand(opts: VerifyOptions): Promise<void> {
|
||||||
|
|
||||||
p.note(lines.join("\n"), c.yellow("Results"));
|
p.note(lines.join("\n"), c.yellow("Results"));
|
||||||
|
|
||||||
const failures = results.filter((r) => !r.success);
|
const failures = allResults.filter((r) => !r.success);
|
||||||
|
|
||||||
if (failures.length > 0) {
|
if (failures.length > 0) {
|
||||||
p.outro(c.red(`${failures.length} issue(s) found`));
|
p.outro(c.red(`${failures.length} issue(s) found`));
|
||||||
|
|
@ -115,87 +157,59 @@ async function verifyCommand(opts: VerifyOptions): Promise<void> {
|
||||||
}
|
}
|
||||||
|
|
||||||
p.outro(c.green("All checks passed"));
|
p.outro(c.green("All checks passed"));
|
||||||
} else {
|
|
||||||
const missing: string[] = [];
|
|
||||||
|
|
||||||
for (const key of REQUIRED_KEYS) {
|
|
||||||
if (!config[key]) {
|
|
||||||
missing.push(key.replace(/(?<=[a-z])(?=[A-Z])/g, "_").toUpperCase());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (missing.length > 0) {
|
async function verifyResults(
|
||||||
p.cancel(
|
results: CheckResult[],
|
||||||
`${c.red("Error:")} Missing required config: ${missing.join(", ")}`,
|
config: Partial<MystEnvironmentVariables>,
|
||||||
);
|
): Promise<void> {
|
||||||
|
const failures = results.filter((r) => !r.success);
|
||||||
|
|
||||||
|
if (failures.length > 0) {
|
||||||
|
const msg = failures[0]?.message ?? "Unknown error";
|
||||||
|
p.cancel(`${c.red("Error:")} ${msg}`);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// After this point, required keys are guaranteed to exist
|
const s = p.spinner();
|
||||||
if (!config.forgejoBaseUrl || !config.forgejoPat || !config.forgejoBotUsername) {
|
|
||||||
process.exitCode = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const forgejoBaseUrl = config.forgejoBaseUrl;
|
|
||||||
const forgejoPat = config.forgejoPat;
|
|
||||||
const forgejoBotUsername = config.forgejoBotUsername;
|
|
||||||
|
|
||||||
s.start("Verifying Forgejo configuration");
|
s.start("Verifying Forgejo configuration");
|
||||||
|
|
||||||
const reachability = await checkForgejoReachability(forgejoBaseUrl);
|
const forgejoResults = await runForgejoChecks(config);
|
||||||
|
|
||||||
if (!reachability.success) {
|
const firstFailure = forgejoResults.find((r) => !r.success);
|
||||||
s.stop(reachability.message);
|
|
||||||
p.cancel(`${c.red("✗")} ${reachability.name}: ${reachability.message}`);
|
if (firstFailure) {
|
||||||
|
s.stop(firstFailure.message);
|
||||||
|
p.cancel(`${c.red("✗")} ${firstFailure.name}: ${firstFailure.message}`);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiToken = await checkForgejoApi(
|
if (forgejoResults.length < 3) {
|
||||||
forgejoBaseUrl,
|
s.stop("No Forgejo configuration");
|
||||||
forgejoPat,
|
p.cancel(`${c.red("Error:")} Forgejo not configured`);
|
||||||
);
|
|
||||||
|
|
||||||
if (!apiToken.success) {
|
|
||||||
s.stop(apiToken.message);
|
|
||||||
p.cancel(`${c.red("✗")} ${apiToken.name}: ${apiToken.message}`);
|
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const repoAccess = await checkRepoAccess(
|
|
||||||
forgejoBaseUrl,
|
|
||||||
forgejoPat,
|
|
||||||
forgejoBotUsername,
|
|
||||||
);
|
|
||||||
|
|
||||||
s.stop("Verification complete");
|
s.stop("Verification complete");
|
||||||
|
|
||||||
|
const reachability = forgejoResults[0]!;
|
||||||
|
const auth = forgejoResults[1]!;
|
||||||
|
const repoAccess = forgejoResults[2]!;
|
||||||
|
|
||||||
p.note(
|
p.note(
|
||||||
[
|
[
|
||||||
`${c.green("✓")} ${reachability.name}`,
|
`${c.green("✓")} ${reachability.name}`,
|
||||||
`${c.green("✓")} ${apiToken.name}: ${apiToken.message}`,
|
`${c.green("✓")} ${auth.name}: ${auth.message}`,
|
||||||
`${c.green(repoAccess.success ? "✓" : "✗")} ${repoAccess.name}: ${repoAccess.message}`,
|
`${c.green(repoAccess.success ? "✓" : "✗")} ${repoAccess.name}: ${repoAccess.message}`,
|
||||||
].join("\n"),
|
].join("\n"),
|
||||||
c.yellow("Results"),
|
c.yellow("Results"),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!repoAccess.success) {
|
|
||||||
process.exitCode = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
p.outro(c.green("Verification passed"));
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const commandConfig: CommandConfig = {
|
export const commandConfig: CommandConfig = {
|
||||||
description: "Verify Myst configuration and connection to Forgejo",
|
description: "Verify Myst configuration and connection to Forgejo",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue