-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Adding prompts to admin:user:create cli command #2690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
steverobbins
wants to merge
2
commits into
magento:develop
from
steverobbins:feature/admin-user-create-prompts
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © 2015 Magento. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
|
|
||
| namespace Magento\Framework\Console; | ||
|
|
||
| use Symfony\Component\Console\Helper\DialogHelper; | ||
| use Symfony\Component\Console\Input\ArgvInput; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class Input extends ArgvInput | ||
| { | ||
| /** | ||
| * Prompt for option value and set as default | ||
| * | ||
| * @param DialogHelper $dialog | ||
| * @param OutputInterface $output | ||
| * @param string $name | ||
| * @param boolean $isHidden | ||
| * @return void | ||
| */ | ||
| public function promptForOption(DialogHelper $dialog, OutputInterface $output, $name, $isHidden = false) | ||
| { | ||
| $method = $isHidden ? 'askHiddenResponse' : 'ask'; | ||
| $this->definition->getOption($name)->setDefault($dialog->$method( | ||
| $output, | ||
| '<info>' . ucwords(str_replace('-', ' ', $name)) . '</info>: ' | ||
| )); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ public function __construct(InstallerFactory $installerFactory, UserValidationRu | |
| protected function configure() | ||
| { | ||
| $this->setName('admin:user:create') | ||
| ->setDescription('Creates an administrator') | ||
| ->setDescription('Creates an administrator. Values for required options will be prompted, if not specified') | ||
| ->setDefinition($this->getOptionsList()); | ||
| parent::configure(); | ||
| } | ||
|
|
@@ -55,6 +55,7 @@ protected function configure() | |
| */ | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $this->setMissingValues($input, $output); | ||
| $errors = $this->validate($input); | ||
| if ($errors) { | ||
| $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>'); | ||
|
|
@@ -123,4 +124,31 @@ public function validate(InputInterface $input) | |
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| /** | ||
| * Get admin user data | ||
| * | ||
| * @param InputInterface $input | ||
| * @param OutputInterface $output | ||
| * @return void | ||
| */ | ||
| protected function setMissingValues(InputInterface &$input, OutputInterface $output) | ||
| { | ||
| if (!method_exists($input, 'promptForOption')) { | ||
| return; | ||
| } | ||
| $dialog = $this->getHelper('dialog'); | ||
| $keys = [ | ||
| AdminAccount::KEY_FIRST_NAME, | ||
| AdminAccount::KEY_LAST_NAME, | ||
| AdminAccount::KEY_USER, | ||
| AdminAccount::KEY_EMAIL, | ||
| AdminAccount::KEY_PASSWORD, | ||
| ]; | ||
| foreach ($keys as $key) { | ||
| if (!$input->getOption($key)) { | ||
| $input->promptForOption($dialog, $output, $key, $key === AdminAccount::KEY_PASSWORD); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check that input actually implements the interface which has promptForOption method |
||
| } | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
setup/src/Magento/Setup/Console/Input/PromptableInputOption.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © 2015 Magento. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
|
|
||
| namespace Magento\Setup\Console\Input; | ||
|
|
||
| use Symfony\Component\Console\Helper\DialogHelper; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\ConsoleOutput; | ||
|
|
||
| class PromptableInputOption extends InputOption | ||
| { | ||
|
|
||
| /** | ||
| * Ignore the default param since we will prompt for it instead | ||
| * | ||
| * @param string | ||
| * @param string|array | ||
| * @param int | ||
| * @param string | ||
| * @param mixed | ||
| */ | ||
| public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null) | ||
| { | ||
| parent::__construct($name, $shortcut, $mode, $description, null); | ||
| } | ||
|
|
||
| /** | ||
| * Prompt for the value to be used if none given, instead of using a default | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function getDefault() | ||
| { | ||
| $default = parent::getDefault(); | ||
| if ($default !== null) { | ||
| return $default; | ||
| } | ||
| $dialog = new DialogHelper; | ||
| $this->setDefault($dialog->ask( | ||
| new ConsoleOutput, | ||
| '<info>' . ucwords(str_replace('-', ' ', $this->getName())) . '</info>: ' | ||
| )); | ||
| return parent::getDefault(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for not being more specific earlier, but better way to achieve this is to introduce new interface, i.e. InputInteractiveInterface, which extends InputInterface. Then here either check is $input an instance of this interface or even better do type hinting to InputInteractiveInterface in the method signature. The latter is not always possible, so either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that what this is? https://github.com/magento/magento2/pull/2690/files#diff-6c32687b440c3791df9e43cba7f888b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify the question please? Literally, there should be an interface with the promptForOption method and here you should either have it in
protected function setMissingValues(InputInteractiveInterface &$input, OutputInterface $output)(better) or callinstanceof(still ok).Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me know does this makes sense to you. I don't want to add too much work :)