From cd216cd660f5d252ee4fa4efe3ece557c2cbee97 Mon Sep 17 00:00:00 2001 From: ZTatman Date: Fri, 17 Apr 2026 12:40:58 -0400 Subject: [PATCH] refactor(cli): simplify base url validation/normalization, update readme and docs to reflect possible deployment configurations --- README.md | 8 ++-- packages/cli/src/commands/config/init.ts | 55 +++++++++--------------- packages/cli/src/utils/config.ts | 5 ++- 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 1e1accb..baa2ab8 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ myst config init Prompts for: -- Forgejo API URL — internal address when co-located (`http://forgejo:3000`) or public URL for cross-host (`https://git.example.com`) +- Forgejo API URL — Docker: `http://forgejo:3000`, bare metal: `http://localhost:3000`, cross-host: `https://git.example.com` - Forgejo PAT (with `read:user` and `read:repository` scopes) ### 2. Deploy with Docker Compose @@ -82,9 +82,11 @@ Myst should not assume a VPS-only environment. The requirement is an existing Fo Create a `.env` file with: ```env -# Same-host (Docker network or localhost): +# Docker (same Compose network): FORGEJO_BASE_URL=http://forgejo:3000 -# Cross-host alternative: +# Bare metal / non-container: +# FORGEJO_BASE_URL=http://localhost:3000 +# Cross-host: # FORGEJO_BASE_URL=https://git.example.com FORGEJO_PAT=forgejo_pat_xxxxxxxxxxxx ``` diff --git a/packages/cli/src/commands/config/init.ts b/packages/cli/src/commands/config/init.ts index 16e05c4..f497b99 100644 --- a/packages/cli/src/commands/config/init.ts +++ b/packages/cli/src/commands/config/init.ts @@ -7,45 +7,29 @@ import * as p from "@clack/prompts"; import { generateEnvFile, SECRET_ENV_KEYS } 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://"); - } - - if (hasProtocol) return trimmed; - - // Infer scheme from the host shape: - // - localhost, IP literals, or single-label hosts (Docker service names - // like `forgejo`) default to http:// since they're typically internal. - // - Anything that looks like a public DNS name (contains a dot) defaults - // to https:// to preserve the existing behavior for `git.example.com`. - const host = trimmed.split("/")[0]?.split(":")[0] ?? ""; - const isInternalHost = - host === "localhost" || - /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || - !host.includes("."); - - return `${isInternalHost ? "http" : "https"}://${trimmed}`; -} - function validateHttpUrlOrHost( value: string | undefined, label: string, ): string | undefined { - if (!value?.trim()) return `${label} is required`; + const trimmed = value?.trim(); + if (!trimmed) return `${label} is required`; + + const hasProtocol = /^[a-z]+:\/\//i.test(trimmed); + const isHttp = /^https?:\/\//i.test(trimmed); + + if (hasProtocol && !isHttp) { + return `${label} must use http:// or https://`; + } + + if (!hasProtocol) { + return `${label} must include a scheme — e.g. http://forgejo:3000 or https://git.example.com`; + } try { - const normalized = normalizeHttpUrl(value); - const url = new URL(normalized); - if (!url.hostname) { - return `${label} must include a domain or hostname`; - } + const url = new URL(trimmed); + if (!url.hostname) return `${label} must include a domain or hostname`; } catch { - return `${label} must be a valid URL or hostname`; + return `${label} must be a valid URL`; } return undefined; @@ -155,7 +139,7 @@ async function init(opts: { output?: string }) { try { s.start("Generating config files"); - const forgejoBaseUrl = normalizeHttpUrl(answers.forgejoBaseUrl); + const forgejoBaseUrl = answers.forgejoBaseUrl.trim(); const envContent = generateEnvFile({ forgejoBaseUrl, @@ -186,8 +170,9 @@ async function init(opts: { output?: string }) { `${c.bold("Forgejo API URL:")} ${c.cyan(forgejoBaseUrl)}`, "", `Myst will use this URL to call Forgejo's API.`, - `- Same host / Docker network: http://forgejo:3000 or http://localhost:3000`, - `- Cross-host deployment: https://git.example.com`, + `- Docker (same Compose network): http://forgejo:3000`, + `- Bare metal / non-container: http://localhost:3000`, + `- Cross-host deployment: https://git.example.com`, ].join("\n"), ); p.log.success( diff --git a/packages/cli/src/utils/config.ts b/packages/cli/src/utils/config.ts index b9a11ef..235a737 100644 --- a/packages/cli/src/utils/config.ts +++ b/packages/cli/src/utils/config.ts @@ -44,8 +44,9 @@ export function parseEnvFile( export function generateEnvFile(options: MystEnvironmentVariables): string { const lines = [ `# Forgejo API endpoint that Myst calls.`, - `# Same-host / Docker: http://forgejo:3000 or http://localhost:3000`, - `# Cross-host: https://git.example.com`, + `# Docker (same Compose network): http://forgejo:3000`, + `# Bare metal / non-container: http://localhost:3000`, + `# Cross-host: https://git.example.com`, `FORGEJO_BASE_URL=${options.forgejoBaseUrl}`, `FORGEJO_PAT=${options.forgejoPat}`, ];