forked from consolidation/site-process
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom transport to allow wrapping with any command. see co…
- Loading branch information
1 parent
ca41dc8
commit a9761b7
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |