diff --git a/bin/osu-acquia-cli.php b/bin/osu-acquia-cli.php index c896f3f..5f48be8 100755 --- a/bin/osu-acquia-cli.php +++ b/bin/osu-acquia-cli.php @@ -2,11 +2,14 @@ // If we're running from phar load the phar autoload file. use OsuWams\Cli\AcquiaCli; +use OsuWams\CliSetup; use Robo\Robo; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Filesystem\Path; +const EX_CONFIG = 78; + $pharPath = Phar::running(TRUE); if ($pharPath) { $root = $pharPath; @@ -59,6 +62,11 @@ $config->set('acquia.key', $environment['acquia']['key']); $config->set('acquia.secret', $environment['acquia']['secret']); } +if (is_null($config->get('acquia.key')) || is_null($config->get('acquia.secret'))) { + $setupHelper = new CliSetup($input, $output); + $statusCode = $setupHelper->cliSetupHelper(); + exit($statusCode); +} $app = new AcquiaCli($config, $input, $output); $statusCode = $app->run($input, $output); exit($statusCode); diff --git a/src/Cli/AcquiaCli.php b/src/Cli/AcquiaCli.php index 6322d83..c731194 100644 --- a/src/Cli/AcquiaCli.php +++ b/src/Cli/AcquiaCli.php @@ -59,7 +59,14 @@ public function __construct( $this->runner->setSelfUpdateRepository(self::REPOSITORY); } - + /** + * Executes the runner's run method with the provided input and output interfaces. + * + * @param InputInterface $input The input interface to be processed by the runner. + * @param OutputInterface $output The output interface where the runner's results will be directed. + * + * @return mixed The result of the runner's run method. + */ public function run(InputInterface $input, OutputInterface $output) { return $this->runner->run($input, $output); } diff --git a/src/CliSetup.php b/src/CliSetup.php new file mode 100644 index 0000000..61de37b --- /dev/null +++ b/src/CliSetup.php @@ -0,0 +1,139 @@ +input = $input; + $this->output = $output; + } + + /** + * Helper method for setting up CLI authentication with Acquia Cloud. + * + * Prompts the user to confirm whether they want to set up authentication. + * If confirmed, it asks for the Acquia Cloud API Key and Secret, then + * attempts to save the credentials. Provides feedback on the success or + * failure of saving the credentials. + * + * @return int + */ + public function cliSetupHelper(): int { + $startConfirm = $this->confirm("Not yet configured to authenticate with Acquia Cloud, do you want to setup?", "y"); + if ($startConfirm) { + $apiKey = $this->askHidden("Please enter an Acquia Cloud API Key"); + $apiSecret = $this->askHidden("Please enter an Acquia Cloud Secret"); + try { + if ($this->saveCredentials($apiKey, $apiSecret)) { + $this->say("Credentials saved successfully."); + return self::EX_NORMAL; + } + else { + $this->say("Failed to save credentials."); + return self::EX_CONFIG; + } + } + catch (FileSaveException $e) { + $this->writeln($e->getMessage()); + return $e->getCode() ?: 1; + } + } + else { + $this->say("Setup cancelled. Exiting..."); + return self::EX_NORMAL; + } + } + + /** + * Saves API credentials to a configuration file. + * + * @param string $apiKey The API key to be saved. + * @param string $apiSecret The API secret to be saved. + * + * @return bool Returns TRUE on success, FALSE on failure. + * @throws \OsuWams\Exception\FileSaveException + */ + private function saveCredentials(string $apiKey, string $apiSecret): bool { + $configDir = $this->getConfigDir(); + if (!is_dir($configDir) && !mkdir($configDir, 0755, TRUE)) { + return FALSE; + } + + $configPath = "$configDir/acquia-cli.yml"; + $configData = [ + 'acquia' => [ + 'key' => $apiKey, + 'secret' => $apiSecret, + ], + ]; + $configContent = Yaml::dump($configData, 4, 2); + + if (file_put_contents($configPath, $configContent) === FALSE) { + throw new FileSaveException("Failed to save the file: $configPath"); + } + return TRUE; + } + + /** + * Retrieves the configuration directory path. + * + * @return string The path to the configuration directory. + */ + private function getConfigDir(): string { + return join(DIRECTORY_SEPARATOR, [ + getenv('HOME') ?: getenv('USERPROFILE'), + '.acquia', + ]); + } + +} diff --git a/src/Exception/FileSaveException.php b/src/Exception/FileSaveException.php new file mode 100644 index 0000000..bc3ed0a --- /dev/null +++ b/src/Exception/FileSaveException.php @@ -0,0 +1,7 @@ +