/** * `celilo subscribers add --secret [--name ] [--registry ] [--tag ] [--package-pattern

]` * * Append (or replace, on URL collision) a subscriber in the static * subscriber-config file. The secret is required — there's no way * to add an unsigned subscriber. Match fields are all optional; * omitting them means "match every event." */ import type { Subscriber } from '@celilo/event-bus/build-bus'; import { addSubscriber, subscriberStorePath } from '../../services/build-bus'; import type { CommandResult } from '../types'; export function handleSubscribersAdd( args: string[], flags: Record, ): CommandResult { const url = args[0]; if (!url) { return { success: false, error: 'Subscriber URL required.\n\nUsage:\n celilo subscribers add --secret [options]', }; } const secret = flags.secret; if (typeof secret !== 'string' || !secret) { return { success: false, error: '--secret is required.', }; } if (!/^https?:\/\//.test(url)) { return { success: false, error: `Subscriber URL must start with http:// or https:// (got "${url}").`, }; } const subscriber: Subscriber = { url, secret, match: {}, }; if (typeof flags.name === 'string') subscriber.name = flags.name; if (typeof flags.registry === 'string') subscriber.match.registry = flags.registry; if (typeof flags.tag === 'string') subscriber.match.tag = flags.tag; if (typeof flags['package-pattern'] === 'string') { subscriber.match.packagePattern = flags['package-pattern']; } let result: ReturnType; try { result = addSubscriber(subscriber); } catch (err) { return { success: false, error: `Could not save subscriber: ${err instanceof Error ? err.message : String(err)}`, }; } const verb = result.replaced ? 'Updated' : 'Added'; return { success: true, message: `${verb} subscriber ${subscriber.name ?? subscriber.url}.\n\nStore: ${subscriberStorePath()}`, }; }