Skip to content

Commit

Permalink
refactor: refactor classes handling command-line tools
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 24, 2024
1 parent e4ce288 commit 088fca2
Show file tree
Hide file tree
Showing 20 changed files with 431 additions and 247 deletions.
14 changes: 11 additions & 3 deletions src/Build/BuildContainerImageStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Exception\RuntimeException;
use Ymir\Cli\Executable\DockerExecutable;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;
use Ymir\Cli\Tool\Docker;

class BuildContainerImageStep implements BuildStepInterface
{
Expand All @@ -28,6 +28,13 @@ class BuildContainerImageStep implements BuildStepInterface
*/
private $buildDirectory;

/**
* The Docker executable.
*
* @var DockerExecutable
*/
private $dockerExecutable;

/**
* The file system.
*
Expand All @@ -38,9 +45,10 @@ class BuildContainerImageStep implements BuildStepInterface
/**
* Constructor.
*/
public function __construct(string $buildDirectory, Filesystem $filesystem)
public function __construct(string $buildDirectory, DockerExecutable $dockerExecutable, Filesystem $filesystem)
{
$this->buildDirectory = rtrim($buildDirectory, '/');
$this->dockerExecutable = $dockerExecutable;
$this->filesystem = $filesystem;
}

Expand Down Expand Up @@ -75,6 +83,6 @@ public function perform(string $environment, ProjectConfiguration $projectConfig
throw new RuntimeException('Unable to find a "Dockerfile" to build the container image');
}

Docker::build($dockerfileName, sprintf('%s:%s', $projectConfiguration->getProjectName(), $environment), $this->buildDirectory);
$this->dockerExecutable->build($dockerfileName, sprintf('%s:%s', $projectConfiguration->getProjectName(), $environment), $this->buildDirectory);
}
}
24 changes: 22 additions & 2 deletions src/Command/Cache/CacheTunnelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Ymir\Cli\ApiClient;
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Command\Network\AddBastionHostCommand;
use Ymir\Cli\Executable\SshExecutable;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;
use Ymir\Cli\Tool\Ssh;

class CacheTunnelCommand extends AbstractCacheCommand
{
Expand All @@ -29,6 +32,23 @@ class CacheTunnelCommand extends AbstractCacheCommand
*/
public const NAME = 'cache:tunnel';

/**
* The SSH executable.
*
* @var SshExecutable
*/
protected $sshExecutable;

/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, SshExecutable $sshExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->sshExecutable = $sshExecutable;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -66,7 +86,7 @@ protected function perform()

$this->output->info(sprintf('Creating SSH tunnel to the "<comment>%s</comment>" cache cluster. You can connect using: <comment>localhost:%s</comment>', $cache['name'], $localPort));

$tunnel = Ssh::tunnelBastionHost($network->get('bastion_host'), $localPort, $cache['endpoint'], 6379);
$tunnel = $this->sshExecutable->openTunnelToBastionHost($network->get('bastion_host'), $localPort, $cache['endpoint'], 6379);

$this->output->info('Once finished, press "<comment>Ctrl+C</comment>" to close the tunnel');

Expand Down
28 changes: 24 additions & 4 deletions src/Command/Database/AbstractDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,35 @@

use Carbon\Carbon;
use Symfony\Component\Console\Exception\RuntimeException;
use Ymir\Cli\ApiClient;
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Command\AbstractCommand;
use Ymir\Cli\Command\Network\AddBastionHostCommand;
use Ymir\Cli\Exception\InvalidInputException;
use Ymir\Cli\Executable\SshExecutable;
use Ymir\Cli\Process\Process;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;
use Ymir\Cli\Tool\Ssh;

abstract class AbstractDatabaseCommand extends AbstractCommand
{
/**
* The SSH executable.
*
* @var SshExecutable
*/
protected $sshExecutable;

/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, SshExecutable $sshExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->sshExecutable = $sshExecutable;
}

/**
* Determine the database server that the command is interacting with.
*/
Expand Down Expand Up @@ -81,9 +101,9 @@ protected function determineUser(): string
}

/**
* Start a SSH tunnel to a private database server.
* Open a SSH tunnel to a private database server.
*/
protected function startSshTunnel(array $databaseServer): Process
protected function openSshTunnel(array $databaseServer): Process
{
$this->output->info(sprintf('Opening SSH tunnel to "<comment>%s</comment>" private database server', $databaseServer['name']));

Expand All @@ -94,7 +114,7 @@ protected function startSshTunnel(array $databaseServer): Process
}

$bastionHost = $network->get('bastion_host');
$tunnel = Ssh::tunnelBastionHost($bastionHost, 3305, $databaseServer['endpoint'], 3306);
$tunnel = $this->sshExecutable->openTunnelToBastionHost($bastionHost, 3305, $databaseServer['endpoint'], 3306);

$timeout = Carbon::now()->addSeconds(10);

Expand Down
24 changes: 22 additions & 2 deletions src/Command/Database/DatabaseServerTunnelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Ymir\Cli\ApiClient;
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Command\Network\AddBastionHostCommand;
use Ymir\Cli\Executable\SshExecutable;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;
use Ymir\Cli\Tool\Ssh;

