refactor(cli): simplify base url validation/normalization, update readme

and docs to reflect possible deployment configurations
This commit is contained in:
ZTatman 2026-04-17 12:40:58 -04:00
parent b87c4554fb
commit cd216cd660
3 changed files with 28 additions and 40 deletions

View file

@ -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
```

View file

@ -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(

View file

@ -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}`,
];