You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Command/CloudTrafficSettlementRecov...

66 lines
2.3 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Command;
use App\Services\Cloud\CloudTrafficSettlementRecovery;
/** CLI-only plan/apply entry for delayed traffic settlement. */
final class CloudTrafficSettlementRecoveryCommand
{
public static function run(array $argv): int
{
$command = self::parse($argv);
$result = $command['action'] === 'inspect'
? CloudTrafficSettlementRecovery::inspect()
: CloudTrafficSettlementRecovery::apply($command['plan_hash']);
fwrite(STDOUT, json_encode(
$result,
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
) . PHP_EOL);
return 0;
}
/** @return array{action:string,plan_hash?:string} */
private static function parse(array $argv): array
{
$action = (string) ($argv[2] ?? '');
if ($action === 'inspect' && count($argv) === 3) {
return ['action' => 'inspect'];
}
if ($action !== 'apply') {
throw new \InvalidArgumentException(
'用法:php xcat cloudTrafficSettlementRecovery inspect;'
. '或 apply --plan-hash=<64位哈希> --apply'
);
}
$options = [];
foreach (array_slice($argv, 3) as $argument) {
if ($argument === '--apply') {
if (isset($options['apply'])) {
throw new \InvalidArgumentException('参数重复:apply');
}
$options['apply'] = true;
continue;
}
if (preg_match('/^--([a-z-]+)=(.*)$/D', $argument, $match)
!== 1 || isset($options[$match[1]])) {
throw new \InvalidArgumentException('恢复参数无效或重复');
}
$options[$match[1]] = $match[2];
}
if (array_diff(array_keys($options), ['plan-hash', 'apply']) !== []
|| ($options['apply'] ?? false) !== true
|| preg_match('/^[a-f0-9]{64}$/D',
(string) ($options['plan-hash'] ?? '')) !== 1) {
throw new \InvalidArgumentException(
'执行恢复必须绑定 inspect 返回的 plan-hash 并显式提供 --apply'
);
}
return [
'action' => 'apply',
'plan_hash' => (string) $options['plan-hash'],
];
}
}