From a9761b733da1768350bb0c569ac3ee5f3e4da482 Mon Sep 17 00:00:00 2001 From: hw Date: Wed, 2 Feb 2022 16:42:35 -0500 Subject: [PATCH 1/4] feat: add custom transport to allow wrapping with any command. see #52 --- src/Factory/CustomTransportFactory.php | 29 ++++++++++ src/Transport/CustomTransport.php | 70 +++++++++++++++++++++++++ tests/Transport/CustomTransportTest.php | 54 +++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 src/Factory/CustomTransportFactory.php create mode 100644 src/Transport/CustomTransport.php create mode 100644 tests/Transport/CustomTransportTest.php 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/Transport/CustomTransport.php b/src/Transport/CustomTransport.php new file mode 100644 index 0000000..7d74b5f --- /dev/null +++ b/src/Transport/CustomTransport.php @@ -0,0 +1,70 @@ +siteAlias = $siteAlias; + } + + /** + * @inheritdoc + */ + public function configure(SiteProcess $process) + { + $this->tty = $process->isTty(); + } + + /** + * inheritdoc + */ + public function wrap($args) + { + $transport = Shell::preEscaped($this->siteAlias->get('custom.command', '')); + $commandToExecute = $this->getCommandToExecute($args); + + return 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..72351fb --- /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'); + $dockerTransport = new CustomTransport($siteAlias); + $actual = $dockerTransport->wrap(['ls']); + $this->assertEquals($expected, implode(' ', $actual)); + } +} From c88c6fbeaba3b7e83c9e34c72841186a6d3012c5 Mon Sep 17 00:00:00 2001 From: hw Date: Wed, 2 Feb 2022 16:45:17 -0500 Subject: [PATCH 2/4] chore: update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9018813..847aa3c 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). + ### 4.1.3 / 4.1.2 - 2022/Jan/18 * Support symfony/process ^5 via illicit access to a private member (#58) From e9d0f026f691b7d22bcd945d4c25bb2df5e34121 Mon Sep 17 00:00:00 2001 From: hw Date: Wed, 2 Feb 2022 16:48:34 -0500 Subject: [PATCH 3/4] fix: add the transport factory in the process manager --- src/ProcessManager.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ProcessManager.php b/src/ProcessManager.php index 7e94557..0532927 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -5,6 +5,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; @@ -70,6 +71,7 @@ public static function addTransports(ProcessManager $processManager) $processManager->add(new SshTransportFactory()); $processManager->add(new DockerComposeTransportFactory()); $processManager->add(new VagrantTransportFactory()); + $processManager->add(new CustomTransportFactory()); return $processManager; } From cc07d0f1e4363547fbeddf694ec98d6da3f994a4 Mon Sep 17 00:00:00 2001 From: hw Date: Thu, 3 Feb 2022 18:38:38 -0500 Subject: [PATCH 4/4] fix: Shell object is not an array --- src/Transport/CustomTransport.php | 7 ++++--- tests/Transport/CustomTransportTest.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Transport/CustomTransport.php b/src/Transport/CustomTransport.php index 7d74b5f..5f897b5 100644 --- a/src/Transport/CustomTransport.php +++ b/src/Transport/CustomTransport.php @@ -35,13 +35,14 @@ public function configure(SiteProcess $process) */ public function wrap($args) { - $transport = Shell::preEscaped($this->siteAlias->get('custom.command', '')); + $cmd = $this->siteAlias->get('custom.command', ''); + $transport = $cmd ? [Shell::preEscaped($cmd)] : []; $commandToExecute = $this->getCommandToExecute($args); - return array_merge( + return array_filter(array_merge( $transport, $commandToExecute - ); + )); } /** diff --git a/tests/Transport/CustomTransportTest.php b/tests/Transport/CustomTransportTest.php index 72351fb..1dc477a 100644 --- a/tests/Transport/CustomTransportTest.php +++ b/tests/Transport/CustomTransportTest.php @@ -47,8 +47,8 @@ public function wrapTestValues() public function testWrap($expected, $siteAliasData) { $siteAlias = new SiteAlias($siteAliasData, '@alias.dev'); - $dockerTransport = new CustomTransport($siteAlias); - $actual = $dockerTransport->wrap(['ls']); + $customTransport = new CustomTransport($siteAlias); + $actual = $customTransport->wrap(['ls']); $this->assertEquals($expected, implode(' ', $actual)); } }