Skip to content

Commit

Permalink
feat: auto-select remote for db-pull
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Apr 8, 2023
1 parent 34a6d88 commit 0504f01
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions App/Commands/DbPull.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace RockShell;
<?php

namespace RockShell;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -14,39 +16,48 @@
* ];
*/

class DbPull extends Command {
class DbPull extends Command
{

const backupdir = "/site/assets/backups/database/";

public function config() {
public function config()
{
$this
->setDescription("Pull a database dump from remote server")
->addArgument("remote", InputArgument::OPTIONAL)
->addOption("list", "l", InputOption::VALUE_NONE, "List remotes")
->addOption("keep", "k", InputOption::VALUE_NONE, "Keep tmp.sql after restore")
->addOption("php", "p", InputOption::VALUE_OPTIONAL, "PHP command to use, eg keyhelp-php81")
;
->addOption("php", "p", InputOption::VALUE_OPTIONAL, "PHP command to use, eg keyhelp-php81");
}

public function handle() {
public function handle()
{
$wire = $this->wire();

// get remotes from config
$remotes = $this->getConfig('remotes');
if(!$remotes OR !count($remotes)) {
if (!$remotes or !count($remotes)) {
$this->error("No remotes defined in \$config->rockshell['remotes'] - see DbPull.php for help");
return self::FAILURE;
}

if($this->option("list")) {
if ($this->option("list")) {
$this->info("Available remotes:");
$this->write($remotes);
}

// if remote is not specified and we only have one remote
// we take that one to pull from
if (!$this->argument("remote") and count($remotes) === 1) {
foreach ($remotes as $remote) {
$remote = (object)$remote;
}
} else {
$remoteName = $this->argument("remote") ?: $this->choice("Choose remote", array_keys($remotes));
$remote = (object)$remotes[$remoteName];
}

// connect to remote
$remoteName = $this->argument("remote") ?: $this->choice("Choose remote", array_keys($remotes));
$remote = (object)$remotes[$remoteName];
$ssh = $remote->ssh;
$dir = rtrim($remote->dir, "/");
$folder = trim(DbDump::backupdir, "/");
Expand All @@ -67,7 +78,7 @@ public function handle() {
'--file' => 'tmp.sql',
]);

if(!$this->option('keep')) {
if (!$this->option('keep')) {
$this->write("Removing tmp.sql...");
$this->exec("rm $folder/tmp.sql", false);
$this->info("Done");
Expand All @@ -76,5 +87,4 @@ public function handle() {
$this->write("");
return self::SUCCESS;
}

}

0 comments on commit 0504f01

Please sign in to comment.