Skip to content

Commit

Permalink
chore(): Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Jul 8, 2023
1 parent e0952dc commit 2694eba
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 289 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
"nunomaduro/termwind": "^1.5",
"composer-runtime-api": "^2.2.2",
"ahinkle/packagist-latest-version": "^2.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
}
}
21 changes: 14 additions & 7 deletions src/Command/CheckUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?php

declare(strict_types=1);

namespace Artemeon\M2G\Command;

use Artemeon\M2G\Helper\VersionHelper;

use Exception;
use JsonException;

use function Termwind\{render};

class CheckUpdateCommand extends Command
{
protected function configure()
{
$this->setName('check_update')
->setDescription('Checks whether a new version is available');
}
protected string $signature = 'check_update';
protected ?string $description = 'Checks whether a new version is available';
protected bool $hidden = true;

protected function handle(): int
/**
* @throws JsonException
* @throws Exception
*/
public function __invoke(): int
{
$currentVersion = VersionHelper::fetchVersion();
$latestVersion = VersionHelper::latestVersion();
Expand All @@ -38,6 +45,6 @@ protected function handle(): int
HTML);
}

return 0;
return self::SUCCESS;
}
}
116 changes: 14 additions & 102 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
@@ -1,127 +1,39 @@
<?php

declare(strict_types=1);

namespace Artemeon\M2G\Command;

use Artemeon\M2G\Config\ConfigReader;
use Exception;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

use function Termwind\render;

