NickCarneiro / remtail

tail log files from multiple remote hosts
MIT License
137 stars 5 forks source link

Add bash completion #13

Open mjpitz opened 9 years ago

mjpitz commented 9 years ago

Thought about this a little bit, but I typically use this to tail the same files every couple of days. We should be able to add bash completion support by creating a history file and using it to auto-complete values that you've entered in the past.

Technical Details

Example The history file currently contains:

4    trillworks.com:/var/log/nginx/access.log
2    okpedro.com:/var/log/apache2/other_vhosts_access.log

After executing the remtail trillworks.com:/var/log/nginx/access.log, the new history file would be:

5    trillworks.com:/var/log/nginx/access.log
2    okpedro.com:/var/log/apache2/other_vhosts_access.log

Not sure if the count is necessary as these will be auto complete values and as far as bash is concerned for the auto-complete, it would just use second part of the line anyway.

NickCarneiro commented 9 years ago

Great idea. I often find myself sshing into the remote hosts just to get the paths right which is a huge pain.

mjpitz commented 9 years ago

Worked this up really quick. One big bug with the way it handles the completion post : . Looked into it a little but didn't spend too much time on it. I also had another iteration where I was using -F instead of -C for the results but I think this one is a little cleaner (although not by much). I think the completion is trying to treat the : as a word separator

_remtail_complete() {
    local word=${COMP_WORDS[COMP_CWORD]}
    local completions="$(cat ~/.remtail_history | awk '{print $2}' | grep ^$word)"

    COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}

complete -F _remtail_complete remtail
NickCarneiro commented 9 years ago

There is value is rolling this into the javascript code. Then we can autocomplete command flags: https://github.com/NickCarneiro/remtail/commit/760c935bd5baa1f6c4afea4ecf596b1d2bf5f5f7

Not done, but you can see where it was going.