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

Fix KubectlTransport::addChdir() #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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