abstract class Command extends \Symfony\Component\Console\Command\Command
class Command extends \Artemeon\Console\Command
{
protected InputInterface $input;
protected OutputInterface $output;

/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
final protected function header(): void
{
$this->input = $input;
$this->output = $output;

return $this->handle();
}

protected function arguments(): array
{
return $this->input->getArguments();
}

protected function argument(string $name)
{
return $this->input->getArgument($name);
}

protected function options(): array
{
return $this->input->getOptions();
}

protected function option(string $name)
{
return $this->input->getOption($name);
}

protected function header(): void
{
$this->success(
$this->output->write(
'
__ __ _ _ <comment>____</comment> ____ _ _ _ _ _
| \/ | __ _ _ __ | |_ (_) ___ <comment>|___ \ </comment> / ___|(_)| |_ | | | | _ _ | |__
| |\/| | / _` || \'_ \ | __|| |/ __| <comment>__) |</comment> | | _ | || __|| |_| || | | || \'_ \
__ __ _ _ <comment>____</comment> ____ _ _ _ _ _
| \/ | __ _ _ __ | |_ (_) ___ <comment>|___ \ </comment> / ___|(_)| |_ | | | | _ _ | |__
| |\/| | / _` || \'_ \ | __|| |/ __| <comment>__) |</comment> | | _ | || __|| |_| || | | || \'_ \
| | | || (_| || | | || |_ | |\__ \ <comment>/ __/</comment> | |_| || || |_ | _ || |_| || |_) |
|_| |_| \__,_||_| |_| \__||_||___/ <comment>|_____|</comment> \____||_| \__||_| |_| \__,_||_.__/
'
);
}

protected function ask(string $question, string $default = null, bool $hidden = false)
{
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$trailingSpace = str_ends_with($question, ' ') ? '' : ' ';
$question = new Question($question . $trailingSpace, $default);
$question->setHidden($hidden);

return $helper->ask($this->input, $this->output, $question);
}
|_| |_| \__,_||_| |_| \__||_||___/ <comment>|_____|</comment> \____||_| \__||_| |_| \__,_||_.__/
protected function secret(string $question, string $default = null)
{
return $this->ask($question, $default, true);
}

protected function info(string $message)
{
$this->output->writeln($message);
}
protected function error(string $message)
{
render(
<<<HTML
<div class="my-1 ml-1 px-1 bg-red-400 text-white">
$message
</div>
HTML
);
}

protected function success(string $message)
{
$this->output->writeln("<info>$message</info>");
}

protected function warn(string $message)
{
render(
<<<HTML
<div class="ml-1 px-1 bg-yellow-500 text-gray-900">
<strong>! $message !</strong>
</div>
HTML
'
);
}

protected function checkConfig(): void
final protected function checkConfig(): void
{
$config = (new ConfigReader())->read();

if (!$config) {
$this->info('');
$this->newLine();
$this->warn('You have not configured mantis2github yet');
$this->warn('Please run "mantis2github configure" to get started');
$this->info('');
$this->newLine();

exit(1);
exit(self::INVALID);
}
}

abstract protected function handle(): int;
}
57 changes: 24 additions & 33 deletions src/Command/ConfigurationCommand.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
<?php

declare(strict_types=1);

namespace Artemeon\M2G\Command;

use Artemeon\M2G\Config\ConfigReader;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Yaml\Yaml;

use function Termwind\{render, terminal};

class ConfigurationCommand extends Command
{
protected string $signature = 'configure {file? : The config.yaml to use for setting up the tool.}';
protected ?string $description = 'Configure the tool';

protected string $configPath = __DIR__ . '/../../../config.yaml';
protected array $config = [];

protected function configure()
{
$this->setName('configure')
->setDescription('Configure the tool')
->addArgument('file', InputArgument::OPTIONAL, 'The config.yaml to use for setting up the tool.');
}

protected function handle(): int
public function __invoke(): int
{
$this->readExistingConfigFromPath();

Expand All @@ -34,9 +31,10 @@ protected function handle(): int
$this->warn('Configuration file already exists');
$this->warn('If you continue your configuration will be overwritten');

if ($this->ask("\n Are you sure you want to continue? [Y/n]", 'n') !== 'Y') {
if (!$this->confirm("\n Are you sure you want to continue?")) {
$this->info("\n Alright!\n");
return 1;

return self::INVALID;
}
}

Expand All @@ -50,14 +48,12 @@ protected function handle(): int
$this->askForGitHubRepository();
$this->saveConfig();

return 0;
return self::SUCCESS;
}

protected function askForMantisUrl(): void
private function askForMantisUrl(): void
{
$this->info(" Please enter the URL of your Mantis installation (e.g. https://tickets.company.tld):");

$mantisUrl = $this->ask(" >");
$mantisUrl = $this->ask('Please enter the URL of your Mantis installation (e.g. https://tickets.company.tld):');

$parsedUrl = parse_url($mantisUrl);

Expand All @@ -72,7 +68,7 @@ protected function askForMantisUrl(): void
$mantisUrl = "{$parsedUrl['scheme']}://{$parsedUrl['host']}$port/";

// Check if something is available on the given URL
// If not, we assume that the URL is wrong
// If not, we assume that the URL is wrong.
$headers = @get_headers($mantisUrl);
if (!$headers || $headers[0] === 'HTTP/1.1 404 Not Found') {
$this->error(
Expand All @@ -85,12 +81,10 @@ protected function askForMantisUrl(): void
$this->config['mantisUrl'] = $mantisUrl;
}

protected function askForMantisToken(): void
private function askForMantisToken(): void
{
$this->info("\n Head over to {$this->config['mantisUrl']}api_tokens_page.php, create a new API token,");
$this->info(" and enter the token here:");

$token = $this->secret(" >");
$this->info("Head over to {$this->config['mantisUrl']}api_tokens_page.php and create a new API token.");
$token = $this->secret('Mantis API Token');

if (empty($token)) {
$this->error('The token is empty. Please try again.');
Expand All @@ -101,12 +95,11 @@ protected function askForMantisToken(): void
$this->config['mantisToken'] = $token;
}

protected function askForGitHubToken(): void
private function askForGitHubToken(): void
{
$this->info("\n Head over to https://github.com/settings/tokens, create a new personal access token");
$this->info(" with the `repo` scope and enter the token here:");
$this->info("Head over to https://github.com/settings/tokens, create a new personal access token with the `repo` scope.");

$token = $this->secret(" >");
$token = $this->secret("GitHub Token");

if (empty($token)) {
$this->error('The token is empty. Please try again.');
Expand All @@ -117,11 +110,9 @@ protected function askForGitHubToken(): void
$this->config['githubToken'] = $token;
}

protected function askForGitHubRepository(): void
private function askForGitHubRepository(): void
{
$this->info("\n Enter the GitHub repository you want to create issues for (e.g. user/repository):");

$repository = $this->ask(" >");
$repository = $this->ask('Enter the GitHub repository you want to create issues for (e.g. user/repository)');

if (empty($repository) || count(explode('/', $repository)) !== 2) {
$this->error("The given repository is invalid.");
Expand All @@ -132,7 +123,7 @@ protected function askForGitHubRepository(): void
$this->config['githubRepository'] = $repository;
}

protected function saveConfig(): void
private function saveConfig(): void
{
$stub = file_get_contents(__DIR__ . '/../../stubs/config.yaml.stub');

Expand All @@ -153,7 +144,7 @@ protected function saveConfig(): void
$this->success(" Synchronize your first issue by running `mantis2github sync`!\n");
}

protected function readExistingConfigFromPath()
private function readExistingConfigFromPath(): void
{
if (!$this->argument('file')) {
return;
Expand Down Expand Up @@ -183,6 +174,6 @@ protected function readExistingConfigFromPath()

$this->saveConfig();

exit(0);
exit(self::SUCCESS);
}
}
Loading

0 comments on commit 2694eba

Please sign in to comment.