Skip to content

Commit

Permalink
refactor command names + add key commands
Browse files Browse the repository at this point in the history
Signed-off-by: Dieter Coopman <dieter@deltasolutions.be>
  • Loading branch information
dietercoopman committed Mar 28, 2023
1 parent 939d76b commit 2d37c3d
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 26 deletions.
40 changes: 22 additions & 18 deletions sajan
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ use Dietercoopman\SajanPhp\DnsFlushCommand;
use Dietercoopman\SajanPhp\DnsUsingCommand;
use Dietercoopman\SajanPhp\IpLanCommand;
use Dietercoopman\SajanPhp\IpPublicCommand;
use Dietercoopman\SajanPhp\KeyMakeCommand;
use Dietercoopman\SajanPhp\KeyCopyCommand;
use Dietercoopman\SajanPhp\LaravelInstallCommand;
use Dietercoopman\SajanPhp\MysqlCompare;
use Dietercoopman\SajanPhp\ServerDelete;
use Dietercoopman\SajanPhp\ServerCreate;
use Dietercoopman\SajanPhp\ServerInfo;
use Dietercoopman\SajanPhp\ServerLaravelSitesCommand;
use Dietercoopman\SajanPhp\LaravelSitesCommand;
use Dietercoopman\SajanPhp\MysqlCompareCommand;
use Dietercoopman\SajanPhp\ServerDeleteCommand;
use Dietercoopman\SajanPhp\ServerCreateCommand;
use Dietercoopman\SajanPhp\ServerInfoCommand;
use Dietercoopman\SajanPhp\MiscSecretCommand;
use Dietercoopman\SajanPhp\SajanApplication;
use Dietercoopman\SajanPhp\ServerList;
use Dietercoopman\SajanPhp\ServerListCommand;
use Dietercoopman\SajanPhp\StarCommand;
use Dietercoopman\SajanPhp\TipsCommand;
use Dietercoopman\SajanPhp\ToolsCheck;
use Dietercoopman\SajanPhp\ToolsInstall;
use Dietercoopman\SajanPhp\ToolsUpdate;
use Dietercoopman\SajanPhp\ToolsCheckCommand;
use Dietercoopman\SajanPhp\ToolsInstallCommand;
use Dietercoopman\SajanPhp\ToolsUpdateCommand;
use Dietercoopman\SajanPhp\UpdateCommand;
use Dietercoopman\SajanPhp\WebpackInitCommand;
use Symfony\Component\Process\Process;
Expand All @@ -41,19 +43,21 @@ $app->add(new DnsFlushCommand());
$app->add(new DnsUsingCommand());
$app->add(new IpLanCommand());
$app->add(new IpPublicCommand());
$app->add(new KeyMakeCommand());
$app->add(new KeyCopyCommand());
$app->add(new LaravelInstallCommand());
$app->add(new ServerCreate());
$app->add(new ServerInfo());
$app->add(new ServerList());
$app->add(new ServerDelete());
$app->add(new ServerLaravelSitesCommand());
$app->add(new ServerCreateCommand());
$app->add(new ServerInfoCommand());
$app->add(new ServerListCommand());
$app->add(new ServerDeleteCommand());
$app->add(new LaravelSitesCommand());
$app->add(new MiscSecretCommand());
$app->add(new MysqlCompare());
$app->add(new MysqlCompareCommand());
$app->add(new StarCommand());
$app->add(new TipsCommand());
$app->add(new ToolsInstall());
$app->add(new ToolsCheck());
$app->add(new ToolsUpdate());
$app->add(new ToolsInstallCommand());
$app->add(new ToolsCheckCommand());
$app->add(new ToolsUpdateCommand());
$app->add(new UpdateCommand());
$app->add(new WebpackInitCommand());
$app->run();
56 changes: 56 additions & 0 deletions src/KeyCopyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Dietercoopman\SajanPhp;

use Dietercoopman\SajanPhp\Services\Configurator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use function Termwind\render;

class KeyCopyCommand extends BaseCommand
{
/**
* Configure the command.
*
* @return void
*/
protected function configure()
{
$this
->setName('key:copy')
->setDescription('Copy the value of a public key to your clipboard')
->setAliases(['kc']);
}

/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$helper = $this->getHelper('question');
$configurator = (new Configurator());

$this->title();
$keyfile = '~/.ssh/' . $configurator->askFor($helper, $input, $output, $this->getPossibleSshKeys(), 'Please select the ssh key you want to use');
Process::fromShellCommandline('cat '.$keyfile.'.pub | pbcopy')->mustRun()->getOutput();
render('<span class="success mt-1 mb-1">The public key '.$keyfile.' has been copied to your clipboard.</span>');

return 0;
}

