-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add site:shared push pull commands for shared files #91
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\Console\Site; | ||
|
|
||
| use Bigpixelrocket\DeployerPHP\Contracts\BaseCommand; | ||
| use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO; | ||
| use Bigpixelrocket\DeployerPHP\DTOs\SiteDTO; | ||
| use Bigpixelrocket\DeployerPHP\Traits\PlaybooksTrait; | ||
| use Bigpixelrocket\DeployerPHP\Traits\ServersTrait; | ||
| use Bigpixelrocket\DeployerPHP\Traits\SitesTrait; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| #[AsCommand( | ||
| name: 'site:shared:pull', | ||
| description: 'Download a file from a site\'s shared directory' | ||
| )] | ||
| class SiteSharedPullCommand extends BaseCommand | ||
| { | ||
| use PlaybooksTrait; | ||
| use ServersTrait; | ||
| use SitesTrait; | ||
|
|
||
| // ---- | ||
| // Configuration | ||
| // ---- | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| parent::configure(); | ||
|
|
||
| $this | ||
| ->addOption('domain', null, InputOption::VALUE_REQUIRED, 'Site domain') | ||
| ->addOption('remote', null, InputOption::VALUE_REQUIRED, 'Remote filename (relative to shared/)') | ||
| ->addOption('local', null, InputOption::VALUE_REQUIRED, 'Local destination file path'); | ||
| } | ||
|
|
||
| // ---- | ||
| // Execution | ||
| // ---- | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| parent::execute($input, $output); | ||
|
|
||
| $this->heading('Download Shared File'); | ||
|
|
||
| // | ||
| // Select site | ||
| // ---- | ||
|
|
||
| $site = $this->selectSite(); | ||
|
|
||
| if (is_int($site)) { | ||
| return $site; | ||
| } | ||
|
|
||
| $this->displaySiteDeets($site); | ||
|
|
||
| // | ||
| // Get server for site | ||
| // ---- | ||
|
|
||
| $server = $this->getServerForSite($site); | ||
|
|
||
| if (is_int($server)) { | ||
| return $server; | ||
| } | ||
|
|
||
| $this->displayServerDeets($server); | ||
|
|
||
| // | ||
| // Verify server connection | ||
| // ---- | ||
|
|
||
| $info = $this->serverInfo($server); | ||
|
|
||
| if (is_int($info)) { | ||
| return $info; | ||
| } | ||
|
|
||
| // | ||
| // Resolve paths | ||
| // ---- | ||
|
|
||
| $remoteRelative = $this->resolveRemotePath(); | ||
| if ($remoteRelative === null) { | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $remotePath = $this->buildSharedPath($site, $remoteRelative); | ||
|
|
||
| // | ||
| // Verify remote file exists | ||
| // ---- | ||
|
|
||
| try { | ||
| if (! $this->remoteFileExists($server, $remotePath)) { | ||
| $this->nay("Remote file not found: {$remoteRelative}"); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
| } catch (\RuntimeException $e) { | ||
| $this->nay($e->getMessage()); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $localPath = $this->resolveLocalPath($remoteRelative); | ||
| if ($localPath === null) { | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| // | ||
| // Check local file overwrite | ||
| // ---- | ||
|
|
||
| if ($this->fs->exists($localPath)) { | ||
| /** @var bool $overwrite */ | ||
| $overwrite = $this->io->promptConfirm( | ||
| label: "Local file {$localPath} exists. Overwrite?", | ||
| default: false | ||
| ); | ||
|
|
||
| if (! $overwrite) { | ||
| $this->io->warning('Download cancelled.'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } | ||
|
|
||
| // | ||
| // Download file | ||
| // ---- | ||
|
|
||
| $this->io->info("Downloading <fg=cyan>{$remotePath}</> to <fg=cyan>{$localPath}</>"); | ||
| $this->io->writeln(''); | ||
|
|
||
| try { | ||
| $this->ssh->downloadFile($server, $remotePath, $localPath); | ||
| } catch (\RuntimeException $e) { | ||
| $this->nay($e->getMessage()); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $this->yay('Shared file downloaded'); | ||
|
|
||
| // | ||
| // Show command replay | ||
| // ---- | ||
|
|
||
| $this->showCommandReplay('site:shared:pull', [ | ||
| 'domain' => $site->domain, | ||
| 'remote' => $remoteRelative, | ||
| 'local' => $localPath, | ||
| ]); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| // ---- | ||
| // Helpers | ||
| // ---- | ||
|
|
||
| private function resolveRemotePath(): ?string | ||
| { | ||
| /** @var string|null $remoteInput */ | ||
| $remoteInput = $this->io->getOptionOrPrompt( | ||
| 'remote', | ||
| fn (): string => $this->io->promptText( | ||
| label: 'Remote filename (relative to shared/):', | ||
| placeholder: '.env', | ||
| required: true | ||
| ) | ||
| ); | ||
|
|
||
| $normalized = $this->normalizeRelativePath($remoteInput ?? ''); | ||
|
|
||
| if ($normalized === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return $normalized; | ||
| } | ||
|
|
||
| private function resolveLocalPath(string $remoteRelative): ?string | ||
| { | ||
| $default = basename($remoteRelative) ?: $remoteRelative; | ||
|
|
||
| /** @var string $localInput */ | ||
| $localInput = $this->io->getOptionOrPrompt( | ||
| 'local', | ||
| fn (): string => $this->io->promptText( | ||
| label: 'Local destination path:', | ||
| default: $default, | ||
| required: true | ||
| ) | ||
| ); | ||
|
|
||
| try { | ||
| return $this->fs->expandPath($localInput); | ||
| } catch (\RuntimeException $e) { | ||
| $this->nay($e->getMessage()); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private function normalizeRelativePath(string $path): ?string | ||
| { | ||
| $cleaned = trim(str_replace('\\', '/', $path)); | ||
| $cleaned = preg_replace('#/+#', '/', $cleaned); | ||
|
|
||
| if ($cleaned === null) { | ||
| $this->nay('Remote filename is required.'); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| $cleaned = ltrim($cleaned, '/'); | ||
|
|
||
| if ($cleaned === '' || str_contains($cleaned, '..')) { | ||
| $this->nay('Remote filename must be relative to the shared/ directory and cannot contain "..".'); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return $cleaned; | ||
| } | ||
|
|
||
| private function buildSharedPath(SiteDTO $site, string $relative = ''): string | ||
| { | ||
| $sharedRoot = $this->getSiteSharedPath($site); | ||
|
|
||
| if ($relative === '') { | ||
| return $sharedRoot; | ||
| } | ||
|
|
||
| return rtrim($sharedRoot, '/').'/'.ltrim($relative, '/'); | ||
| } | ||
|
|
||
| private function remoteFileExists(ServerDTO $server, string $remotePath): bool | ||
| { | ||
| $result = $this->ssh->executeCommand( | ||
| $server, | ||
| sprintf('test -f %s', escapeshellarg($remotePath)) | ||
| ); | ||
|
|
||
| if ($result['exit_code'] === 0) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($result['exit_code'] === 1) { | ||
| return false; | ||
| } | ||
|
|
||
| $output = trim((string) $result['output']); | ||
| $message = $output === '' ? "Failed checking remote file: {$remotePath}" : $output; | ||
|
|
||
| throw new \RuntimeException($message); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.