From b2c669cfab2438cdf42a858de0d335c5964ab080 Mon Sep 17 00:00:00 2001 From: Marc Reichel Date: Thu, 17 Feb 2022 22:54:02 +0100 Subject: [PATCH] Introduce config importer --- README.md | 8 ++++ src/Command/ConfigurationCommand.php | 42 ++++++++++++++++++- .../CreateGithubIssueFromMantisIssue.php | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6c6cfbb..88c4f2d 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,14 @@ mantis2github configure The command will direct you through the installation process. +### Quick setup + +If you have used a previous version of this package and already have a `config.yaml` file, you can skip the configuration by running: + +```shell +mantis2github configure path/to/config.yaml +``` + ## Usage ```shell diff --git a/src/Command/ConfigurationCommand.php b/src/Command/ConfigurationCommand.php index 2664008..5c20f53 100644 --- a/src/Command/ConfigurationCommand.php +++ b/src/Command/ConfigurationCommand.php @@ -4,6 +4,8 @@ use Artemeon\M2G\Config\ConfigReader; use Artemeon\M2G\Service\GithubConnector; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Yaml\Yaml; use function Termwind\{render, terminal}; @@ -25,12 +27,15 @@ public function __construct(GithubConnector $mantisConnector) protected function configure() { - $this->setName('configure'); - $this->setDescription('Configure the tool'); + $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 { + $this->readExistingConfigFromPath(); + terminal()->clear(); $this->header(); @@ -159,4 +164,37 @@ protected function saveConfig(): void $this->success(" Synchronize your first issue by running `mantis2github sync`!\n"); } + + protected function readExistingConfigFromPath() + { + if (!$this->argument('file')) { + return; + } + + if (!file_exists($this->argument('file'))) { + $this->error('The given config file does not exist.'); + + exit(1); + } + + $config = Yaml::parseFile($this->argument('file')); + + if (!$config['MANTIS_URL'] || !$config['MANTIS_TOKEN'] || !$config['GITHUB_TOKEN'] || !$config['GITHUB_REPOSITORY']) { + $this->error('The given config file is incomplete.'); + $this->info('Please configure the tool without the file parameter.'); + + exit(1); + } + + $this->config = [ + 'mantisUrl' => $config['MANTIS_URL'], + 'mantisToken' => $config['MANTIS_TOKEN'], + 'githubToken' => $config['GITHUB_TOKEN'], + 'githubRepository' => $config['GITHUB_REPOSITORY'], + ]; + + $this->saveConfig(); + + exit(0); + } } diff --git a/src/Command/CreateGithubIssueFromMantisIssue.php b/src/Command/CreateGithubIssueFromMantisIssue.php index f3e796f..4f6b599 100644 --- a/src/Command/CreateGithubIssueFromMantisIssue.php +++ b/src/Command/CreateGithubIssueFromMantisIssue.php @@ -38,7 +38,7 @@ public function __construct(MantisConnector $mantisConnector, GithubConnector $g protected function configure() { $this->setName('sync') - ->setDescription('Synchronize a Mantis issue to GitHub') + ->setDescription('Synchronize a list of Mantis issues to GitHub') ->addArgument('ids', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Mantis issue ids'); }