After "drush use @local" I would like the output of "drush site-alias" to indicate that @local is "active", similar to "git checkout 7.x; git branch". For example,

$ drush use @local
Site set to @local
$ drush sa
@local (* active *)
@none
default

It would also be nice to have "drush -v sa" remind me that I can use "drush use @alias" to switch site aliases or "drush use -" to pop the current one from the stack. (I think it is a stack ...)

Comments

greg.1.anderson’s picture

There is a __drush_ps1() function in drush.complete.sh that you can add to your PS1 to show your active site alias right on your prompt.

I think that patches that add git-style active indicators (prefix each line with "* " or " ") would be considered favorably.

benjifisher’s picture

Status: Active » Needs review
StatusFileSize
new1.24 KB

@greg.1.anderson:

Thanks for the hint. It can be frustrating to submit a patch and get no attention.

Patch attached. It provides git-style indicators, as suggested. If no site alias has been set, then there is no change.

When testing, remember that if you set a remote alias, then "drush sa" is executed remotely.

Note: My guess was that "drush use" kept a stack of site aliases. After looking at the code, I see that it keeps only the current alias and one alternative.

benjifisher’s picture

I have had two thoughts since writing the patch in #2:

  1. The change should be documented. Maybe add a line to the help text for the site-alias (sa) command, or maybe the site-set (use) command. Anyplace else?
  2. It would also be nice to indicate the site alias, if any, that will become active after drush use -. The only question is what marks to use for the active and alternate site aliases: * (or + or =) and -? Or maybe (following vim) % and #?

Or maybe you prefer to keep it the way it is.

I meant to note earlier that the patch in #2 has no effect if site-alias is invoked with the --table or --full options.

greg.1.anderson’s picture

Status: Needs review » Needs work

I think this is okay with the addition of the suggested documentation. I'd also be okay with the "-" on the previous alias, if desired.

benjifisher’s picture

Status: Needs work » Needs review
StatusFileSize
new2.66 KB

The attached patch adds a line or two to the help text, as suggested in #3, and marks the previous alias with "-".

