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 fb40018
Show file tree
Hide file tree
Showing 2 changed files with 58 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;
}
}
31 changes: 31 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 Down

0 comments on commit fb40018

Please sign in to comment.