Utilisation de kak comme éditeur par défaut + réecriture du script de nettoyage du known_hosts

This commit is contained in:
Benjamin VAUDOUR 2022-02-19 09:56:19 +00:00
parent f94229ddd0
commit 4f516d7cbe
2 changed files with 68 additions and 25 deletions

View File

@ -4,32 +4,73 @@ edit:add-var cleankh~ {|@argv|
use framagit.org/benjamin.vaudour/elv-lib/mods/ip
use framagit.org/benjamin.vaudour/elv-lib/mods/list
fn sort-host {|e1 e2|
var i1 = (common:cond (ip:is-ip $e1) 1 0)
var i2 = (common:cond (ip:is-ip $e2) 1 0)
if (!= $i1 $i2) {
- $i1 $i2
} elif (== $i1 0) {
compare $e1 $e2
var hosts = [&]
var ips = [&]
var ids = [&]
var file = $E:HOME/.ssh/known_hosts
fn readfile {
cat $file | each {|l|
if (not (str:has-prefix $l '#')) {
put $l
}
} | eawk {|_ host @rest|
var id = (str:join ' ' $rest)
var ihosts = []
str:split ',' $host | each {|h|
set @ihosts = $@ihosts $h
if (ip:is-ip $h) {
if (not (has-key $ips $h)) {
set ips[$h] = [ $id ]
} else {
ip:comp $e1 $e2
set ips[$h] = [ (all $ips[$h]) $id ]
}
} else {
if (not (has-key $hosts $h)) {
set hosts[$h] = [ $id ]
} else {
set hosts[$h] = [ (all $hosts[$h]) $id ]
}
}
}
}
}
var khosts = [&]
cat ~/.ssh/known_hosts | eawk {|_ hosts @rest|
var key = (str:join ' ' $rest)
if (not (has-key $khosts $key)) {
set khosts[$key] = [&]
fn searchips {|id|
if (has-key $ids $id) {
each {|h|
if (ip:is-ip $h) {
put $h
}
str:split , $hosts | each {|h|
set khosts[$key][$h] = $nil
} $ids[$id]
}
}
var lines = [(keys $khosts | each {|key|
var hosts = (keys $khosts[$key] | list:sort $sort-host~ | str:join ,)
put $hosts $key | str:join ' '
})]
echo (str:join "\n" $lines) > ~/.ssh/known_hosts
fn hashost {|id|
if (has-key $ids $id) {
list:contains {|h| not (ip:is-ip $h) } $ids[$id]
} else {
put $false
}
}
fn formatlines {
keys $hosts | order | each {|h|
order $hosts[$h] | each {|id|
var hid = (str:join ',' [ $h (searchips $id) ])
put $hid $id | str:join ' '
}
}
keys $ips | list:sort $ip:comp~ | each {|ip|
order $ips[$ip] | each {|id|
if (not (hashost $id)) {
put $ip $id | str:join ' '
}
}
}
}
readfile
var @lines = (formatlines)
echo (str:join "\n" $lines) > $file
}

View File

@ -1,4 +1,5 @@
var cmd = $e:nvim~
#var cmd = $e:nvim~
var cmd = $e:kak~
edit:add-var vi~ {|@argv| $cmd $@argv }
edit:add-var vb~ {|@argv| $cmd $@argv ~/.bashrc }
@ -6,5 +7,6 @@ edit:add-var vc~ {|@argv| $cmd $@argv ~/.vimrc }
edit:add-var ve~ {|@argv| $cmd $@argv ~/.config/elvish/rc.elv }
edit:add-var vf~ {|@argv| $cmd $@argv ~/.config/fish/config.fish }
edit:add-var vp~ {|@argv| $cmd $@argv PKGBUILD }
edit:add-var vv~ {|@argv| $cmd -R $@argv }
#edit:add-var vv~ {|@argv| $cmd -R $@argv }
edit:add-var vv~ {|@argv| $cmd -ro $@argv }
edit:add-var vz~ {|@argv| $cmd $@argv ~/.zshrc }