CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | #!/usr/bin/env bun
/**
* Block G3 — `gluecron` CLI.
*
* A dependency-free Bun executable that talks to a Gluecron server using
* either the REST `/api/*` endpoints or the GraphQL endpoint at `/api/graphql`.
*
* gluecron login — store a PAT in ~/.gluecron/config.json
* gluecron whoami — print the logged-in user
* gluecron repo ls — list repos for the logged-in user
* gluecron repo show <owner/name> — pretty-print a repo
* gluecron repo create <name> — create a repo for the logged-in user
* gluecron issues ls <owner/name> — list open issues
* gluecron gql '<query>' — run a GraphQL query verbatim
*
* Build: bun build cli/gluecron.ts --compile --outfile gluecron
* Install: cp gluecron /usr/local/bin/
*/
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
const VERSION = "0.1.0";
const DEFAULT_HOST = process.env.GLUECRON_HOST || "http://localhost:3000";
const CONFIG_DIR = join(homedir(), ".gluecron");
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
// ---------- Config ----------
interface Config {
host: string;
token?: string;
username?: string;
}
export function loadConfig(): Config {
try {
if (existsSync(CONFIG_FILE)) {
const raw = readFileSync(CONFIG_FILE, "utf8");
const parsed = JSON.parse(raw) as Partial<Config>;
return {
host: parsed.host || DEFAULT_HOST,
token: parsed.token,
username: parsed.username,
};
}
} catch {
// fall through
}
return { host: DEFAULT_HOST };
}
export function saveConfig(cfg: Config) {
mkdirSync(CONFIG_DIR, { recursive: true });
writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2), {
mode: 0o600,
});
}
// ---------- HTTP ----------
export async function http(
cfg: Config,
method: string,
path: string,
body?: unknown
): Promise<any> {
const url = cfg.host.replace(/\/+$/, "") + path;
const headers: Record<string, string> = {
"content-type": "application/json",
accept: "application/json",
};
if (cfg.token) headers.authorization = `Bearer ${cfg.token}`;
const res = await fetch(url, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
const text = await res.text();
let json: any;
try {
json = text ? JSON.parse(text) : {};
} catch {
json = { _raw: text };
}
if (!res.ok) {
const msg = json?.error || res.statusText || "request failed";
throw new Error(`[${res.status}] ${msg}`);
}
return json;
}
// ---------- GraphQL Utilities ----------
function sanitizeGraphQLString(input: string): string {
return input.replace(/["\\]/g, '\\$&');
}
// ---------- Commands ----------
export const HELP = `gluecron CLI v${VERSION}
Usage:
gluecron login Save a personal access token
gluecron whoami Print the logged-in user
gluecron repo ls [--user <name>] List repos
gluecron repo show <owner/name> Show a repo
gluecron repo create <name> [--private]
Create a repo
gluecron issues ls <owner/name> List open issues
gluecron gql '<query>' Run a GraphQL query
gluecron host [url] Get or set the server URL
gluecron version Print version
gluecron help Print this help
Env:
GLUECRON_HOST override the server URL (default: ${DEFAULT_HOST})
`;
export async function cmdLogin(
cfg: Config,
prompt: (q: string) => Promise<string>
): Promise<Config> {
const host =
(await prompt(`Server URL [${cfg.host}]: `)) || cfg.host;
const token = await prompt("Personal access token (glc_...): ");
if (!token) throw new Error("token is required");
const next: Config = { host, token };
// Probe /api/user/me to confirm
const me = await http(next, "GET", "/api/user/me").catch(() => null);
if (me?.username) next.username = me.username;
saveConfig(next);
return next;
}
export async function cmdWhoami(cfg: Config): Promise<string> {
if (!cfg.token) return "(not logged in)";
const me = await http(cfg, "GET", "/api/user/me").catch(() => null);
if (!me?.username) return cfg.username || "(unknown)";
return `${me.username} (${me.email || "no email"})`;
}
export async function cmdRepoLs(
cfg: Config,
user?: string
): Promise<Array<{ owner: string; name: string; visibility: string }>> {
const username = user || cfg.username;
if (!username) throw new Error("no user context — log in or pass --user");
const q = `{ user(username:"${sanitizeGraphQLString(username)}") { repos { name visibility } } }`;
const r = await http(cfg, "POST", "/api/graphql", { query: q });
const repos = r?.data?.user?.repos || [];
return repos.map((x: any) => ({
owner: username,
name: x.name,
visibility: x.visibility,
}));
}
export async function cmdRepoShow(
cfg: Config,
slug: string
): Promise<Record<string, any>> {
const [owner, name] = slug.split("/");
if (!owner || !name) throw new Error("expected owner/name");
const q = `{ repository(owner:"${sanitizeGraphQLString(owner)}", name:"${sanitizeGraphQLString(name)}") {
name description visibility starCount forkCount
owner { username }
issues(state:"open", limit:5) { number title }
} }`;
const r = await http(cfg, "POST", "/api/graphql", { query: q });
return r?.data?.repository || null;
}
export async function cmdRepoCreate(
cfg: Config,
name: string,
isPrivate = false
): Promise<any> {
if (!cfg.username) throw new Error("log in first (gluecron login)");
return http(cfg, "POST", "/api/repos", {
name,
owner: cfg.username,
isPrivate,
});
}
export async function cmdIssuesLs(
cfg: Config,
slug: string
): Promise<Array<{ number: number; title: string }>> {
const [owner, name] = slug.split("/");
const q = `{ repository(owner:"${sanitizeGraphQLString(owner)}", name:"${sanitizeGraphQLString(name)}") { issues(state:"open", limit:50) { number title } } }`;
const r = await http(cfg, "POST", "/api/graphql", { query: q });
return r?.data?.repository?.issues || [];
}
export async function cmdGql(cfg: Config, query: string): Promise<any> {
return http(cfg, "POST", "/api/graphql", { query });
}
// ---------- Command Handlers ----------
async function handleHostCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
if (rest[0]) {
cfg.host = rest[0];
saveConfig(cfg);
}
out(cfg.host);
return 0;
}
async function handleLoginCmd(cfg: Config, out: (msg: string) => void): Promise<number> {
const { default: readline } = await import("node:readline/promises");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
const next = await cmdLogin(cfg, (q) => rl.question(q));
out(`Logged in as ${next.username || "(unknown)"}`);
return 0;
} finally {
rl.close();
}
}
async function handleRepoCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
const sub = rest[0];
if (sub === "ls") {
return handleRepoLsCmd(cfg, rest, out);
}
if (sub === "show") {
return handleRepoShowCmd(cfg, rest, out);
}
if (sub === "create") {
return handleRepoCreateCmd(cfg, rest, out);
}
out("usage: gluecron repo (ls|show|create)");
return 1;
}
async function handleRepoLsCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
const userFlagIdx = rest.indexOf("--user");
const user = userFlagIdx >= 0 && userFlagIdx + 1 < rest.length ? rest[userFlagIdx + 1] : undefined;
const repos = await cmdRepoLs(cfg, user);
for (const r of repos) {
out(` ${r.owner}/${r.name} · ${r.visibility}`);
}
return 0;
}
async function handleRepoShowCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
const repo = await cmdRepoShow(cfg, rest[1]);
if (!repo) {
out("(not found)");
return 1;
}
out(JSON.stringify(repo, null, 2));
return 0;
}
async function handleRepoCreateCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
const isPrivate = rest.includes("--private");
const name = rest.find((x, i) => i > 0 && !x.startsWith("--"));
if (!name) {
out("usage: gluecron repo create <name> [--private]");
return 1;
}
const r = await cmdRepoCreate(cfg, name, isPrivate);
out(JSON.stringify(r, null, 2));
return 0;
}
async function handleIssuesCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
if (rest[0] !== "ls" || !rest[1]) {
out("usage: gluecron issues ls <owner/name>");
return 1;
}
const issues = await cmdIssuesLs(cfg, rest[1]);
for (const i of issues) {
out(` #${i.number} ${i.title}`);
}
return 0;
}
async function handleGqlCmd(cfg: Config, rest: string[], out: (msg: string) => void): Promise<number> {
if (!rest[0]) {
out("usage: gluecron gql '<query>'");
return 1;
}
const r = await cmdGql(cfg, rest.join(" "));
out(JSON.stringify(r, null, 2));
return 0;
}
// ---------- Dispatcher ----------
export async function dispatch(argv: string[], out = console.log): Promise<number> {
const cfg = loadConfig();
const [cmd, ...rest] = argv;
if (!cmd || cmd === "help" || cmd === "--help" || cmd === "-h") {
out(HELP);
return 0;
}
if (cmd === "version" || cmd === "--version" || cmd === "-v") {
out(VERSION);
return 0;
}
try {
switch (cmd) {
case "host":
return await handleHostCmd(cfg, rest, out);
case "login":
return await handleLoginCmd(cfg, out);
case "whoami":
out(await cmdWhoami(cfg));
return 0;
case "repo":
return await handleRepoCmd(cfg, rest, out);
case "issues":
return await handleIssuesCmd(cfg, rest, out);
case "gql":
return await handleGqlCmd(cfg, rest, out);
default:
out(`unknown command: ${cmd}\n`);
out(HELP);
return 1;
}
} catch (err) {
out(`error: ${(err as Error).message}`);
return 1;
}
}
// Entry
if (import.meta.main) {
const code = await dispatch(process.argv.slice(2));
process.exit(code);
} |