From 4f516d7cbe23dd2f7ad871c0ef61c75205317b92 Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR Date: Sat, 19 Feb 2022 09:56:19 +0000 Subject: [PATCH] =?UTF-8?q?Utilisation=20de=20kak=20comme=20=C3=A9diteur?= =?UTF-8?q?=20par=20d=C3=A9faut=20+=20r=C3=A9ecriture=20du=20script=20de?= =?UTF-8?q?=20nettoyage=20du=20known=5Fhosts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliases/cleankh.elv | 87 +++++++++++++++++++++++++++++++++------------ aliases/vi.elv | 6 ++-- 2 files changed, 68 insertions(+), 25 deletions(-) diff --git a/aliases/cleankh.elv b/aliases/cleankh.elv index e32dc0b..1de810f 100644 --- a/aliases/cleankh.elv +++ b/aliases/cleankh.elv @@ -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 - } else { - ip:comp $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 { + 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] = [&] - } - str:split , $hosts | each {|h| - set khosts[$key][$h] = $nil + fn searchips {|id| + if (has-key $ids $id) { + each {|h| + if (ip:is-ip $h) { + put $h + } + } $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 } diff --git a/aliases/vi.elv b/aliases/vi.elv index f3cb39b..2ff2179 100644 --- a/aliases/vi.elv +++ b/aliases/vi.elv @@ -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 }