Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Not working on Windows (Solution) #112

Closed
Biostate opened this issue Dec 29, 2024 · 0 comments
Closed

Not working on Windows (Solution) #112

Biostate opened this issue Dec 29, 2024 · 0 comments

Comments

@Biostate
Copy link

I encountered a "General Error" when using Windows and executing a command. After investigating, I came across PR #64 and experimented with a few tweaks to resolve the issue. Here's what worked for me:

The Issue

The << syntax doesn't work in the Windows Command Shell. However, you can use double quotes (") instead.

The Solution

I extended the SSH class in my project and made the following changes:

<?php

namespace App\Ssh;

use Spatie\Ssh\Ssh as SpatieSsh;

class Ssh extends SpatieSsh
{
    public function getExecuteCommand($command): string
    {
        $commands = $this->wrapArray($command);
        $commandString = implode(' &&'.PHP_EOL, $commands);

        if (in_array($this->host, ['local', 'localhost', '127.0.0.1'])) {
            return $commandString;
        }

        $passwordCommand = $this->getPasswordCommand();
        $extraOptions = implode(' ', $this->getExtraOptions());
        $target = $this->getTargetForSsh();
        $bash = $this->addBash ? "'bash -se'" : '';

        return "{$passwordCommand}ssh {$extraOptions} {$target} {$bash} \"".PHP_EOL
            .$commandString.PHP_EOL
            ."\"";
    }
}

Key Changes

  • Removed the EOF part and replaced it with double quotes around the commands.
  • Added && between commands to ensure they run sequentially on the server.

Example Usage

Here’s how I use the modified class:

$process = Ssh::create('forge', 'host')
    ->removeBash()
    ->execute([
        'composer install',
        'php artisan key:generate --force',
        'php artisan migrate:fresh --seed --force',
        'npm install && npm run build',
        'php artisan storage:link --force',
    ]);

Example Output

ssh forge@host  "
composer install &&
php artisan key:generate --force &&
php artisan migrate:fresh --seed --force &&
npm install && npm run build &&
php artisan storage:link --force
"

Additional Notes

To ensure compatibility, I added a flag in my project that uses this custom SSH class if the application is running on a Windows machine.

You might wonder: "Why not open a PR to fix this issue?" Unfortunately, I don’t currently have the time to contribute a proper fix with tests. Since this solution was developed for my hobby project, I didn’t prioritize making it a formal PR. Besides, I suspect there aren't many Windows users in the PHP community who would encounter this issue.

I hope this helps someone facing a similar problem. If you have suggestions or improvements, feel free to share. Thanks!

@spatie spatie locked and limited conversation to collaborators Dec 30, 2024
@freekmurze freekmurze converted this issue into discussion #113 Dec 30, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant