diff --git a/src/Command/Tunnel/TunnelOpenCommand.php b/src/Command/Tunnel/TunnelOpenCommand.php index efb86e4a2..bc9425a16 100644 --- a/src/Command/Tunnel/TunnelOpenCommand.php +++ b/src/Command/Tunnel/TunnelOpenCommand.php @@ -32,6 +32,7 @@ public function __construct(private readonly Api $api, private readonly Config $ protected function configure(): void { $this->addOption('gateway-ports', 'g', InputOption::VALUE_NONE, 'Allow remote hosts to connect to local forwarded ports'); + $this->addOption('json', null, InputOption::VALUE_NONE, 'Outputs tunnel information in JSON on stdout'); $this->selector->addProjectOption($this->getDefinition()); $this->selector->addEnvironmentOption($this->getDefinition()); $this->selector->addAppOption($this->getDefinition()); @@ -122,6 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $error = false; $processIds = []; + $tunnels = []; foreach ($relationships as $name => $services) { foreach ($services as $key => $service) { $service['_relationship_name'] = $name; @@ -136,6 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $relationshipString, $this->tunnelManager->getUrl($openTunnelInfo), )); + $tunnels[$relationshipString] = ['relationship' => $relationshipString, 'url' => $this->tunnelManager->getUrl($tunnel),]; continue; } @@ -175,6 +178,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $relationshipString, $this->tunnelManager->getUrl($tunnel), )); + $tunnels[$relationshipString] = ['relationship' => $relationshipString, 'url' => $this->tunnelManager->getUrl($tunnel),]; $processIds[] = $pid; } @@ -199,6 +203,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); } + if ($input->getOption('json')) { + $output->writeln(json_encode(['tunnels' => $tunnels], JSON_THROW_ON_ERROR)); + } + $processManager->killParent($error); $processManager->monitor($log); diff --git a/src/Command/Tunnel/TunnelSingleCommand.php b/src/Command/Tunnel/TunnelSingleCommand.php index 16d974bd5..bdcdcf8c1 100644 --- a/src/Command/Tunnel/TunnelSingleCommand.php +++ b/src/Command/Tunnel/TunnelSingleCommand.php @@ -29,8 +29,10 @@ public function __construct(private readonly Api $api, private readonly Question protected function configure(): void { $this - ->addOption('port', null, InputOption::VALUE_REQUIRED, 'The local port'); - $this->addOption('gateway-ports', 'g', InputOption::VALUE_NONE, 'Allow remote hosts to connect to local forwarded ports'); + ->addOption('port', null, InputOption::VALUE_REQUIRED, 'The local port') + ->addOption('gateway-ports', 'g', InputOption::VALUE_NONE, 'Allow remote hosts to connect to local forwarded ports') + ->addOption('json', null, InputOption::VALUE_NONE, 'Outputs tunnel information in JSON on stdout') + ; $this->selector->addProjectOption($this->getDefinition()); $this->selector->addEnvironmentOption($this->getDefinition()); $this->selector->addAppOption($this->getDefinition()); @@ -146,6 +148,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->stdErr->writeln(''); + if ($input->getOption('json')) { + $output->writeln(json_encode(['tunnel' => ['relationship' => $relationshipString, 'url' => $this->tunnelManager->getUrl($tunnel),]], JSON_THROW_ON_ERROR)); + } + $processManager->monitor($this->stdErr); return $process->isSuccessful() ? 0 : 1;