polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
158 lines (149 loc) • 6.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateOutputFormat = validateOutputFormat;
exports.registerWatchConditionCommands = registerWatchConditionCommands;
const watch_condition_handler_1 = require("../handlers/watch-condition.handler");
const manager_1 = require("../config/manager");
const auth_adapter_1 = require("../config/auth-adapter");
const errors_1 = require("../utils/errors");
function validateOutputFormat(value) {
if (!['table', 'json'].includes(value)) {
throw new Error('Output format must be either "table" or "json"');
}
return value;
}
function registerWatchConditionCommands(program) {
const watchConditionCmd = program.command('watch-condition');
watchConditionCmd.description('观看条件配置管理');
const getCmd = watchConditionCmd
.command('get')
.description('获取观看条件配置')
.option('--channel-id <id>', '频道ID (可选,不传为全局设置)')
.option('-o, --output <format>', '输出格式 (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const parentOptions = program.opts();
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions);
const watchConditionHandler = new watch_condition_handler_1.WatchConditionHandler(authConfig, serviceConfig);
await watchConditionHandler.getWatchCondition({
channelId: options.channelId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
getCmd.addHelpText('after', `
Examples:
# 获取全局观看条件
$ polyv-live-cli watch-condition get
# 获取频道观看条件
$ polyv-live-cli watch-condition get --channel-id 123456
# JSON 输出格式
$ polyv-live-cli watch-condition get --channel-id 123456 -o json
Output Formats:
table - 表格输出格式 (默认)
json - JSON 格式输出,适合程序化处理
`);
const setCmd = watchConditionCmd
.command('set')
.description('设置观看条件配置')
.option('--channel-id <id>', '频道ID (可选,不传为全局设置)')
.option('--rank <number>', '条件级别 (1=主要条件/ 2=次要条件)', parseInt)
.option('--auth-type <type>', '认证类型 (none|code|pay|phone|info|custom|external|direct)')
.option('--enabled <value>', '是否启用 (Y|N)')
.option('--auth-code <password>', '密码 (authType=code 时使用)')
.option('--price <amount>', '价格/元 (authType=pay 时使用)', parseFloat)
.option('--config-file <path>', 'JSON 配置文件路径 (复杂配置场景)')
.option('-o, --output <format>', '输出格式 (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const parentOptions = program.opts();
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(parentOptions);
const watchConditionHandler = new watch_condition_handler_1.WatchConditionHandler(authConfig, serviceConfig);
await watchConditionHandler.setWatchCondition({
channelId: options.channelId,
rank: options.rank,
authType: options.authType,
enabled: options.enabled,
authCode: options.authCode,
price: options.price,
configFile: options.configFile,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
setCmd.addHelpText('after', `
Examples:
# 简单设置:关闭观看条件(公开观看)
$ polyv-live-cli watch-condition set --channel-id 123456 --rank 1 --auth-type none --enabled Y
# 简单设置:密码观看
$ polyv-live-cli watch-condition set --channel-id 123456 --rank 1 --auth-type code --enabled Y --auth-code "abc123"
# 简单设置:付费观看
$ polyv-live-cli watch-condition set --channel-id 123456 --rank 1 --auth-type pay --enabled Y --price 99.9
# 复杂设置:使用 JSON 配置文件
$ polyv-live-cli watch-condition set --channel-id 123456 --config-file ./watch-condition.json
# 设置全局观看条件
$ polyv-live-cli watch-condition set --rank 1 --auth-type none --enabled N
JSON Config File Format:
{
"authSettings": [
{
"rank": 1,
"enabled": "Y",
"authType": "code",
"authCode": "abc123"
},
{
"rank": 2,
"enabled": "N"
}
]
}
Output Formats:
table - 表格输出格式 (默认)
json - JSON 格式输出,适合程序化处理
`);
}
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: 30000,
debug: false
}
};
}
else {
throw error;
}
}
const serviceConfig = {
baseUrl: configResult.config.baseUrl,
timeout: configResult.config.timeout,
debug: configResult.config.debug,
};
return {
authConfig: authResult.config,
serviceConfig,
};
}
//# sourceMappingURL=watch-condition.commands.js.map