1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 22:57:46 +00:00

optimize kube_stat with ensure-cache (#470)

refactor ensure-index to ensure-cache in:
- kubernetes.nu
- ssh.nu

Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
fj0r 2023-04-29 01:34:35 +08:00 committed by GitHub
parent cfdc7b725c
commit c1e798aad4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 23 deletions

View file

@ -1,11 +1,12 @@
export def ensure-index [index path action] {
export def ensure-cache [cache path action] {
let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
if ($ts | is-empty) { return false }
let tc = (do -i { ls $index | get 0.modified })
if not (($index | path exists) and ($ts < $tc)) {
mkdir (dirname $index)
do $action
let tc = (do -i { ls $cache | get 0.modified })
if not (($cache | path exists) and ($ts < $tc)) {
mkdir (dirname $cache)
do $action | save -f $cache
}
open $cache
}
export def 'str max-length' [] {
@ -49,7 +50,7 @@ def fmt-group [p] {
def "ssh-hosts" [] {
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
ensure-index $cache ~/.ssh/**/* { ||
ensure-cache $cache ~/.ssh/**/* { ||
let data = (ssh-list | each {|x|
let uri = $"($x.User)@($x.HostName):($x.Port)"
{
@ -67,10 +68,8 @@ def "ssh-hosts" [] {
identfile: ($data.identfile | str max-length),
}
{max: $max, completion: $data} | save -f $cache
{max: $max, completion: $data}
}
cat $cache | from json
}
def "nu-complete ssh" [] {