elvish_config/lib/moi/completion/ssh.elv

107 lines
2.1 KiB
Plaintext
Raw Normal View History

2020-03-13 14:03:36 +00:00
use re
use str
use moi/util/condition
use moi/util/file
use moi/util/ip
use moi/util/list
use moi/util/map
use moi/util/option
2021-08-27 20:06:49 +00:00
var options-ssh = [ 1 2 4 6 A D f g I i L l m o v a b C c e F k N n p q R s T t X x ]
var options-scp = [ 3 4 6 B C p q r v c F i l o P S ]
2020-03-13 14:03:36 +00:00
2021-08-27 20:06:49 +00:00
var names = [
benjamin
luence
]
2020-03-13 14:03:36 +00:00
fn -kh {
2021-12-11 21:09:02 +00:00
cat ~/.ssh/known_hosts | peach {|l|
2020-07-22 09:24:43 +00:00
put [(str:split ' ' $l)]
2021-12-11 21:09:02 +00:00
} | peach {|e|
2021-08-27 20:06:49 +00:00
var domains @_ = $@e
str:split ',' $domains
2020-03-13 14:03:36 +00:00
}
}
2021-12-11 21:09:02 +00:00
fn -port {|cmd @argv|
2021-08-27 20:06:49 +00:00
var o = (condition:cset (eq $cmd 'ssh') '-p' '-P')
var margs = (option:map $argv)
var p = (map:value-of $margs $o &default=[])
if (list:empty $p) {
put 22
} else {
put $p[-1]
2020-03-13 14:03:36 +00:00
}
}
2021-12-11 21:09:02 +00:00
fn -complete-names {|| each {|n| put $n'@' } $names }
2020-03-13 14:03:36 +00:00
2021-12-11 21:09:02 +00:00
fn -complete-domains {|name hosts|
each {|h|
put $name@$h
} $hosts
2020-03-13 14:03:36 +00:00
}
2021-12-11 21:09:02 +00:00
fn -complete-remote-dir {|port address dir|
try {
2021-12-11 21:09:02 +00:00
ssh -p $port $address 'ls '$dir | each {|f|
put $address':'$dir$f
}
} except e {
2020-03-13 14:03:36 +00:00
}
}
2021-12-11 21:09:02 +00:00
fn -complete-args {|hosts cmd @argv|
2021-08-27 20:06:49 +00:00
var arg = $argv[-1]
var i = (str:index $arg @)
if (< $i 0) {
-complete-names
all $hosts
if (eq $cmd scp) {
edit:complete-filename $cmd $@argv
2020-03-13 14:03:36 +00:00
}
return
}
2021-08-27 20:06:49 +00:00
var n h = $arg[..$i] $arg[(+ $i 1)..]
if (eq $cmd scp) {
2021-10-13 18:49:48 +00:00
set i = (str:index $h :)
if (>= $i 0) {
2021-08-27 20:06:49 +00:00
var d = $h[(+ $i 1)..]
2021-10-13 18:49:48 +00:00
set h = $h[..$i]
if (list:includes $h $hosts) {
2021-08-27 20:06:49 +00:00
var p = (-port $cmd @argv)
var e = [ (str:split / $d) ]
if (not-eq $e[-1] '') {
2021-10-13 18:49:48 +00:00
set d = (str:join / $e[..-1])
if (eq $d '') {
2021-10-13 18:49:48 +00:00
set d = /
}
}
-complete-remote-dir $p $n@$h $d
2020-03-13 14:03:36 +00:00
}
return
2020-03-13 14:03:36 +00:00
}
}
-complete-domains $n $hosts
2020-03-13 14:03:36 +00:00
}
2021-12-11 21:09:02 +00:00
fn complete {|@argv|
2021-08-27 20:06:49 +00:00
var @hosts = (-kh)
var cmd = $argv[0]
var is-ssh = (eq $cmd ssh)
var po = (condition:cset $is-ssh -p -P)
2020-03-13 14:03:36 +00:00
if (<= (count $argv) 2) {
all (condition:cset $is-ssh $options-ssh $options-scp)
-complete-args $hosts $@argv
2020-03-13 14:03:36 +00:00
} elif (eq $argv[-2] $po) {
put 22
2020-03-13 14:03:36 +00:00
} else {
-complete-args $hosts $@argv
2020-03-13 14:03:36 +00:00
}
}
2021-10-13 18:49:48 +00:00
set edit:completion:arg-completer[scp] = $complete~
set edit:completion:arg-completer[ssh] = $complete~