Compare commits
3 commits
main
...
feature/cl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31c3105e34 | ||
|
|
f1a4a6d18c | ||
|
|
c4c41e0e31 |
7 changed files with 113 additions and 106 deletions
|
|
@ -93,7 +93,7 @@ GRANT_TOKEN_SECRET="your-secret-here"
|
|||
|
||||
- [x] Project renamed from Embargo to Myst
|
||||
- [x] CLI scaffold (`myst init`)
|
||||
- [x] Config generation (`.env`, `myst.config.json`)
|
||||
- [x] Config generation (`.env`)
|
||||
- [ ] Service foundation (FastAPI + PostgreSQL)
|
||||
- [ ] `myst forgejo bootstrap` — verify Forgejo connectivity
|
||||
- [ ] `myst doctor` — validate configuration
|
||||
|
|
|
|||
|
|
@ -130,8 +130,7 @@ Only repo owners can create view links for their repos.
|
|||
|
||||
`myst init` should generate all Myst-owned deployment config needed for Phase 1:
|
||||
|
||||
- `.env`
|
||||
- `myst.config.json`
|
||||
- `.env` (environment variables for Docker/container deployment)
|
||||
- any future Myst-only deployment templates, but never Forgejo provisioning files
|
||||
|
||||
`myst init` should provide inline guidance that:
|
||||
|
|
|
|||
|
|
@ -208,7 +208,6 @@ The CLI is the primary way to configure and validate Myst against an existing Fo
|
|||
- generate a grant token secret by default unless the operator pastes one
|
||||
- generate all Myst-owned config files needed for deployment in Phase 1
|
||||
- write `.env`
|
||||
- write `myst.config.json`
|
||||
- print manual follow-up actions for admin URL protection, app deployment, and Forgejo validation
|
||||
- show links to Forgejo docs for API usage, token scopes, repo permissions, and the admin CLI
|
||||
- remind operators to run `myst init` in the directory where Myst will be deployed, usually on the target VPS or server workspace
|
||||
|
|
@ -285,7 +284,6 @@ Forgejo docs to link directly from CLI output:
|
|||
Expected outputs from `init`:
|
||||
|
||||
- `.env`
|
||||
- `myst.config.json`
|
||||
- optional platform templates for Myst only (e.g., Docker Compose for Dokploy)
|
||||
|
||||
## Data Model
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"name": "@myst/cli",
|
||||
"description": "A CLI tool that helps you wire Myst into an existing Forgejo instance with expiring grant access controls for repos you choose to share.",
|
||||
"description": "CLI for configuring and verifying Myst with Forgejo",
|
||||
"version": "0.1.0",
|
||||
"author": {
|
||||
"name": "Zach Tatman",
|
||||
"email": "zach.c.tatman@gmail.com"
|
||||
"email": "zach.tatman.dev@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ZTatman/myst/issues"
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ function validateHttpUrlOrHost(
|
|||
try {
|
||||
const normalized = normalizeHttpUrl(value);
|
||||
const url = new URL(normalized);
|
||||
|
||||
if (!url.hostname) {
|
||||
return `${label} must include a domain or hostname`;
|
||||
}
|
||||
|
|
@ -125,7 +124,7 @@ async function preflightWriteTargets(filePaths: string[]): Promise<void> {
|
|||
* @param _args - The command arguments.
|
||||
* @returns {Promise<void>} A promise that resolves when the initialization is complete.
|
||||
*/
|
||||
export async function initCommand(_args: string[]): Promise<void> {
|
||||
export async function initCommand(args?: string[]): Promise<void> {
|
||||
p.intro(c.bold("myst init"));
|
||||
p.note(
|
||||
[
|
||||
|
|
@ -224,30 +223,14 @@ export async function initCommand(_args: string[]): Promise<void> {
|
|||
publicUrl,
|
||||
});
|
||||
|
||||
const configContent = JSON.stringify(
|
||||
{
|
||||
adminUrl,
|
||||
forgejoBaseUrl,
|
||||
forgejoBotUsername: answers.forgejoBotUsername,
|
||||
publicUrl,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
);
|
||||
|
||||
const outDir = process.cwd();
|
||||
const envPath = path.join(outDir, ".env");
|
||||
const configPath = path.join(outDir, "myst.config.json");
|
||||
|
||||
await preflightWriteTargets([envPath, configPath]);
|
||||
await preflightWriteTargets([envPath]);
|
||||
await writePrivateFile(envPath, envContent);
|
||||
await writePrivateFile(configPath, configContent);
|
||||
|
||||
s.stop("Config files generated");
|
||||
p.note(
|
||||
[c.green(".env"), c.green("myst.config.json")].join("\n"),
|
||||
c.yellow("Files written to current directory"),
|
||||
);
|
||||
p.note(c.green(".env"), c.yellow("File written to current directory"));
|
||||
p.note(
|
||||
[
|
||||
`${c.bold("Public viewer URL:")} ${c.cyan(publicUrl)}`,
|
||||
|
|
|
|||
64
packages/cli/src/help.ts
Normal file
64
packages/cli/src/help.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import * as c from "yoctocolors";
|
||||
import pkg from "../package.json" with { type: "json" };
|
||||
|
||||
const PADDING = 4;
|
||||
const INDENT = " ";
|
||||
|
||||
type HelpRow = {
|
||||
name: string;
|
||||
desc?: string;
|
||||
};
|
||||
|
||||
const commandRows: HelpRow[] = [
|
||||
{ name: "init", desc: "Generate config files with the proper env variables" },
|
||||
];
|
||||
|
||||
const globalFlagRows: HelpRow[] = [
|
||||
{ name: "-h, --help", desc: "Display help information" },
|
||||
{ name: "-v, --version", desc: "Display package version number" },
|
||||
];
|
||||
|
||||
function formatSection(
|
||||
header: string,
|
||||
rows: HelpRow[],
|
||||
colWidth: number,
|
||||
): string {
|
||||
return [
|
||||
c.bold(`${header}:`),
|
||||
...rows.map((row) =>
|
||||
row.desc
|
||||
? `${INDENT}${row.name.padEnd(colWidth)}${row.desc}`
|
||||
: `${INDENT}${row.name}`,
|
||||
),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the help text for the CLI.
|
||||
* @returns The help text.
|
||||
*/
|
||||
export function getHelpText(): string {
|
||||
const allRows = [...commandRows, ...globalFlagRows];
|
||||
const colWidth = Math.max(...allRows.map((row) => row.name.length)) + PADDING;
|
||||
|
||||
return [
|
||||
c.yellow("myst"),
|
||||
"",
|
||||
`${INDENT}${pkg.description}`,
|
||||
"",
|
||||
formatSection(
|
||||
"Usage",
|
||||
[
|
||||
{
|
||||
name: "myst <command> [options/flags] [arguments]",
|
||||
},
|
||||
],
|
||||
colWidth,
|
||||
),
|
||||
"",
|
||||
formatSection("Commands", commandRows, colWidth),
|
||||
"",
|
||||
formatSection("Global flags", globalFlagRows, colWidth),
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
|
@ -1,99 +1,50 @@
|
|||
#!/usr/bin/env node
|
||||
import { parseArgs } from "node:util";
|
||||
|
||||
import * as c from "yoctocolors";
|
||||
import pkg from "../package.json" with { type: "json" };
|
||||
|
||||
import { initCommand } from "./commands/init.js";
|
||||
|
||||
const PADDING = 4;
|
||||
const INDENT = " ";
|
||||
const USAGE = "myst <command> [options/flags] [arguments]";
|
||||
import { getHelpText } from "./help.js";
|
||||
|
||||
type CommandName = "init";
|
||||
|
||||
interface Option {
|
||||
name: CommandName | string | string[];
|
||||
desc: string;
|
||||
}
|
||||
type CommandHandler = (args?: string[]) => Promise<void>;
|
||||
|
||||
interface CLIConfig {
|
||||
commands: Option[];
|
||||
globalFlags: Option[];
|
||||
}
|
||||
|
||||
const config: CLIConfig = {
|
||||
commands: [
|
||||
{ name: "init", desc: "Bootstrap Myst and generate config/env files" },
|
||||
],
|
||||
globalFlags: [
|
||||
{ name: ["-h", "--help"], desc: "Display help information" },
|
||||
{ name: ["-v", "--version"], desc: "Display package version number" },
|
||||
],
|
||||
const commandHandlers: Record<CommandName, CommandHandler> = {
|
||||
init: initCommand,
|
||||
};
|
||||
|
||||
const splitCamelCaseWords = (text: string): string =>
|
||||
text.replace(/([a-z])([A-Z])/g, "$1 $2");
|
||||
|
||||
const toTitleCase = (text: string): string =>
|
||||
text.replace(/\b\w/g, (c) => c.toUpperCase());
|
||||
|
||||
function formatSection(
|
||||
header: string,
|
||||
options: Option[],
|
||||
colWidth: number,
|
||||
): string {
|
||||
const rows = options.map((o) => {
|
||||
const name = Array.isArray(o.name) ? o.name.join(", ") : o.name;
|
||||
return `${INDENT}${name.padEnd(colWidth)}${o.desc}`;
|
||||
});
|
||||
const title = toTitleCase(splitCamelCaseWords(header));
|
||||
return [c.bold(title + ":"), ...rows].join("\n");
|
||||
}
|
||||
|
||||
const allOptions = Object.values(config).flat();
|
||||
const COL_WIDTH =
|
||||
Math.max(
|
||||
...allOptions.map((o) =>
|
||||
Array.isArray(o.name) ? o.name.join(", ").length : o.name.length,
|
||||
),
|
||||
) + PADDING;
|
||||
|
||||
const HELP_TEXT = `
|
||||
${c.yellow(pkg.name)}
|
||||
|
||||
|
||||
${INDENT}${USAGE}
|
||||
|
||||
|
||||
${Object.entries(config)
|
||||
.map(([header, options]) =>
|
||||
formatSection(header, options as Option[], COL_WIDTH),
|
||||
)
|
||||
.join("\n\n")}
|
||||
`;
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const [command, ...options] = process.argv.slice(2);
|
||||
const flags = options.filter((o) => o.startsWith("-"));
|
||||
const { values, positionals } = parseArgs({
|
||||
args: process.argv.slice(2),
|
||||
options: {
|
||||
version: {
|
||||
type: "boolean",
|
||||
short: "v",
|
||||
},
|
||||
help: {
|
||||
type: "boolean",
|
||||
short: "h",
|
||||
},
|
||||
output: {
|
||||
type: "string",
|
||||
short: "o",
|
||||
},
|
||||
},
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
if (flags.includes("-v") || flags.includes("--version")) {
|
||||
process.stdout.write(`${pkg.version}\n`);
|
||||
const command = positionals[0];
|
||||
|
||||
if (!command) {
|
||||
process.stdout.write(getHelpText());
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!command || flags.includes("-h") || flags.includes("--help")) {
|
||||
process.stdout.write(`${HELP_TEXT}`);
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
const knownCommands: Record<CommandName, (args: string[]) => Promise<void>> =
|
||||
{
|
||||
init: initCommand,
|
||||
};
|
||||
|
||||
const isKnownCommand = (cmd: string): cmd is CommandName =>
|
||||
cmd in knownCommands;
|
||||
cmd in commandHandlers;
|
||||
|
||||
if (!isKnownCommand(command)) {
|
||||
process.stderr.write(`Unknown command: ${command}\n`);
|
||||
|
|
@ -101,8 +52,20 @@ async function main(): Promise<void> {
|
|||
return;
|
||||
}
|
||||
|
||||
if (values.help) {
|
||||
process.stdout.write(getHelpText());
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (values.version) {
|
||||
process.stdout.write(`${pkg.version}`);
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await knownCommands[command](options);
|
||||
await commandHandlers[command]();
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
process.stderr.write(`${c.red("Error:")} ${message}\n`);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue