Skip to content

Commit

Permalink
Fix KubectlTransport::addChdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
ceesgeene committed Dec 5, 2022
1 parent 49ec2b9 commit ce574f9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/Transport/KubectlTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/**
Expand All @@ -15,6 +16,9 @@ class KubectlTransport implements TransportInterface
/** @var bool */
protected $tty;

/** @var string */
protected $cd_remote;

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

Expand Down Expand Up @@ -64,6 +68,21 @@ public function wrap($args)
$transport = is_array($entrypoint) ? [...$transport, ...$entrypoint] : [...$transport, $entrypoint];
}

if ($this->cd_remote) {
// Wrap the command in a subshell, to be able to prepend a `cd`.
$args = [
'sh',
'-c',
// Escape each argument for the target system and then join.
implode(' ', Escape::argsForSite($this->siteAlias, [
'cd',
$this->cd_remote,
Shell::op('&&'),
...$args
]))
];
}

return array_merge($transport, $args);
}

Expand All @@ -72,13 +91,13 @@ public function wrap($args)
*/
public function addChdir($cd_remote, $args)
{
return array_merge(
[
'cd',
$cd_remote,
Shell::op('&&'),
],
$args
);
// If the site alias specifies a root, and it matches the requested
// directory, there is no need to wrap the command in a subshell.
if ($cd_remote === $this->siteAlias->get('root') && $this->siteAlias->get('kubectl.cd_root') === false) {
return $args;
}

$this->cd_remote = $cd_remote;
return $args;
}
}
34 changes: 34 additions & 0 deletions tests/Transport/KubectlTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ public function wrapTestValues()
]
],
],

// With root.
[
"kubectl --namespace=vv exec --tty=false --stdin=false deploy/drupal -- sh -c 'cd /path/to/drupal && ls'",
['ls'],
[
'root' => '/path/to/drupal',
'kubectl' => [
'tty' => false,
'interactive' => false,
'namespace' => 'vv',
'resource' => 'deploy/drupal',
]
],
],

// With root and cd_root set to false.
[
'kubectl --namespace=vv exec --tty=false --stdin=false deploy/drupal -- ls',
['ls'],
[
'root' => '/path/to/drupal',
'kubectl' => [
'tty' => false,
'interactive' => false,
'namespace' => 'vv',
'resource' => 'deploy/drupal',
'cd_root' => false,
]
],
],
];
}

Expand All @@ -110,6 +141,9 @@ public function testWrap($expected, $args, $siteAliasData)
{
$siteAlias = new SiteAlias($siteAliasData, '@alias.dev');
$dockerTransport = new KubectlTransport($siteAlias);
if (isset($siteAliasData['root'])) {
$dockerTransport->addChdir($siteAliasData['root']);
}
$actual = $dockerTransport->wrap($args);
$this->assertEquals($expected, implode(' ', $actual));
}
Expand Down

0 comments on commit ce574f9

Please sign in to comment.