-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Add bin/magento commands to list store/website data #7982
Conversation
Just FYI, these command also exist in n98/magerun2, but contain less detailed output then what you are suggesting: $ vendor/bin/n98-magerun2 sys:website:list
Magento Websites
+----+------+
| id | code |
+----+------+
| 1 | base |
+----+------+
$ vendor/bin/n98-magerun2 sys:store:list
+----+------+
| id | code |
+----+------+
| 1 | nl |
| 3 | fr |
+----+------+
So if this PR is accepted, I think it's best if these commands then are removed from n98/magerun2, since otherwise we will have 2 commands to do the same thing. |
@hostep From a purely outsider point of view, I'm wondering whether any of the commands in n98-magerun2 should have any bearing on what is baked into the magento core. Eg, there are many third party apps that handle layered navigation but magento still includes their own one. Furthermore, if the core I do like magerun a lot and have contributed to the v1 version, but am just not sure where the divide in logic goes now that a lot of the core deployment steps are baked into Also I know the tests are failing for this PR, will review. |
Well, n98-magerun2 works by inheriting all the commands from The upside of the n98-magerun2 project, is that new (and useful) features are implemented much faster then in Magento core. It wouldn't surprise me that this PR is going to take a couple of months to be included into core Magento, while if you would create a PR to enhance the feature in n98-magerun2, it will probably be released in a new version in less then a month time. And you could then use it on all Magento 2 versions and not only on the very last one. I do agree with you that some features from n98-magerun2 might as well be ported over to core Magento for users who don't want to use n98-magerun2, but then those users will have to wait a long time until it happens I'm afraid. In the end, it all boils down to what the maintainers from Magento and n98-magerun2 agree upon on where certain features should be implemented I think. Just my 2 cents :) |
@hostep All valid points regarding M2 turnaround time etc. However, I'd at least want to try and make the magento 2 code a better place to be rather than building scaffolding on top of it. Thanks for highlighting this to me, I wasn't aware of how magerun2 worked and that does seem like an area for conflict. |
|
11ee118
to
6fe5e4f
Compare
* Class StoreListCommand | ||
* @package Magento\Store\Console\Command | ||
*/ | ||
class StoreListCommand extends Command |
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.
Have you tried to run these commands when magento is not installed? Please check this case. Requesting StoreManager in constructor might lead to DB calls.
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.
@antonkril Damn fine point you make there. The additions I made threw an exception when I ran bin/magento
without any parameters at all on an uninstalled instance.
I couldn't see any precedence for conditional loading of console classes, so I rolled my own as a proof of concept. Is there a preferred way to handle this kind of thing or is my solution acceptable?
Please review, thank you.
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.
@antonkril Any thoughts on the above? :)
Hi @convenient , there are several ways of registering Magento CLI commands. The one used in Sample Data is to allow the commands appear before the application is installed. This is an exceptional case. Usually, commands from the modules register the commands via |
|
||
/** | ||
* Class StoreListCommand | ||
* @package Magento\Store\Console\Command |
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 add more details in the description of the classes. Also, @package
is not necessary.
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.
phpdoc updated in commit a9ca0045d50d5a94e0f83fe7c9ef293e7957438e
The format for this updated doc is in a format consistent with app/code/Magento/Cron/Console/Command/CronCommand.php
Thank you for explaining all that @buskamuza. I guess my big mistake was using the I've fixed this, tested locally, and have pushed up the changed. Travis is showing all green too. Just waiting on codacy. Many thanks |
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.
The implementation looks good. But there are couple necessary changes:
- See new comment in the code
- Please implement tests
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$table = $this->getHelperSet()->get('table'); |
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 wrap the logic in try-catch
and specify return codes for success and failure explicitly. See an example in https://github.com/magento/magento2/blob/develop/app/code/Magento/Deploy/Console/Command/SetModeCommand.php#L108
The same for another test
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.
Will do 👍
- magento#7982 (comment) - As @buskamuza suggested
a9ca004
to
1b8f48c
Compare
1b8f48c
to
591ca38
Compare
- Return non-zero code on error - With unit tests
591ca38
to
996de8a
Compare
I've rebased so just to be clear what i've done
Is that everything you need? |
|
||
return \Magento\Framework\Console\Cli::RETURN_SUCCESS; | ||
} catch (\Exception $e) { | ||
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
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.
Similar catch logic is already implemented in the CLI entry point script in bin/magento. I would suggest not to duplicate similar code here. The one difference which I see is that this code will output exception message regardless of verbosity. Can this be done using exact exception types for the cases where you want to output exceptions?
One problem which I see is that the return code is just returned from the execute method. CLI framework will consider that this is valid result of the command and will end process successfully.
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.
Oh, just checked comments from previous reviewers. Let me sync up with them.
|
||
return \Magento\Framework\Console\Cli::RETURN_SUCCESS; | ||
} catch (\Exception $e) { | ||
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
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.
same comment about catching logic here
@convenient Than you, I've added 2 comments. Can you please take a look. |
@convenient nevermind previous comment, all looks good. Apparently this is new convention in CLI framework to require wrapping in try catch for each command. |
@vrann Thanks for the updates, you gave me a scare there with so many emails ;) |
Proposal
I used commands in n98-magerun to output a list of store data and website data, nothing that can't be achieved by connecting to the database and doing a few
SELECT
statements but the magerun wrapper was very helpful in Magento 1.I'd like to have these commands back in use for Magento 2
Naming things
I think the code for the
website:list
command should live in the same directory as thestore:list
command because both entities are part of theMage/Store
module. Also any commands in theMage/Store
module should be prefixed withstore
. However, this gives us the odd naming hierarchical structure in the command names listed, listing a website has to include the wordstore
or break convention.If anyone has any suggestions I'm all ears, I've gone for option 1 by default and option 3 breaks convention.
A comment on my code
I'm not sure if this is the correct standard for creating new CLI commands. I copied the approach from the sample data module. If this method is outdated shout out.
Example usage and output