Skip to content

Commit

Permalink
Transport support for Skpr (#66)
Browse files Browse the repository at this point in the history
* Skpr plugin

Signed-off-by: Karl Hepworth <[email protected]>

* Quick fixes

Signed-off-by: Karl Hepworth <[email protected]>

Signed-off-by: Karl Hepworth <[email protected]>
  • Loading branch information
fubarhouse authored Dec 1, 2022
1 parent 399bd23 commit 49ec2b9
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Factory/SkprTransportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Consolidation\SiteProcess\Factory;

use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Transport\SkprTransport;

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

/**
* @inheritdoc
*/
public function create(SiteAliasInterface $siteAlias)
{
return new SkprTransport($siteAlias);
}
}
2 changes: 2 additions & 0 deletions src/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Consolidation\SiteProcess;

use Consolidation\SiteProcess\Factory\KubectlTransportFactory;
use Consolidation\SiteProcess\Factory\SkprTransportFactory;
use Consolidation\SiteProcess\Factory\VagrantTransportFactory;
use Psr\Log\LoggerInterface;
use Consolidation\SiteAlias\SiteAliasInterface;
Expand Down Expand Up @@ -70,6 +71,7 @@ public static function addTransports(ProcessManager $processManager)
{
$processManager->add(new SshTransportFactory());
$processManager->add(new KubectlTransportFactory());
$processManager->add(new SkprTransportFactory());
$processManager->add(new DockerComposeTransportFactory());
$processManager->add(new VagrantTransportFactory());

Expand Down
66 changes: 66 additions & 0 deletions src/Transport/SkprTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Consolidation\SiteProcess\Transport;

use Consolidation\SiteProcess\SiteProcess;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Util\Shell;

/**
* SkprTransport knows how to wrap a command to run on a site hosted
* on the Skpr platform.
*/
class SkprTransport implements TransportInterface
{

/** @var \Consolidation\SiteAlias\SiteAliasInterface */
protected $siteAlias;

public function __construct(SiteAliasInterface $siteAlias)
{
$this->siteAlias = $siteAlias;
}

/**
* @inheritdoc
*/
public function configure(SiteProcess $process)
{
$path = $this->siteAlias->getDefault('skpr.path', getcwd());
if ($path) {
$process->chdirToSiteRoot($path);
}
}

/**
* inheritdoc
*/
public function wrap($args)
{
$environment = $this->siteAlias->get('skpr.env');

$transport = [
'skpr',
'exec',
"$environment",
];
$transport[] = "--";

return array_merge($transport, $args);
}

/**
* @inheritdoc
*/
public function addChdir($cd_remote, $args)
{
return array_merge(
[
'cd',
$cd_remote,
Shell::op('&&'),
],
$args
);
}
}
51 changes: 51 additions & 0 deletions tests/Transport/SkprTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Consolidation\SiteProcess;

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

class SkprTransportTest extends TestCase
{
/**
* Data provider for testWrap.
*/
public function wrapTestValues()
{
return [
// Everything explicit.
[
'skpr exec dev -- ls',
['ls'],
[
'skpr' => [
'env' => 'dev',
]
],
],

// Ensure we aren't escaping arguments after "--"
[
'skpr exec dev -- monday "tuesday" \'wednesday\'',
['monday', '"tuesday"', "'wednesday'"],
[
'skpr' => [
'env' => 'dev'
]
],
],
];
}

/**
* @dataProvider wrapTestValues
*/
public function testWrap($expected, $args, $siteAliasData)
{
$siteAlias = new SiteAlias($siteAliasData, '@alias.dev');
$dockerTransport = new SkprTransport($siteAlias);
$actual = $dockerTransport->wrap($args);
$this->assertEquals($expected, implode(' ', $actual));
}
}

0 comments on commit 49ec2b9

Please sign in to comment.