/** * ───────────────────────────────────────────── * SPB Git — Personal Git Platform * ───────────────────────────────────────────── * Author : Simon-Pierre Boucher * Contact : contact@spboucher.ai * File : cli/commands/init.mjs * Purpose : `spbgit init` — interactive setup wizard * License : MIT © Simon-Pierre Boucher * ───────────────────────────────────────────── */ import { mkdirSync } from 'node:fs'; import pc from 'picocolors'; import { loadCliConfig, saveCliConfig, expandHome, CONFIG_PATH } from '../lib/config.mjs'; import { prompt } from '../lib/ui.mjs'; import { git } from '../lib/git.mjs'; import { api } from '../lib/api.mjs'; export function registerInit(program) { program .command('init') .description('interactive setup: server URL, token, workspace directory') .action(async () => { const existing = loadCliConfig() ?? {}; console.log(pc.bold('SPB Git CLI setup\n')); const server = await prompt('Server URL', existing.server ?? 'https://git.spboucher.ai'); const token = await prompt('Personal access token (paste, hidden after save)', existing.token ? '(keep current)' : ''); const workspace = expandHome(await prompt('Workspace directory', existing.workspace ?? '~/code')); const config = { server: server.replace(/\/+$/, ''), token: token === '(keep current)' ? existing.token : token, workspace, author: existing.author ?? { name: 'Simon-Pierre Boucher', email: 'contact@spboucher.ai' }, }; mkdirSync(workspace, { recursive: true }); saveCliConfig(config); console.log(`\n${pc.green('✓')} Config written to ${CONFIG_PATH} (mode 600)`); // Ensure global git identity exists. const name = await git(process.cwd(), ['config', '--global', 'user.name']); if (!name.ok || !name.stdout.trim()) { await git(process.cwd(), ['config', '--global', 'user.name', config.author.name]); console.log(`${pc.green('✓')} git user.name set to ${config.author.name}`); } const email = await git(process.cwd(), ['config', '--global', 'user.email']); if (!email.ok || !email.stdout.trim()) { await git(process.cwd(), ['config', '--global', 'user.email', config.author.email]); console.log(`${pc.green('✓')} git user.email set to ${config.author.email}`); } if (config.token) { try { const who = await api(config, 'GET', '/api/v1/whoami'); console.log(`${pc.green('✓')} Token valid — authenticated as ${who.owner} (${who.tokenLabel})`); } catch (err) { console.log(`${pc.yellow('!')} Could not verify token: ${err.message}`); } } else { console.log(`${pc.yellow('!')} No token saved — read-only operations only. Get one with: spbgit token new`); } }); }