When testing, beware of remote aliases (see #2).

moshe weitzman’s picture

Version: 7.x-5.8 » 8.x-6.x-dev
Assigned: Unassigned » greg.1.anderson
greg.1.anderson’s picture

Status: Needs review » Needs work

This should be reworked once #1866344: Make --pipe awesome with output format engines lands. The other alias marker should be placed one of the table columns, and the default format should be table. Other format types should be supported.

I'm tempted to just close this and fix it with #1866344, but will keep it open for now.

moshe weitzman’s picture

[ #1866344] has landed. I guess work here can proceed? I agree with the initial suggestion for folks to customize their bash prompt. It is really the best way.

benjifisher’s picture

Do you mean the suggestion to use __drush_ps1() in #1? That is already implemented (unless it was removed after 7.x-5.8).

Personally, I think that my bash prompt is already too long. I do not want to add more to it. The time I want to see this information is when I look at my site aliases.

If people think that adding "* " for the current site alias and "- " for the previous alias (and " " for all others), then I will have a look at re-rolling the patch in #5.

greg.1.anderson’s picture

If you run drush sa --format=table on the latest 8.x-6.x-dev, you will get a tabular output with fields for the alias id, root, uri, host and user (the last two for remote aliases only). This table is too wide for most terminal windows, and probably would not make a good default format.

It would be easy enough to add a new field to the site-alias command that indicated the last and next-to-last aliases used. I think that "*" for the last alias and "-" for the next-to-last (with empty for the others) would be just fine.

I'm not sure what the default format should be for the sa command, but if you made the default format 'table' and the default fields the last-alias marker and the alias ID, that would give a result in line with what was suggested previously. If you then ran sa with the --full option, you'd get more fields, in line with the current --format=table output, so I think that would probably work out.

moshe weitzman’s picture

@Benji - Care to implement #10? This requires some knowledge of output formats. Would be great if you and others became familiar with this new system.

greg.1.anderson’s picture

Assigned: greg.1.anderson » Unassigned

Unassigning. I'll help answer questions; see drush topic docs-output-formats for an introduction.

benjifisher’s picture

@Moshe: thanks for reminding me about this. Yes, I think I can implement it.

@greg.1.anderson: I had a look at the docs. No questions so far; thanks for the offer.

benjifisher’s picture

OK, I do have some questions. I think some of the documentation is incomplete, but maybe I am misreading it or my environment may be funny.

I checked out the latest 8.x-6.x branch and "drush cc drush". I do not have a drush config file on this machine. (At least, that is what "drush st" tells me.)

According to "drush help sa",

--pipe Equivalent to --format=var_export.

but "drush sa --pipe" gives me the simple list I am used to (same as "drush sa") whereas using "--format=var_export" gives what is described in drush topic docs-output-formats. Is this a bug?

According to drush topic docs-output-formats

::: --format=config :::
A configuration file in executable php format. The variable name is "config", and the variable keys are taken from the output data array's keys.

It appears that "config" can be overridden by specifying (for example) 'variable-name' => 'aliases', in the 'outputformat' array. (See sitealias_drush_command())

What does the 'field-mappings' key do?

According to drush help sa, the default format is "config", but "drush sa --format=config" gives something different from "drush sa".

Changing the 'default' key in the 'outputformat' array (say to "table") does not seem to have any effect.

benjifisher’s picture

I can answer some of my own questions.

  1. If I try drush sa @dev then --pipe behaves almost as expected. The only difference between --pipe and --format=var_export is that the latter ends with an additional newline.
  2. According to my experiments, 'variable-name' does work as I thought.
  3. I still do not know what 'field-mappings' does.
  4. The default format of config applies to drush sa @dev
  5. Changing the 'default' key affects drush sa @dev

In short, the documentation on formats for drush sa applies if I give it an argument.

I even found out where the output format is changed if I do not give any arguments: the line

drush_set_default_outputformat('list', array('field-labels' => array('#name' => 'Name')));

in _drush_sitealias_user_specified_list().

New questions:

  1. I propose moving this line to the main callback; I do not think it belongs where it is. Any objections?
  2. If I change 'list' to 'table' here, I get tabular output by default, but with the default fields (Name, Root, URI, Host, User). The second argument to drush_set_default_outputformat() does not seem to make any difference. For example, I tried array('#name' => 'Foo') and I tried adding a 'fields-default' key. How can I specify which fields to display?
moshe weitzman’s picture

3. I think that field-mappings is a lookup instruction. see its usage at http://api.drush.org/api/drush/includes!output.inc/function/drush_rows_o...

1. I don't that it matters much where this line is. Go ahead and move it if you wish.
2. This is controlled by the 'fields-default' key in the command definition. I confirmed by addeding uri to this array and then --format=table started printing out the uri from a site alias. Arguably that should be in the default fields.

moshe weitzman’s picture

site-alias command gets slightly complex with outputformat because it does two things depending if an argument was provided: list or detail. This is a common pattern - see test-run, watchdog-list/watchdog-show, queue-list/queue-run, and views-list/views-execute. Maybe we should add a site-list command and require folks to use that for site alias listings? If we go that route, it makes sense to split our part of test-run into a new test-list command.

Thoughts?

greg.1.anderson’s picture

Right now, site-alias in default (list) mode is a lot slower than it needs to be, because the site-alias command does not know (does not check) how its output format will handle the data. In list mode, only the array key is needed, but the full data structure is produced, because the implementation is very general-purpose. If we had a site-list command, it could execute more effectively.

Despite that fact, from a UX point of view, I wouldn't really want to have a bunch of *-list variants for all commands that are like this. If we did implement the commands like this for performance reasons, maybe we could intelligently have the command dispatcher switch from the 'site-alias' command to the 'site-alias-list' command (which is aliased to 'site-list' and 'sl') iff the selected output format is a simple list.

Just thinking aloud; not sure of the best solution.

moshe weitzman’s picture

Assigned: Unassigned » moshe weitzman

I like the command dispatcher approach. I think site-alias-list could be a hidden command. I can take this on. If this turns out well, we'd want to unify those other commands I mentioned on the same approach.

@benjifisher - sorry to hijack your issue a bit. feel free to continue mulling how we implement the site-set marker and what the default fields should be for a listing.

benjifisher’s picture

Moshe:

Hijack away. I think that I have done my job as a beta tester--which is pretty much what you asked for in #11--if I ask good questions and you end up doing the implementation. In the mean time, worrying about the 'field-mappings' key led me to submit #2047241: drush_lookup_field_by_path() always returns empty string if $path is an array and #2047209: Document $mapping in drush_lookup_field_by_path() and 'field-mappings' in drush_rows_of_key_value_to_array_table().

... from a UX point of view, I wouldn't really want to have a bunch of *-list variants ...

I agree. We design things differently if they are intended to be used interactively. Think vi regular expressions vs. PCRE.

Right now, site-alias in default (list) mode is a lot slower than it needs to be, ...

This would be easy to fix. Just change the code at the point where you check whether any arguments were passed.

I think the real difficulty with this issue, as I explored in #14 and #15 and Moshe summarized in #17, is that drush sa and drush sa @dev are really two commands, with different default formats. Right now, that is accomplished with drush_set_default_outputformat(). (This function does not work the way I expect, which may or may not mean that it simply does not work.) I think a cleaner approach is to use two different commands internally, so that each can have its own set of defaults. IIUC, this is what you have in mind with "the command dispatcher approach."

moshe weitzman’s picture

I don't think we can speed things up here. We want the ability to show a table or csv for a listing that has root/uri etc.

So, I started to implement the original feature request here and ran into a snag. When you have used site-set, all your commands are targetted to the remote site - even site-list! So, you won't ever see a site marked as being active. We could probably force sa command to always operate locally, but I don't think thats wise.

When I was testing this, I once again smiled as my bash prompt told me what site was active. I really encourage folks to source our drush.complete.sh script and then use the __drush_ps1() function in their PS1. This is documented in the README.

Not sure there is anything more we should do here.

greg.1.anderson’s picture

Hm, yes, it would be easy enough to flag the site-list command to always run locally, like ssh does. I think that overall that would be a big improvement, but it would be a source of frustration to anyone who actually did want to run the site-list command remotely, to see what sites are defined on a different machine.

Maybe what we need here is a command-exception list for site-set, so that site-list runs locally when you've set the site to a remote machine, but you can still use drush @remote site-list to inspect the aliases on the remote machine.

It would be good if this level of functionality were supported if we implement the proposal in this issue. I agree that relying on __drush_ps1 works well, and sidesteps the oddities with site-list nicely.

moshe weitzman’s picture

Status: Needs work » Closed (works as designed)

Too complicated. I'm back to recommending the custom prompt.

greg.1.anderson’s picture

Fine by me. I can't think of another command that would want to use the facility described in #22.

benjifisher’s picture

I can't think of another command that would want to use the facility described in #22.

  1. drush site-reset seems to run locally. IMHO, it would be a bug if it did not.
  2. drush site-set does not seem to run locally. I consider this a bug.
  3. As we have been discussing, drush site-alias does not run locally, and I also consider this a bug.

For example, @dev is remote. I do not know if it matters, but locally I am running 6.x from git and the remote server is running 5.9. Real example:

$ drush use @local
Site set to @local
[@local]$ drush use @dev
Site set to @dev
[@dev]$ drush use @local
[@dev]$ drush use -
[@dev]$ drush site-reset
$

Note the prompts. (Disclaimer: I edited out the part of the prompt that is not relevant to this discussion.)

greg.1.anderson’s picture

#25 does look like a bug; you are correct that drush use should always run locally. Please note that we already have a facility to set certain commands to always run locally; the enhancement in #22 was a suggestion to make a command run either remotely or locally depending on -how- the remote site alias was specified.

moshe weitzman’s picture

Status: Closed (works as designed) » Active

I just committed the one line fix to make site-set always run locally.

I added same line to site-alias and branched the current code as follows below. I ran out of time and brain power for this tonight. Not sure I will finish it. I'm having trouble finding the right conditions under which we should drush_invoke_process():

  $site_set = drush_sitealias_site_get();
  $alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS');
  if ($alias && $site_set != $alias) {
    $options = drush_redispatch_get_options();
    $backend_options = array(
      'integrate' => FALSE,
    );
    $values = drush_invoke_process($alias, 'site-alias', func_get_args(), $options, $backend_options);
    return $values['object'];
  else {
    // All the code we run today
greg.1.anderson’s picture

#27 looks about right to me in concept. I don't have time to run it to see if there are any actual operational problems with it; you imply that there is some trouble, but it looks like it must be pretty close to right to me.

greg.1.anderson’s picture

Status: Active » Closed (won't fix)
Issue tags: +Needs migration

This issue was marked closed (won't fix) because Drush has moved to Github.

If this feature is still desired, you may copy it to our Github project. For best results, create a Pull Request that has been updated for the master branch. Post a link here to the PR, and please also change the status of this issue to closed (duplicate).

Please ask support questions on Drupal Answers.