UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

173 lines (172 loc) 6.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadAuthAndServiceConfig = loadAuthAndServiceConfig; exports.validateOutputFormat = validateOutputFormat; exports.registerPlatformLabelCommands = registerPlatformLabelCommands; const platform_label_handler_1 = require("../handlers/platform-label.handler"); const manager_1 = require("../config/manager"); const auth_adapter_1 = require("../config/auth-adapter"); const errors_1 = require("../utils/errors"); const DEFAULT_TIMEOUT_MS = 30000; async function loadAuthAndServiceConfig(parentOptions) { const authResult = auth_adapter_1.authAdapter.tryGetAuthConfig(parentOptions); if (!authResult) { throw new Error(auth_adapter_1.authAdapter.getStatusMessage(parentOptions)); } let configResult; try { configResult = await manager_1.configManager.load({ cliOptions: parentOptions, }); } catch (error) { if (error instanceof Error && error.message.includes('Auth configuration is incomplete')) { configResult = { config: { baseUrl: 'https://api.polyv.net', timeout: DEFAULT_TIMEOUT_MS, debug: false, }, }; } else { throw error; } } const serviceConfig = { baseUrl: configResult.config.baseUrl, timeout: configResult.config.timeout, debug: configResult.config.debug, }; const isVerbose = !!parentOptions['verbose']; if (isVerbose) { console.log(`🔐 Authentication Source: ${authResult.source}`); if (authResult.accountName) { console.log(`👤 Account: ${authResult.accountName}`); } console.log(''); } return { authConfig: authResult.config, serviceConfig, isVerbose, }; } function validateOutputFormat(value) { if (!['table', 'json'].includes(value)) { throw new Error('Invalid output format. Must be "table" or "json"'); } return value; } function registerPlatformLabelCommands(program) { const platformCmd = program.commands.find((cmd) => cmd.name() === 'platform'); if (!platformCmd) { throw new Error('Platform command not found. Platform label commands must be registered after platform commands.'); } const labelCmd = platformCmd.command('label'); labelCmd.description('Label management (标签管理)'); const listCmd = labelCmd .command('list') .description('List viewer labels (获取标签列表)') .option('-o, --output <format>', 'output format (table|json)', validateOutputFormat, 'table') .action(async (options) => { try { const parentOptions = program.opts(); const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions); const handler = new platform_label_handler_1.PlatformLabelHandler(authConfig, serviceConfig); await handler.listLabels({ output: options.output, }); } catch (error) { (0, errors_1.logError)(error instanceof Error ? error : new Error(String(error))); process.exit(1); } }); listCmd.addHelpText('after', ` Examples: $ polyv-live-cli platform label list $ polyv-live-cli platform label list -o json Output Formats: table - Formatted table output (default) json - JSON format for programmatic use `); const createCmd = labelCmd .command('create') .description('Create a viewer label (创建标签)') .requiredOption('--name <labelName>', 'label name (标签名称)') .option('-o, --output <format>', 'output format (table|json)', validateOutputFormat, 'table') .action(async (options) => { try { const parentOptions = program.opts(); const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions); const handler = new platform_label_handler_1.PlatformLabelHandler(authConfig, serviceConfig); await handler.createLabel({ labelName: options.name, output: options.output, }); } catch (error) { (0, errors_1.logError)(error instanceof Error ? error : new Error(String(error))); process.exit(1); } }); createCmd.addHelpText('after', ` Examples: $ polyv-live-cli platform label create --name "VIP用户" $ polyv-live-cli platform label create --name "Staff" -o json `); const updateCmd = labelCmd .command('update') .description('Update a viewer label (更新标签)') .requiredOption('--id <labelId>', 'label ID (标签ID)', (value) => parseInt(value, 10)) .requiredOption('--name <labelName>', 'label name (标签名称)') .option('-o, --output <format>', 'output format (table|json)', validateOutputFormat, 'table') .action(async (options) => { try { const parentOptions = program.opts(); const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions); const handler = new platform_label_handler_1.PlatformLabelHandler(authConfig, serviceConfig); await handler.updateLabel({ labelId: options.id, labelName: options.name, output: options.output, }); } catch (error) { (0, errors_1.logError)(error instanceof Error ? error : new Error(String(error))); process.exit(1); } }); updateCmd.addHelpText('after', ` Examples: $ polyv-live-cli platform label update --id 1 --name "VIP会员" $ polyv-live-cli platform label update --id 2 --name "New Name" -o json `); const deleteCmd = labelCmd .command('delete') .description('Delete a viewer label (删除标签)') .requiredOption('--id <labelId>', 'label ID (标签ID)', (value) => parseInt(value, 10)) .option('-o, --output <format>', 'output format (table|json)', validateOutputFormat, 'table') .action(async (options) => { try { const parentOptions = program.opts(); const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions); const handler = new platform_label_handler_1.PlatformLabelHandler(authConfig, serviceConfig); await handler.deleteLabel({ labelId: options.id, output: options.output, }); } catch (error) { (0, errors_1.logError)(error instanceof Error ? error : new Error(String(error))); process.exit(1); } }); deleteCmd.addHelpText('after', ` Examples: $ polyv-live-cli platform label delete --id 1 $ polyv-live-cli platform label delete --id 2 -o json `); } //# sourceMappingURL=platform-label.commands.js.map