Skip to content

Commit

Permalink
feat: add custom transport to allow wrapping with any command. see co…
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainweb committed Feb 2, 2022
1 parent ca41dc8 commit a9761b7
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Factory/CustomTransportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Consolidation\SiteProcess\Factory;

use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Transport\CustomTransport;
use Consolidation\Config\ConfigInterface;

/**
* CustomTransportFactory will create an CustomTransport for applicable site aliases.
*/
class CustomTransportFactory implements TransportFactoryInterface
{
/**
* @inheritdoc
*/
public function check(SiteAliasInterface $siteAlias)
{
return $siteAlias->has('command');
}

/**
* @inheritdoc
*/
public function create(SiteAliasInterface $siteAlias)
{
return new CustomTransport($siteAlias);
}
}
70 changes: 70 additions & 0 deletions src/Transport/CustomTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Consolidation\SiteProcess\Transport;

use Consolidation\SiteProcess\SiteProcess;
use Consolidation\SiteProcess\Util\Escape;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Util\Shell;
use Consolidation\Config\ConfigInterface;

/**
* CustomTransport knows how to wrap a command such that it runs within
* any cli.
*/
class CustomTransport implements TransportInterface
{
protected $tty;
protected $siteAlias;

public function __construct(SiteAliasInterface $siteAlias)
{
$this->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];
}
}
54 changes: 54 additions & 0 deletions tests/Transport/CustomTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Consolidation\SiteProcess;

use Consolidation\SiteProcess\Transport\CustomTransport;
use PHPUnit\Framework\TestCase;
use Consolidation\SiteAlias\SiteAlias;

class CustomTransportTest extends TestCase
{
/**
* Data provider for testWrap.
*/
public function wrapTestValues()
{
return [
[
'ls',
[
'custom' => [
'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));
}
}

0 comments on commit a9761b7

Please sign in to comment.