From a9761b733da1768350bb0c569ac3ee5f3e4da482 Mon Sep 17 00:00:00 2001 From: hw Date: Wed, 2 Feb 2022 16:42:35 -0500 Subject: [PATCH] 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)); + } +}