Skip to content

Commit

Permalink
UHF-8525: Allow values to be passed as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Sep 8, 2023
1 parent c937db2 commit 530bdd4
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/Commands/ApiAccountCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,26 @@ public function reveal(string $value) : int {
*/
#[Command(name: 'helfi:update-api-secret')]
#[Option(name: 'filename', description: 'An optional filename to read the secret from.')]
public function update(array $options = ['filename' => NULL]) : int {
$type = $this->askType();
#[Option(name: 'type', description: 'The type of given secret. [1 = DRUPAL_VAULT_ACCOUNTS or 2 = DRUPAL_API_ACCOUNTS')]
#[Option(name: 'id', description: 'An unique ID for given item.')]
#[Option(name: 'plugin', description: 'The plugin')]
#[Option(name: 'data', description: 'The data.')]
public function update(array $options = [
'value' => NULL,
'type' => NULL,
'id' => NULL,
'plugin' => NULL,
'data' => NULL,
]) : int {
$type = $this->askType($options['type']);

if ($options['filename']) {
$value = file_get_contents($options['filename']);
}
else {
$value = $this->io()->ask('The base64 and JSON encoded secret value');

if (strlen($value) >= 4095) {
if ($value && strlen($value) >= 4095) {
throw new \InvalidArgumentException('The secret value is longer than 4096 bytes and will be truncated by your terminal. Use --file to pass a file to read the content from instead.');
}
}
Expand All @@ -78,7 +88,7 @@ public function update(array $options = ['filename' => NULL]) : int {
$this->io()
->note(sprintf('Current value: %s', json_encode($values, flags: JSON_PRETTY_PRINT)));

$values = array_merge($values, [$this->processValues($type)]);
$values = array_merge($values, [$this->processValues($type, $options)]);

$this->io()->note(sprintf('New value: %s', json_encode($values)));
$this->io()
Expand Down Expand Up @@ -118,12 +128,17 @@ public function create() : int {
/**
* Prompts the user for the secret type.
*
* @param string|null $type
* The type.
*
* @return string
* The secret type.
*/
private function askType() : string {
$type = $this->io()
->ask('The type of given secret [1 = DRUPAL_VAULT_ACCOUNTS or 2 = DRUPAL_API_ACCOUNTS]', '2');
private function askType(string $type = NULL) : string {
if (!$type) {
$type = $this->io()
->ask('The type of given secret [1 = DRUPAL_VAULT_ACCOUNTS or 2 = DRUPAL_API_ACCOUNTS]', '2');
}

return match($type) {
'1' => 'DRUPAL_VAULT_ACCOUNTS',
Expand All @@ -137,29 +152,33 @@ private function askType() : string {
*
* @param string $type
* The secret type.
* @param array $options
* The default options.
*
* @return array
* The processed field values.
*/
private function processValues(string $type) : array {
private function processValues(string $type, array $options = []) : array {
$values = [];

foreach ($this->getFields($type) as $name => $options) {
foreach ($this->getFields($type) as $name => $option) {
[
'description' => $description,
'default_value' => $defaultValue,
'callback' => $callback,
] = $options;
] = $option;

if (!$callback) {
$callback = function ($value) {
return trim($value);
};
}

$value = $this->io()
->ask(sprintf('Provide %s [%s]', $name, $description), $defaultValue);
$value = $callback($value);
if ((!isset($options[$name])) || is_null($options[$name])) {
$options[$name] = $this->io()
->ask(sprintf('Provide %s [%s]', $name, $description), $defaultValue);
}
$value = $callback($options[$name]);

if (!$value) {
continue;
Expand Down

0 comments on commit 530bdd4

Please sign in to comment.