|
26 | 26 | #[AsCommand(name: 'ecommit:deploy-rsync', description: 'Deploy the application with RSYNC and SSH')] |
27 | 27 | final class DeployRsyncCommand extends Command |
28 | 28 | { |
| 29 | + public const TARGET_FILE_REGEX = '/^file:\/\/(?<path>.+)$/'; |
| 30 | + public const TARGET_SSH_REGEX = '/^ssh:\/\/(?<username>.+)@(?<hostname>[^:]+)(:(?<port>\d+)){0,1}:(?<path>.+)$/'; |
| 31 | + |
29 | 32 | /** |
30 | 33 | * @var array |
31 | 34 | */ |
@@ -92,10 +95,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
92 | 95 | if ($ignoreFile) { |
93 | 96 | $command[] = '--exclude-from='.$ignoreFile; |
94 | 97 | } |
95 | | - $command[] = '-e'; |
96 | | - $command[] = sprintf('ssh -p%s', $environment['port']); |
97 | | - $command[] = $this->getDirPath($this->projetDir); |
98 | | - $command[] = sprintf('%s@%s:%s', $environment['username'], $environment['hostname'], $this->getDirPath($environment['dir'])); |
| 98 | + if (preg_match(self::TARGET_FILE_REGEX, $environment['target'], $targetPats)) { |
| 99 | + $command[] = $this->getDirPath($this->projetDir); |
| 100 | + $command[] = $this->getDirPath($targetPats['path']); |
| 101 | + } elseif (preg_match(self::TARGET_SSH_REGEX, $environment['target'], $targetPats)) { |
| 102 | + $command[] = '-e'; |
| 103 | + $command[] = sprintf('ssh -p%s', (isset($targetPats['port']) && '' !== $targetPats['port']) ? $targetPats['port'] : 22); |
| 104 | + $command[] = $this->getDirPath($this->projetDir); |
| 105 | + $command[] = sprintf('%s@%s:%s', $targetPats['username'], $targetPats['hostname'], $this->getDirPath($targetPats['path'])); |
| 106 | + } else { |
| 107 | + throw new \Exception('Invalid target'); |
| 108 | + } |
99 | 109 |
|
100 | 110 | foreach ($this->executeProcess($command) as $data) { |
101 | 111 | $output->writeln($data); |
|
0 commit comments