Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom transport to allow wrapping with any command #61

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems too generic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greg-1-anderson, I agree. I hesitated to put it in but on thinking more, it started making sense as the functionality is really that generic. That said, I am open to alternatives.

}

/**
* @inheritdoc
*/
public function create(SiteAliasInterface $siteAlias)
{
return new CustomTransport($siteAlias);
}
}
2 changes: 2 additions & 0 deletions src/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
71 changes: 71 additions & 0 deletions src/Transport/CustomTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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)
{
$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];
}
}
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');
$customTransport = new CustomTransport($siteAlias);
$actual = $customTransport->wrap(['ls']);
$this->assertEquals($expected, implode(' ', $actual));
}
}