From 3a77aa36bc21a180180e7d02dccccb2d568437db Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 14:51:11 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- src/Task/RealMeSetupTask.php | 59 +++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/Task/RealMeSetupTask.php b/src/Task/RealMeSetupTask.php index 7897da5..64a6e62 100644 --- a/src/Task/RealMeSetupTask.php +++ b/src/Task/RealMeSetupTask.php @@ -4,13 +4,15 @@ use Exception; -use SilverStripe\Control\HTTPRequest; -use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\RealMe\RealMeService; use SilverStripe\Control\Director; use SilverStripe\Control\Controller; use SilverStripe\Dev\BuildTask; +use SilverStripe\HybridExecution\HybridOutput; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; /** * Class RealMeSetupTask @@ -25,15 +27,16 @@ */ class RealMeSetupTask extends BuildTask { - private static $segment = 'RealMeSetupTask'; + protected static string $commandName = 'RealMeSetupTask'; private static $dependencies = [ 'Service' => '%$' . RealMeService::class, ]; - protected $title = "RealMe Setup Task"; + protected string $title = "RealMe Setup Task"; - protected $description = 'Validates a realme configuration & creates the resources needed to integrate with realme'; + protected static string $description = 'Validates a realme configuration & creates the resources ' + . 'needed to integrate with realme'; /** * @var RealMeService @@ -47,13 +50,14 @@ class RealMeSetupTask extends BuildTask */ private $errors = array(); + private HybridOutput $output; + /** * Run this setup task. See class phpdoc for the full description of what this does - * - * @param HTTPRequest $request */ - public function run($request) + protected function execute(InputInterface $input, HybridOutput $output): int { + $this->output = $output; try { // Ensure we are running on the command-line, and not running in a browser if (false === Director::is_cli()) { @@ -64,22 +68,37 @@ public function run($request) } // Validate all required values exist - $forEnv = $request->getVar('forEnv'); + $forEnv = $input->getOption('forEnv'); // Throws an exception if there was a problem with the config. $this->validateInputs($forEnv); $this->outputMetadataXmlContent($forEnv); - $this->message(PHP_EOL . _t( + $this->output->writeln(['', _t( RealMeSetupTask::class . '.BUILD_FINISH', 'RealMe setup complete. Please copy the XML into a file for upload to the {env} environment or DIA ' . 'to complete the integration', array('env' => $forEnv) - )); + )]); } catch (Exception $e) { - $this->message($e->getMessage() . PHP_EOL); + $this->output->writeln('' . $e->getMessage() . ''); + return Command::FAILURE; } + return Command::SUCCESS; + } + + public function getOptions(): array + { + return [ + new InputOption( + 'forEnv', + null, + InputOption::VALUE_REQUIRED, + 'The RealMe environment to set up', + suggestedValues: $this->service->getAllowedRealMeEnvironments() + ), + ]; } /** @@ -135,7 +154,7 @@ private function validateInputs($forEnv) )); } - $this->message(_t( + $this->output->writeln(_t( RealMeSetupTask::class . '.VALIDATION_SUCCESS', 'Validation succeeded, continuing with setup...' )); @@ -149,7 +168,7 @@ private function validateInputs($forEnv) private function outputMetadataXmlContent($forEnv) { // Output metadata XML so that it can be sent to RealMe via the agency - $this->message(_t( + $this->output->writeln(_t( RealMeSetupTask::class . '.OUPUT_PREFIX', 'Metadata XML is listed below for the \'{env}\' RealMe environment, this should be sent to the agency so ' . 'they can pass it on to RealMe Operations staff', @@ -181,7 +200,7 @@ private function outputMetadataXmlContent($forEnv) ) ); - $this->message($message); + $this->output->writeln($message); } /** @@ -223,16 +242,6 @@ private function getConfigurationTemplateDir() return $path . '/templates/saml-conf'; } - /** - * Output a message to the console - * @param string $message - * @return void - */ - private function message($message) - { - echo $message . PHP_EOL; - } - /** * Thin wrapper around is_readable(), used mainly so we can test this class completely *