Skip to content

Commit

Permalink
Merge pull request #9 from osu-wams/add-setup
Browse files Browse the repository at this point in the history
Add setup
  • Loading branch information
emerham authored Sep 30, 2024
2 parents 2ac6be9 + 8d985e5 commit 643a9e4
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
8 changes: 8 additions & 0 deletions bin/osu-acquia-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
9 changes: 8 additions & 1 deletion src/Cli/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
139 changes: 139 additions & 0 deletions src/CliSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace OsuWams;

use OsuWams\Exception\FileSaveException;
use Robo\Tasks;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

/**
* This class handles the CLI setup for authenticating with Acquia Cloud.
*/
class CliSetup extends Tasks {

/**
* Exit status code indicating a configuration error.
*
* The value of this constant is 78.
*
* @var int
*/
private const EX_CONFIG = 78;

/**
* Exit status code for normal operations.
*
* The value of this constant is 0;
*
* @var int
*/
private const EX_NORMAL = 0;

/**
* The input interface.
*
* @var \Symfony\Component\Console\Input\InputInterface
*/
protected $input;

/**
* The Output interface.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* Constructor method for initializing input and output interfaces.
*
* @param InputInterface $input The input interface instance.
* @param OutputInterface $output The output interface instance.
*
* @return void
*/
public function __construct(InputInterface $input, OutputInterface $output) {
$this->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',
]);
}

}
7 changes: 7 additions & 0 deletions src/Exception/FileSaveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace OsuWams\Exception;

class FileSaveException extends \Exception {

}

0 comments on commit 643a9e4

Please sign in to comment.