class DatabaseServerTunnelCommand extends AbstractDatabaseServerCommand
{
Expand All @@ -29,6 +32,23 @@ class DatabaseServerTunnelCommand extends AbstractDatabaseServerCommand
*/
public const NAME = 'database:server:tunnel';

/**
* The SSH executable.
*
* @var SshExecutable
*/
protected $sshExecutable;

/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, SshExecutable $sshExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->sshExecutable = $sshExecutable;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -68,7 +88,7 @@ protected function perform()

$this->output->info(sprintf('Opening SSH tunnel to the "<comment>%s</comment>" database server. You can connect using: <comment>localhost:%s</comment>', $databaseServer['name'], $localPort));

$tunnel = Ssh::tunnelBastionHost($network->get('bastion_host'), $localPort, $databaseServer['endpoint'], 3306);
$tunnel = $this->sshExecutable->openTunnelToBastionHost($network->get('bastion_host'), $localPort, $databaseServer['endpoint'], 3306);

$this->output->info('Once finished, press "<comment>Ctrl+C</comment>" to close the tunnel');

Expand Down
7 changes: 4 additions & 3 deletions src/Command/Database/ExportDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Ymir\Cli\Database\Connection;
use Ymir\Cli\Database\Mysqldump;
use Ymir\Cli\Exception\InvalidInputException;
use Ymir\Cli\Executable\SshExecutable;
use Ymir\Cli\Process\Process;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;

Expand All @@ -45,9 +46,9 @@ class ExportDatabaseCommand extends AbstractDatabaseCommand
/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration)
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration, SshExecutable $sshExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration, $sshExecutable);

$this->filesystem = $filesystem;
}
Expand Down Expand Up @@ -88,7 +89,7 @@ protected function perform()
$tunnel = null;

if (!$connection->needsSshTunnel()) {
$tunnel = $this->startSshTunnel($connection->getDatabaseServer());
$tunnel = $this->openSshTunnel($connection->getDatabaseServer());
}

$this->output->infoWithDelayWarning(sprintf('Exporting "<comment>%s</comment>" database', $connection->getDatabase()));
Expand Down
7 changes: 4 additions & 3 deletions src/Command/Database/ImportDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Ymir\Cli\Database\Connection;
use Ymir\Cli\Database\PDO;
use Ymir\Cli\Exception\InvalidInputException;
use Ymir\Cli\Executable\SshExecutable;
use Ymir\Cli\Process\Process;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;

Expand All @@ -45,9 +46,9 @@ class ImportDatabaseCommand extends AbstractDatabaseCommand
/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration)
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration, SshExecutable $sshExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration, $sshExecutable);

$this->filesystem = $filesystem;
}
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function perform()
$tunnel = null;

if ($connection->needsSshTunnel()) {
$tunnel = $this->startSshTunnel($connection->getDatabaseServer());
$tunnel = $this->openSshTunnel($connection->getDatabaseServer());
}

$this->output->infoWithDelayWarning(sprintf('Importing "<comment>%s</comment>" to the "<comment>%s</comment>" database', $filename, $connection->getDatabase()));
Expand Down
24 changes: 22 additions & 2 deletions src/Command/Docker/DeleteDockerImagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputOption;
use Ymir\Cli\ApiClient;
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Command\AbstractCommand;
use Ymir\Cli\Tool\Docker;
use Ymir\Cli\Executable\DockerExecutable;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;

class DeleteDockerImagesCommand extends AbstractCommand
{
Expand All @@ -34,6 +37,23 @@ class DeleteDockerImagesCommand extends AbstractCommand
*/
private const ALL_PATTERN = 'dkr.ecr';

/**
* The Docker executable.
*
* @var DockerExecutable
*/
private $dockerExecutable;

/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, DockerExecutable $dockerExecutable, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->dockerExecutable = $dockerExecutable;
}

/**
* {@inheritdoc}
*/
Expand All @@ -60,7 +80,7 @@ protected function perform()
return;
}

Docker::rmigrep($pattern);
$this->dockerExecutable->removeImagesMatchingPattern($pattern);

$this->output->info('Deployment docker images deleted successfully');
}
Expand Down
18 changes: 13 additions & 5 deletions src/Command/Project/ConfigureProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Command\AbstractProjectCommand;
use Ymir\Cli\Exception\InvalidInputException;
use Ymir\Cli\Executable\WpCliExecutable;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\ProjectConfiguration\WordPress\WordPressConfigurationChangeInterface;
use Ymir\Cli\Support\Arr;
use Ymir\Cli\Tool\WpCli;

class ConfigureProjectCommand extends AbstractProjectCommand
{
Expand All @@ -48,14 +48,22 @@ class ConfigureProjectCommand extends AbstractProjectCommand
*/
private $configurationChanges;

/**
* The WP-CLI executable.
*
* @var WpCliExecutable
*/
private $wpCliExecutable;

/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, iterable $configurationChanges = [])
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, WpCliExecutable $wpCliExecutable, iterable $configurationChanges = [])
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->configurationChanges = new Collection();
$this->wpCliExecutable = $wpCliExecutable;

foreach ($configurationChanges as $configurationChange) {
$this->addConfigurationChange($configurationChange);
Expand All @@ -79,9 +87,9 @@ protected function configure()
*/
protected function perform()
{
if (!WpCli::isInstalledGlobally()) {
if ($this->wpCliExecutable->isInstalled()) {
throw new RuntimeException('WP-CLI needs to be available globally to scan your project');
} elseif (!WpCli::isWordPressInstalled()) {
} elseif (!$this->wpCliExecutable->isWordPressInstalled()) {
throw new RuntimeException('WordPress needs to be installed and connected to a database to scan your project');
}

Expand All @@ -93,7 +101,7 @@ protected function perform()

$this->output->info('Scanning your project');

$plugins = WpCli::listPlugins()->groupBy('status');
$plugins = $this->wpCliExecutable->listPlugins()->groupBy('status');

$activePlugins = $plugins->only(['active', 'must-use'])->flatten(1);
$inactivePlugins = $plugins->only(['inactive'])->flatten(1);
Expand Down
Loading

0 comments on commit 088fca2

Please sign in to comment.