| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | namespace Clansuite\Capture\CLI; |
| 14: | |
| 15: | use function error_log; |
| 16: | use Clansuite\Capture\CaptureService; |
| 17: | use Exception; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class CaptureCommand |
| 23: | { |
| 24: | public function __construct(private readonly CaptureService $captureService) |
| 25: | { |
| 26: | } |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | public function run(array $argv): int |
| 36: | { |
| 37: | |
| 38: | if ($this->hasHelpFlag($argv)) { |
| 39: | $help = new HelpCommand; |
| 40: | $help->run($argv); |
| 41: | |
| 42: | return 0; |
| 43: | } |
| 44: | |
| 45: | $args = $this->parseArgs($argv); |
| 46: | $ip = $args[0]; |
| 47: | $port = $args[1]; |
| 48: | $protocol = $args[2]; |
| 49: | $options = $args[3]; |
| 50: | |
| 51: | if ($ip === '' || $port <= 0) { |
| 52: | $help = new HelpCommand; |
| 53: | $help->run(['', "❌ Missing required arguments: ip and port are required.\n"]); |
| 54: | |
| 55: | return 1; |
| 56: | } |
| 57: | |
| 58: | try { |
| 59: | $savedPath = $this->captureService->capture($ip, $port, $protocol, $options); |
| 60: | print $savedPath . "\n"; |
| 61: | |
| 62: | return 0; |
| 63: | } catch (Exception $e) { |
| 64: | error_log('❌ ' . $e->getMessage()); |
| 65: | |
| 66: | return 1; |
| 67: | } |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | private function hasHelpFlag(array $argv): bool |
| 74: | { |
| 75: | foreach ($argv as $arg) { |
| 76: | if ($arg === '-h' || $arg === '--help' || $arg === 'help') { |
| 77: | return true; |
| 78: | } |
| 79: | } |
| 80: | |
| 81: | return false; |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | private function parseArgs(array $argv): array |
| 90: | { |
| 91: | $ip = $argv[1] ?? ''; |
| 92: | $port = (int) ($argv[2] ?? 0); |
| 93: | $protocol = $argv[3] ?? 'auto'; |
| 94: | $options = []; |
| 95: | |
| 96: | |
| 97: | return [$ip, $port, $protocol, $options]; |
| 98: | } |
| 99: | } |
| 100: | |