Skip to content

Commit

Permalink
Merge pull request #54 from bambamboole/master
Browse files Browse the repository at this point in the history
Refactor to extraOptions property for more extensibility
  • Loading branch information
freekmurze committed Nov 12, 2021
2 parents 792b722 + e424bcb commit 1afb815
Showing 1 changed file with 28 additions and 54 deletions.
82 changes: 28 additions & 54 deletions src/Ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ class Ssh

protected string $host;

protected string $pathToPrivateKey = '';

protected ?int $port;

protected bool $enableStrictHostChecking = true;

protected bool $quietMode = false;

protected bool $enablePasswordAuthentication = true;
protected array $extraOptions = [];

protected Closure $processConfigurationClosure;

Expand All @@ -32,7 +24,9 @@ public function __construct(string $user, string $host, int $port = null)

$this->host = $host;

$this->port = $port;
if ($port !== null){
$this->usePort($port);
}

$this->processConfigurationClosure = fn (Process $process) => null;

Expand All @@ -46,7 +40,14 @@ public static function create(...$args): self

public function usePrivateKey(string $pathToPrivateKey): self
{
$this->pathToPrivateKey = $pathToPrivateKey;
$this->extraOptions['private_key'] = '-i ' . $pathToPrivateKey;

return $this;
}

public function useJumpHost(string $jumpHost):self
{
$this->extraOptions['jump_host'] = '-J ' . $jumpHost;

return $this;
}
Expand All @@ -56,7 +57,7 @@ public function usePort(int $port): self
if ($port < 0) {
throw new Exception('Port must be a positive integer.');
}
$this->port = $port;
$this->extraOptions['port'] = '-p ' . $port;

return $this;
}
Expand All @@ -77,42 +78,49 @@ public function onOutput(Closure $onOutput): self

public function enableStrictHostKeyChecking(): self
{
$this->enableStrictHostChecking = true;
unset($this->extraOptions['enable_strict_check']);

return $this;
}

public function disableStrictHostKeyChecking(): self
{
$this->enableStrictHostChecking = false;
$this->extraOptions['enable_strict_check'] = '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';

return $this;
}

public function enableQuietMode(): self
{
$this->quietMode = true;
$this->extraOptions['quiet'] = '-q';

return $this;
}

public function disableQuietMode(): self
{
$this->quietMode = false;
unset($this->extraOptions['quiet']);

return $this;
}

public function disablePasswordAuthentication(): self
{
$this->enablePasswordAuthentication = false;
$this->extraOptions['password_authentication'] = '-o PasswordAuthentication=no';

return $this;
}

public function enablePasswordAuthentication(): self
{
$this->enablePasswordAuthentication = true;
unset($this->extraOptions['password_authentication']);

return $this;
}

public function addExtraOption(string $option): self
{
$this->extraOptions[] = $option;

return $this;
}
Expand All @@ -126,7 +134,7 @@ public function getExecuteCommand($command): string
{
$commands = $this->wrapArray($command);

$extraOptions = $this->getExtraSshOptions();
$extraOptions = implode(' ', $this->getExtraOptions());

$commandString = implode(PHP_EOL, $commands);

Expand Down Expand Up @@ -187,52 +195,18 @@ public function upload(string $sourcePath, string $destinationPath): Process
return $this->run($uploadCommand);
}

protected function getExtraSshOptions(): string
{
$extraOptions = $this->getExtraOptions();

if (! is_null($this->port)) {
$extraOptions[] = "-p {$this->port}";
}

return implode(' ', $extraOptions);
}

protected function getExtraScpOptions(): string
{
$extraOptions = $this->getExtraOptions();

$extraOptions[] = '-r';

if (! is_null($this->port)) {
$extraOptions[] = "-P {$this->port}";
}

return implode(' ', $extraOptions);
}

private function getExtraOptions(): array
{
$extraOptions = [];

if ($this->pathToPrivateKey) {
$extraOptions[] = "-i {$this->pathToPrivateKey}";
}

if (! $this->enableStrictHostChecking) {
$extraOptions[] = '-o StrictHostKeyChecking=no';
$extraOptions[] = '-o UserKnownHostsFile=/dev/null';
}

if (! $this->enablePasswordAuthentication) {
$extraOptions[] = '-o PasswordAuthentication=no';
}

if ($this->quietMode) {
$extraOptions[] = '-q';
}

return $extraOptions;
return array_values($this->extraOptions);
}

protected function wrapArray($arrayOrString): array
Expand Down

0 comments on commit 1afb815

Please sign in to comment.