multidomain-tool/multidomain
#!/usr/local/bin/php
<?php
/**
* For anyone how seeks quality code here.
* This is a quick tool to generate a set of env variables.
* It's full of non-pure functions, global variables and other anti patters.
* But it's good enough. Have a good day
*/
require 'vendor/autoload.php';
use Ahc\Cli\Application;
use Ahc\Cli\Output\Color;
use Ahc\Cli\IO\Interactor;
use EscolaLms\MultidomainTool\Database;
use EscolaLms\MultidomainTool\MinIO;
use EscolaLms\MultidomainTool\Variables;
use EscolaLms\MultidomainTool\Keys;
if (php_sapi_name() !== 'cli') {
exit;
}
$app = new Application('Wellms multi-domain helper', '0.0.1');
$color = new Color;
echo $color->info('Wellms Headless LMS multi-domain helper' . "\n");
$app
->command('add', 'Add domains. Use comma separated values', 'a') // alias a
->arguments('[domains]')
->action(function ($domains) use ($app, $color) {
$interactor = new Interactor;
if (empty($domains)) {
$domains = $interactor->prompt('Comma separated Domain Names.', '');
}
$final_vars = [];
$final_domains = [];
if (Variables::checkEnvVars($app)) {
foreach (explode(',', $domains) as $domain) {
$domain = trim($domain);
$final_domains[] = $domain;
$domain_key = str_replace(['.', '-'], '_', strtoupper($domain));
echo $color->info('Generating environment and variables for ' . "$domain domain\n");
$minio_vars = MinIO::createUserPolicyBucket($domain);
$postgres_vars = Database::createPostgresUserAndDatabase($domain_key);
$rsa_keys = Keys::generateRSAKeysPair();
if ($minio_vars['success'] && $postgres_vars['success']) {
$final_vars = [
...$final_vars,
...Variables::listDomainVariables($domain_key, $minio_vars['vars']),
...Variables::listDomainVariables($domain_key, $postgres_vars['vars']),
...Variables::listDomainVariables($domain_key, ['JWT_PUBLIC_KEY_BASE64' => base64_encode($rsa_keys['publickey'])]),
...Variables::listDomainVariables($domain_key, ['JWT_PRIVATE_KEY_BASE64' => base64_encode($rsa_keys['privatekey'])]),
...Variables::listDomainVariables($domain_key, ['INITIAL_USER_PASSWORD' => Variables::getRandomSecurePassword()])
];
}
};
$final_vars['MULTI_DOMAINS'] = 'MULTI_DOMAINS=' . (isset($_ENV['MULTI_DOMAINS']) ? $_ENV['MULTI_DOMAINS'] . "," . implode(",", $final_domains) : implode(",", $final_domains));
Variables::printVariables($final_vars);
file_put_contents("build/{$domains}.env", implode("\n", $final_vars));
echo $color->info("If you forgot to copy those above, don't worry they are saved to " . realpath("build/{$domains}.env") . "\n");
}
})
// Done setting up this command for now, tap() to retreat back so we can add another command
->tap()
->command('remove', 'remove domain', 'r') // alias co
->arguments('[domain]')
->action(function ($domain) {
$interactor = new Interactor;
if (empty($domain)) {
$domain = $interactor->prompt('Domain Name', '');
}
$color = new Color;
echo $color->info('Implement logic for removing domain $domain');
});
$app->handle($argv);