polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
100 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAuthAndServiceConfig = loadAuthAndServiceConfig;
exports.validateOutputFormat = validateOutputFormat;
exports.registerPromotionCommands = registerPromotionCommands;
const promotion_handler_1 = require("../handlers/promotion.handler");
const auth_adapter_1 = require("../config/auth-adapter");
const manager_1 = require("../config/manager");
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('Using authentication from:', authResult.source);
}
return { authConfig: authResult.config, serviceConfig, isVerbose };
}
function validateOutputFormat(format) {
if (format !== 'table' && format !== 'json') {
throw new Error('Invalid output format. Must be table or json');
}
return format;
}
function registerPromotionCommands(program) {
const promotionCmd = program
.command('promotion')
.description('Manage marketing promotion channels (管理营销推广渠道)');
promotionCmd
.command('list')
.description('List all promotion channels (列出所有推广渠道)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new promotion_handler_1.PromotionHandler(authConfig, serviceConfig);
await handler.listPromotions({
channelId: options.channelId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
promotionCmd
.command('create')
.description('Batch create promotion channels (批量创建推广渠道)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--names <names>', 'Comma-separated promotion channel names (推广渠道名称, 用逗分隔)')
.option('-f, --force', 'skip confirmation prompt')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new promotion_handler_1.PromotionHandler(authConfig, serviceConfig);
await handler.createPromotions({
channelId: options.channelId,
names: options.names.split(',').map((n) => n.trim()).filter((n) => n.trim().length > 0),
force: options.force,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
}
//# sourceMappingURL=promotion.commands.js.map