diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f36458..072401a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### Unreleased + +* Adds a new option to site aliases which lets users specify any command to wrap invoked processes (#61). + ### 5.0.0 - 2022/Feb/18 * Support symfony/process ^6 diff --git a/src/Factory/CustomTransportFactory.php b/src/Factory/CustomTransportFactory.php new file mode 100644 index 0000000..3528986 --- /dev/null +++ b/src/Factory/CustomTransportFactory.php @@ -0,0 +1,29 @@ +has('command'); + } + + /** + * @inheritdoc + */ + public function create(SiteAliasInterface $siteAlias) + { + return new CustomTransport($siteAlias); + } +} diff --git a/src/ProcessManager.php b/src/ProcessManager.php index ce9c5ba..d9b5b95 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -6,6 +6,7 @@ use Consolidation\SiteProcess\Factory\VagrantTransportFactory; use Psr\Log\LoggerInterface; use Consolidation\SiteAlias\SiteAliasInterface; +use Consolidation\SiteProcess\Factory\CustomTransportFactory; use Consolidation\SiteProcess\Factory\SshTransportFactory; use Consolidation\SiteProcess\Factory\DockerComposeTransportFactory; use Consolidation\SiteProcess\Factory\TransportFactoryInterface; @@ -72,6 +73,7 @@ public static function addTransports(ProcessManager $processManager) $processManager->add(new KubectlTransportFactory()); $processManager->add(new DockerComposeTransportFactory()); $processManager->add(new VagrantTransportFactory()); + $processManager->add(new CustomTransportFactory()); return $processManager; } diff --git a/src/Transport/CustomTransport.php b/src/Transport/CustomTransport.php new file mode 100644 index 0000000..5f897b5 --- /dev/null +++ b/src/Transport/CustomTransport.php @@ -0,0 +1,71 @@ +siteAlias = $siteAlias; + } + + /** + * @inheritdoc + */ + public function configure(SiteProcess $process) + { + $this->tty = $process->isTty(); + } + + /** + * inheritdoc + */ + public function wrap($args) + { + $cmd = $this->siteAlias->get('custom.command', ''); + $transport = $cmd ? [Shell::preEscaped($cmd)] : []; + $commandToExecute = $this->getCommandToExecute($args); + + return array_filter(array_merge( + $transport, + $commandToExecute + )); + } + + /** + * @inheritdoc + */ + public function addChdir($cd_remote, $args) + { + // Make no assumptions about the CLI and what it can support. + // The CLI itself should handle this with the options specified + // in the custom command. + return []; + } + + /** + * getCommandToExecute processes the arguments for the command to + * be executed such that they are appropriate for the transport mechanism. + */ + protected function getCommandToExecute($args) + { + // Escape each argument for the target system and then join + $args = Escape::argsForSite($this->siteAlias, $args); + $commandToExecute = implode(' ', $args); + + return [$commandToExecute]; + } +} diff --git a/tests/Transport/CustomTransportTest.php b/tests/Transport/CustomTransportTest.php new file mode 100644 index 0000000..1dc477a --- /dev/null +++ b/tests/Transport/CustomTransportTest.php @@ -0,0 +1,54 @@ + [ + 'command' => '', + ], + ], + ], + [ + 'platform ls', + [ + 'custom' => [ + 'command' => 'platform', + ], + ], + ], + [ + 'platform -e dev ls', + [ + 'custom' => [ + 'command' => 'platform -e dev', + ], + ], + ], + ]; + } + + /** + * @dataProvider wrapTestValues + */ + public function testWrap($expected, $siteAliasData) + { + $siteAlias = new SiteAlias($siteAliasData, '@alias.dev'); + $customTransport = new CustomTransport($siteAlias); + $actual = $customTransport->wrap(['ls']); + $this->assertEquals($expected, implode(' ', $actual)); + } +}