Skip to content

Commit

Permalink
Merge pull request #73 from easybell-gmbh/ipv6
Browse files Browse the repository at this point in the history
add ipv6 support for command execute and file upload
  • Loading branch information
freekmurze authored Jan 17, 2023
2 parents 4bae2a3 + 5ad08bd commit d3576be
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
}
}
}
15 changes: 11 additions & 4 deletions src/Ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getExecuteCommand($command): string

$delimiter = 'EOF-SPATIE-SSH';

$target = $this->getTarget();
$target = $this->getTargetForSsh();

if (in_array($this->host, ['local', 'localhost', '127.0.0.1'])) {
return $commandString;
Expand Down Expand Up @@ -184,7 +184,7 @@ public function executeAsync($command): Process

public function getDownloadCommand(string $sourcePath, string $destinationPath): string
{
return "scp {$this->getExtraScpOptions()} {$this->getTarget()}:$sourcePath $destinationPath";
return "scp {$this->getExtraScpOptions()} {$this->getTargetForScp()}:$sourcePath $destinationPath";
}

public function download(string $sourcePath, string $destinationPath): Process
Expand All @@ -196,7 +196,7 @@ public function download(string $sourcePath, string $destinationPath): Process

public function getUploadCommand(string $sourcePath, string $destinationPath): string
{
return "scp {$this->getExtraScpOptions()} $sourcePath {$this->getTarget()}:$destinationPath";
return "scp {$this->getExtraScpOptions()} $sourcePath {$this->getTargetForScp()}:$destinationPath";
}

public function upload(string $sourcePath, string $destinationPath): Process
Expand Down Expand Up @@ -242,7 +242,14 @@ protected function run(string $command, string $method = 'run'): Process
return $process;
}

protected function getTarget(): string
protected function getTargetForScp(): string
{
$host = filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? '[' . $this->host . ']' : $this->host;

return "{$this->user}@{$host}";
}

protected function getTargetForSsh(): string
{
return "{$this->user}@{$this->host}";
}
Expand Down
28 changes: 28 additions & 0 deletions tests/SshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,31 @@

assertMatchesSnapshot($command);
});

it('can handle ipv4 addresses on execute', function () {
$local = new Ssh('user', '127.0.0.2');
$command = $local->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can handle ipv4 addresses on upload', function () {
$local = new Ssh('user', '127.0.0.2');
$command = $local->getUploadCommand('.env', 'spatie.be/current/.env');

assertMatchesSnapshot($command);
});

it('can handle ipv6 addresses on execute', function () {
$local = new Ssh('user', '::1');
$command = $local->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can handle ipv6 addresses on upload', function () {
$local = new Ssh('user', '::1');
$command = $local->getUploadCommand('.env', 'spatie.be/current/.env');

assertMatchesSnapshot($command);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ssh user@127.0.0.2 'bash -se' << \EOF-SPATIE-SSH
whoami
EOF-SPATIE-SSH
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scp -r .env user@127.0.0.2:spatie.be/current/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ssh user@::1 'bash -se' << \EOF-SPATIE-SSH
whoami
EOF-SPATIE-SSH
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scp -r .env user@[::1]:spatie.be/current/.env

0 comments on commit d3576be

Please sign in to comment.