private function getPossibleSshKeys(): array
{
$possibleKeys = Process::fromShellCommandline('ls ~/.ssh')->mustRun()->getOutput();
$possibleKeys = collect(explode("\n", $possibleKeys))->filter(function ($key) {
return strstr($key, '.pub');
})->transform(function ($key) {
return str_replace('.pub', '', $key);
})->toArray();
return array_values($possibleKeys);
}
}
55 changes: 55 additions & 0 deletions src/KeyMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Dietercoopman\SajanPhp;

use Dietercoopman\SajanPhp\Services\Configurator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use function Termwind\ask;
use function Termwind\render;

class KeyMakeCommand extends BaseCommand
{
/**
* Configure the command.
*
* @return void
*/
protected function configure()
{
$this
->setName('key:make')
->setDescription('Create a new ssh key')
->setAliases(['km']);
}

/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$passwordsEqual = false;
$this->title();

render('<div class="ml-1">Generating public/private rsa key pair.</div>');
$file = ask('<div class="ml-1 mr-1">Enter file in which to save the key (~/.ssh/id_rsa):</div>');
while (!$passwordsEqual) {
$passphrase = ask('<div class="ml-1 mr-1">Enter passphrase (empty for no passphrase):</div>', ishidden: true);
$passphrasecheck = ask('<div class="ml-1 mr-1">Enter same passphrase again:</div>', ishidden: true);
$passwordsEqual = $passphrasecheck === $passphrase;
}

$command = 'ssh-keygen -t rsa -f '.$file.' -q -P "'.$passphrase.'"';
exec($command);

render('<div class="ml-1">Your identification has been saved in '.$file .'</div>');
render('<div class="ml-1">Your public key has been saved in '.$file.'.pub</div>');

return 0;
}
}
62 changes: 62 additions & 0 deletions src/LaravelSitesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php namespace Dietercoopman\SajanPhp;

use Dietercoopman\SajanPhp\Services\Configurator;
use Dietercoopman\SajanPhp\Services\Laravel;
use Dietercoopman\SajanPhp\Services\Server;
use Dietercoopman\SajanPhp\Traits\HasServer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use function Termwind\{render};


class LaravelSitesCommand extends BaseCommand
{

use HasServer;

/**
* Configure the command.
*
* @return void
*/
protected function configure()
{
$this
->setName('laravel:sites')
->setDescription('Get Laravel applications with their version on a server')
->setAliases(['ls']);
}

/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->title();

$configurator = (new Configurator());
$choices = $this->getServers($configurator);

if (count($choices) > 0) {
$helper = $this->getHelper('question');
$servername = $configurator->askFor($helper, $input, $output, $choices, 'Please select the server you want to run this command for');
} else {
render('<div class="m-1">You have no saved servers, you can create one with the \'server:create\' command.</div>');
return 0;
}

$config = $configurator->validateServer($servername);
$server = (new Laravel())->init($config);
$server->getLaravelApplications($servername);

return 0;
}

}

2 changes: 1 addition & 1 deletion src/MysqlCompare.php → src/MysqlCompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Question\Question;
use function Termwind\render;

class MysqlCompare extends BaseCommand
class MysqlCompareCommand extends BaseCommand
{
use HasServer;

Expand Down
2 changes: 1 addition & 1 deletion src/ServerCreate.php → src/ServerCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use function Termwind\{ask, render};


class ServerCreate extends BaseCommand
class ServerCreateCommand extends BaseCommand
{
/**
* Configure the command.
Expand Down
2 changes: 1 addition & 1 deletion src/ServerDelete.php → src/ServerDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function Termwind\{render};


class ServerDelete extends BaseCommand
class ServerDeleteCommand extends BaseCommand
{

use HasServer;
Expand Down
2 changes: 1 addition & 1 deletion src/ServerInfo.php → src/ServerInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function Termwind\{render};


class ServerInfo extends BaseCommand
class ServerInfoCommand extends BaseCommand
{

use HasServer;
Expand Down
2 changes: 1 addition & 1 deletion src/ServerList.php → src/ServerListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function Termwind\{render};


class ServerList extends BaseCommand
class ServerListCommand extends BaseCommand
{
/**
* Configure the command.
Expand Down
2 changes: 1 addition & 1 deletion src/ToolsCheck.php → src/ToolsCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Process\Process;
use function Termwind\render;

class ToolsCheck extends BaseCommand
class ToolsCheckCommand extends BaseCommand
{
/**
* Configure the command.
Expand Down
2 changes: 1 addition & 1 deletion src/ToolsInstall.php → src/ToolsInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;

class ToolsInstall extends BaseCommand
class ToolsInstallCommand extends BaseCommand
{
/**
* Configure the command.
Expand Down
2 changes: 1 addition & 1 deletion src/ToolsUpdate.php → src/ToolsUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;

class ToolsUpdate extends BaseCommand
class ToolsUpdateCommand extends BaseCommand
{
/**
* Configure the command.
Expand Down

0 comments on commit 2d37c3d

Please sign in to comment.