Problem/Motivation
We want to make adding output formats easy.
Proposed resolution
Add a function name template
function drush_format_X()
This make commands like drush --format=json vget working ok as the patch provides for
function drush_format_json()
function drush_format_print_r()
function drush_format_export()
Remaining tasks
How do we tell what format X does. We need to implement hook_drush_output_formats().
How to invoke these. See #3 #4 #5 #6
User interface changes
API changes
Original report by clemens.tolboom
As a side patch of #1366098: Automatically download Symfony YAML component Rob Loach suggests to add a drush_format_ hook
This solution would allow for #1396178: Add properties output as a --format easily
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | drush_format-1441026-14.patch | 7.78 KB | damiankloip |
| #46 | drush_format-1441026-13.patch | 7.84 KB | damiankloip |
| #43 | drush_format-1441026-12.patch | 7.76 KB | damiankloip |
| #38 | drush_format-1441026-11.patch | 7.77 KB | damiankloip |
| #37 | drush_format-1441026-10.patch | 7.74 KB | damiankloip |
Comments
Comment #1
clemens.tolboomI needed to add a drush_format_drush_r
Comment #2
clemens.tolboomThis is about print_r
Comment #3
damiankloip commentedThis patch will work well to use the formats. My main concern would be that any other drush commands that used --format would not indicate any other formats. Not sure if we can consider making the --format help text dynamic?
so instead of the above implementation based on the function name we could have a new info hook, something like hook_drush_formats() that could be implemented by modules and drush core to declare formats/callbacks etc...
So we could have something like:
All of the various format info could then be used to populate --format help info text etc...
Comment #4
clemens.tolboomGood point about displaying the formats. But the callback can go as drush_format_x will do.
Trying to implement this I'm puzzled how to get the hook called as the format does not belong to a command.
$formats = drush_command_implements('output_formats');is not workable.We need something like
and a way to get this invoke through
function drush_print_help($command)I guess.Note a renamed it to hook_drush_output_formats as we can have input format too.
Comment #4.0
clemens.tolboomUpdated issue summary.
Comment #4.1
clemens.tolboomUpdated issue summary.
Comment #4.2
clemens.tolboomUpdated issue summary.
Comment #5
clemens.tolboomAdded the hook implementation for output.inc
But don't know how to invoke these :(
See to needs review as we could use some feedback.
Comment #6
clemens.tolboomThe following is not working as the formats are not command bound
Comment #7
clemens.tolboomfunction name should be drush_output_format as we also can have input formats in the future.
same
same
Comment #8
greg.1.anderson commentedSorry for the confusing function names.
drush_invokeis for invoking Drush commands. You're looking fordrush_command_invoke_allordrush_command_invoke_all_ref, which invoke command hooks.Kind of odd that the function that invokes commands does not say "command", and the function that invokes hooks does.
Comment #9
damiankloip commentedI was just about to post pretty much the same answer! :)
@clemens.tolboom I have nearly finished a patch that I have been working on this afternoon that already uses drush_command_invoke_all. Hopefully will post tomorrow.
@greg.1.anderson In order for core output formats to be invoked from output_HOOK I have created an output.drush.inc file that goes in the commands/core directory. Is there a better way to do this?
Comment #10
clemens.tolboom@greg.1.anderson
Unfortunately we are _NOT_ talking about a command.
This is about --format for which we think we need a hook. All invokes I cheched (the 3 you mentioned) iterate over commands. So I guess we are stuck for listing the available formats.
The patch is useful as it is for allowing *any* value for --format as the fallback is to print_r.
Maybe we should create a new patch [edit] for the hook_drush_output_format[/edit] and let this in asap?
Comment #11
greg.1.anderson commented@clemens.tolboom: Actually, drush_command_invoke_all is about hooks; however, in Drush, all hook functions must belong to some commandfile. This is analogous to Drupal, where every hook must belong to some module. The Drush commandfile that contains the hooks does not necessarily need to contain any commands; it just needs to be named *.drush.inc. See examples/sync_enable.drush.inc for an example.
@damiankloip: commands/core/output.drush.inc sounds like an okay place to put the default output hooks. I would guess, though, that you'd want to make the output hooks just thin functions that called through to a utility output routine that did the actual formatting. I would put the later in includes/output.inc. If the stuff that remains is really small, you might just put it into commands/core/core.drush.inc. If there are a bunch of hooks, though, having a separate file is probably a good idea.
Comment #12
clemens.tolboom@greg.1.anderson ah thats cool.
@damiankloip I leave it up to you for now :)
Comment #13
damiankloip commentedOk, so here is my first initial patch, based on the above patches from @RobLoach and @clements.tolboom.
As @greg.1.anderson described in #11, the core implementation of hook_drush_output_formats lives in core.drush.inc, and the function callbacks live in output.inc file.
In output.inc there is currently drush_print_r and drush_format_print_r, which is used by drush_format. I think we could just use one but I didn't want to just get rid of drush_print_r current implementation as it is slightly different to that used by drush_format.
I have kept the hook implementation using an array containing a function callback to use, so this can be specified, rather than conforming to a particular function naming convention. Although this can easily change to something like Clemens described in #4.
I have also added details to drush.api.php etc...
Comment #14
msonnabaum commentedLooks pretty good to me. Interested in how the other maintainers are feeling about this.
Comment #15
moshe weitzman commentedLooks good to me too. Are going to convert existing functions to use this in another patch? We have variable commands, cache commands, user-information, ...
This function is passed $input (string to be formatted). Callback is passed an array, not a string. At least thats what drush_format_print_r() documents.Use all capitals for JSON, not Json. It is an acronym.
Comment #16
damiankloip commented@moshe - I was going to create another patch with any changes to existing drush code in another patch yes. Provided this one got the nod of approval for the implementation of it :)
I will look at the documentation and naming etc... of drush_format_ functions and JSON naming.
Should we have 'drush_print_r' and 'drush_format_print_r'? It is currently like this in the patch, mainly to preserve other calls to drush_print_r.
Comment #17
moshe weitzman commentedYou should change existing calls to drush_format_print_r() and then remove drush_print_r
Comment #18
damiankloip commentedok, no problem, Will get onto this a bit later.
Comment #19
greg.1.anderson commented#17: Really? I would think that if we were moving to a single function, drush_print_r would be preferable. The current signature of drush_format_print_r is call-time compatible with drush_print_r, so I don't see the value of keeping the "_format_" when we already have "drush_".Comment #20
clemens.tolboomThis should be named drush_format_get_output_formats.
Comment #21
damiankloip commented@clemens.tolboom I think you might be right, it goes with the whole _output_formats thing...
Comment #22
damiankloip commentedComment #23
greg.1.anderson commentedCareless, I forgot about $handle in drush_print_r. I'm indifferent about the resolution of this.
Comment #24
damiankloip commentedSo am I right in thinking we get rid of drush_print_r in favour of drush_output_print_r, then any calls to drush_print_r in drush code get changed and wrapped in drush_print too? Just to make sure!
Comment #25
greg.1.anderson commentedChange existing calls to drush_print_r to drush_output_print_r; don't expand-in-place the old version of drush_print_r everywhere.
Comment #26
msonnabaum commentedDo we really want to remove drush_print_r()? How about we just mark it as deprecated so we dont break a ton of custom/contrib commands?
Comment #27
moshe weitzman commentedI don't feel strongly, but a major release like Drush5 is a perfect time to break.
Comment #28
msonnabaum commentedSure, just saying there's very little reason to. Let's just make it a wrapper around drush_format, mark it as deprecated and remove it in 6.
Comment #29
damiankloip commentedHere is an updated patch.
I have changed the few core instances to use drush_format instead (they have been wrapped in drush_print too, so they preserve same output as drush_print_r) E.g. in drush_sql_conf function. So now you could also use the --format option with commands like this too (with --strict=0 option atm). We could add this to the options in hook_drush_command too?
OR would you like to keep the core uses of drush_print_r as they were and use drush_print_r wrapper function I have added back to output.inc? I think it would be ok to change core implementations but leave drush_print_r wrapper in for other drush extensions (as said in #28).
I was thinking the drush_format_get_output_formats function could be used in hook_drush_command info to display the types; So in drush_entity I was thinking of using something like this:
You get the idea!
Comment #30
clemens.tolboomWhat still puzzles me is:
When this goes in @damiankloip and @clemens.tolboom would love to have #1396178: Add properties output as a --format in as well.
Comment #31
damiankloip commentedI'm not sure if drush_help should be changed for drush 5? That's why I was thinking that when the format option is used in hook_drush_command users can call the drush_format_get_output_formats to populate the list.
Agree that it would be good to have a way of declaring which commands can use which types. Although not sure how this would work atm.
Would love to see properties as a --format! :) Although, as this is very handy for drush_entity we could put it in there easy enough (I did this yesterday) with the new hook for formats.
Comment #32
clemens.tolboomComment #33
damiankloip commentedComment #34
damiankloip commentedHere is latest patch based on comments from @moshe in #drush...
Main change to note: changed print_r function to handle regular string with labels (as used by vget).
Comment #35
jonhattanInitialize $formats to avoid a warning.
Btw it is pretty related to #1364808: Proposal for an output formats engine.
Same thing.
Comment #36
damiankloip commentedcore_drush_output_formats has been removed from core.drush.inc, in favour of drush_output_formats in output.inc which invokes hook_drush_output_formats from there. static $error code and print_r fallback also removed.
Comment #37
damiankloip commentedAnd the patch would be good.....
Comment #38
damiankloip commentedRe rolled with comment from @jonhattan in #35 fixed.
Comment #39
moshe weitzman commentedStill seeing test failures. For example, try
cd tests && phpunit --filter=testVariableComment #40
damiankloip commented@moshe, I am just looking at this now. I'm not sure why this is failing/getting the null value. When running all of the commands manually they work fine. What am I missing here?
Comment #41
damiankloip commentedThis test fails on the current dev branch without this patch applied. So maybe it is a problem with the test?
Comment #42
clemens.tolboomI'm puzzled with the default format. Say
or (drush_entity)
that is 'pipe' the back-tick result into the next command.
Are we now forcing users to specify a --format=plain or am i missing something?
Comment #43
damiankloip commented@clemens, Not sure what you mean exactly. You think we need to have another output format for piped?
Here is a slightly updated patch, to make sure export returns a value (Don't think it did before). Not sure if the tests will pass yet? I don't trust my machine atm as it's failing for that test on the current master branch without this patch!
Comment #44
moshe weitzman commentedI am still seeing tests fail. To isolate, try this with and without the patch -
phpunit --filter=testVariable .. It is possible that we have a problem only with code that runs through drush_print_pipe(), as this code does. We are doing unwanted quoting of the string to be printed.Comment #45
jhedstromThe specific failure in the test is a syntax error in eval'd code, caused to over-escaping:
string(58) "'$variables[\'date_default_timezone\'] = \'US/Mountain\';'"Comment #46
damiankloip commented@jhedstrom @moshe, I knew it was something going on with the eval of the different strings etc... :)
Here is an updated patch that now passes testing.
I only got one failure back which was in makeMakefileCase::testMakeNoPatchTxt - "Unexpected exit code: /usr/bin/drush --nocolor make /usr/share/drush/tests/makefiles/patches.make --no-core --no-patch-txt --test --md5=print" - It doesn't look like it's anything to do with this patch though (correct me if I'm wrong).
Hopefully this patch is ready to go now?
Comment #47
damiankloip commentedComment #48
damiankloip commentedHere is re rolled patch to remove --pipe format in drush_format and some doxygen comment amends.
Comment #49
damiankloip commented@moshe, Can you give some thoughts/pointers on these points?
Are we just removing the use of --pipe when it is used as an alias of the export format? or all instances of where --pipe is used? Or were you thinking we should make pipe an output format that uses the current drush_print_pipe function? Or does drush_print_pipe need to use drush format instead? I'm not sure of the various implications at the moment.
Depending on this I will need to update tests, other pipe uses, hook_drush_command implementations etc.. accordingly. The tests that use --pipe as an export output alias will pass now when the tests are changed. It depends what else you would like me too change too.
Comment #50
moshe weitzman commentedLatest patch is failing similar to the failures we were seeing before #46. I think you can change all commands that use --pipe to --format. We may need the commands a bit so they can return reasonable json, etc. An example is drush_field_info() which would start emitting json and such instead of csv. The csv format would go away for now.
Comment #51
damiankloip commentedYep, that's what I was saying about that yesterday, if we remove the --pipe option from drush_format we get failures as the test is still using the --pipe option. I have tested this on the local with --format and the tests pass, so the new patch will include all of this too.
I will change everything to use drush_format. What about the newline output of drush_print_pipe? Will this be missed?
Comment #52
damiankloip commentedComment #53
moshe weitzman commentedComment #54
damiankloip commentedDo you think it's an idea to start with an initial commit of the patch in #46 that deals with the output formats hook. Then all of the replacement of drush_print_pipe should be in another patch as I think there will be much more debate around this.
I still have a sandbox at http://drupal.org/sandbox/damiankloip/1485656 where I am continuing this work around universal drush_format usage.
Flip back to needs work etc... if you don't agree!
Comment #55
moshe weitzman commentedComment #56
clemens.tolboom@moshe weitzman
Could you please add more info about the reasons why 'needs works'. It's quite non-informative to see an issue going to 'needs work' without clues. Really :)
Comment #57
greg.1.anderson commentedSuperseded by #1364808: Proposal for an output formats engine and #1866344: Make --pipe awesome with output format engines.
Comment #57.0
greg.1.anderson commentedUpdated issue summary.