romkatv / zsh4humans

A turnkey configuration for Zsh
MIT License
1.75k stars 112 forks source link

[feature request] ssh postcmd and precmd #85

Open kurktchiev opened 3 years ago

kurktchiev commented 3 years ago

I am going to use your history sync as an example of why it could be useful (as described in https://github.com/romkatv/zsh4humans/issues/35#issuecomment-675908718)

() {
  local hist
  for hist in ~/.zsh_history*~$HISTFILE(N); do
    fc -RI $hist
  done
}

This sends along the history files from the local machine to remote

function z4h-ssh-configure() {
  (( z4h_ssh_enable )) || return 0
  local file
  for file in $ZDOTDIR/.zsh_history.*:$z4h_ssh_host(N); do
    (( $+z4h_ssh_send_files[$file] )) && continue
    z4h_ssh_send_files[$file]='"$ZDOTDIR"'${file:t}
  done
}

This brings back the histories from the remote box

zstyle -e ':z4h:ssh:*'                        retrieve-history  'reply=($ZDOTDIR/.zsh_history.${(%):-%m}:$z4h_ssh_host)'

Now with all in place, the retrieved history is not read in until I open a new terminal. In this case it would be useful to have something like (fully recognizing that this can be made smarter to not re-read full history but only freshly retrieved files, its just easier as an example)

function reread_history () {
  local hist
  for hist in ~/.zsh_history*~$HISTFILE(N); do
    fc -RI $hist
  done
}

zstyle -e ':z4h:ssh:*'                        postcmd  'reread_history'

I can also see the usefulness of having a precmd which will let me do and manipulate some things, the idea comes from your own dotfiles using the zstyle ':z4h:term-title:ssh' precmd ${${${Z4H_SSH##*:}//\%/%%}:-%m} directives, i just think there are use cases outside of term-title for it.

romkatv commented 3 years ago

Does this do what you want?

function ssh() {
  # put precmd code here
  z4h ssh "$@"
  # put postcmd code here
}
kurktchiev commented 3 years ago

It would, yes, but it doesnt give me the flexibility as I may want to use specific zstyle's for specific hosts, which in my daily work tends to happen a lot. If I can do:

zstyle -e ':z4h:ssh:*'                  postcmd  'reread_history'
zstyle -e ':z4h:ssh:some_box'  postcmd  'something specific for that host'

It is much more flexible and lets me utilize the already established nomenclature for other z4h controls. or maybe i am trying to force z4h to do too much, which is also a valid answer.

romkatv commented 3 years ago

maybe i am trying to force z4h to do too much, which is also a valid answer.

Asking for more is not a problem. The most valuable thing (for me) would be if you can explain your use case.

What do you want to run in postcmd that depends on the host?

kurktchiev commented 3 years ago

So i connect to a ton of airgapped boxes in which case I use my laptop as the source of truth, which holds all my configs/etc. Since switching to z4h life has become 90% more enjoyable as I can now magic transfer my setup from my source of truth to any remote host.

One caveat (and this is something I think you have added to your already long TODO list) is that when I hop on an airgapped box my setup does not come with me, not huge problem, but it does slow me down.

So to work around the limitation I have cobbled together a custom ssh() much like what you posted, to try and send along the needed files before to trick z4h into thinking everything is in place and it doesnt need to pull anything from the internet.

My limitation to this approach is two fold, its much cleaner to breakout my "workarounds" for airgapped boxes by executing precmd for those hosts that need it and having a "default" or "normal" behavior for anything that doesnt fit that scenario, the postcmd is just nice to have as explained in reading in the history for example, or say resetting some terminal settings, or commands you passed along to say tmux. In some ways it is trying to acomplish similar things to what you mention in the Issue #35 thread around grouping hosts for your history and moving things around based on groups, just using z4h to do the heavy lifting for me.

zstyle -e ':z4h:ssh:air_gapped_boxes*' precmd  'send over z4h and binaries before \
                                           z4h ssh does its install attempt and fails'
zstyle -e ':z4h:ssh:*' postcmd  'reread_history'
zstyle -e ':z4h:ssh:air_gapped_boxes*' postcmd  'reread_history && reset_tmux_layout \
                                            && something_else'
romkatv commented 3 years ago

Do I understand it correctly that you wouldn't need precmd/postcmd if z4h ssh worked for out of the box for air-gapped remote hosts?

kurktchiev commented 3 years ago

Yes that would solve my problems

romkatv commented 3 years ago

Got it. Then I'll need to make z4h ssh work when connecting to air-gapped hosts.

I won't be adding precmd/postcmd hooks at this time. Any feature that I add should have at least one use-case/problem for which it is the best solution. These hooks don't have such a use-case/problem yet, so it's premature to add them.

kurktchiev commented 3 years ago

thats fair, feel free to close