1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37: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

@ -22,14 +22,15 @@ export def "parse cmd" [] {
| reject sw
}
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-env {
@ -75,7 +76,7 @@ export def "kube-config" [] {
def "nu-complete kube ctx" [] {
let k = (kube-config)
let cache = $'($env.HOME)/.cache/nu-complete/k8s/(basename $k.path).json'
ensure-index $cache $k.path { ||
let data = (ensure-cache $cache $k.path { ||
let clusters = ($k.data | get clusters | select name cluster.server)
let data = ($k.data
| get contexts
@ -89,10 +90,9 @@ def "nu-complete kube ctx" [] {
| upsert mx_cl (if $max_cl > $a.mx_cl { $max_cl } else $a.mx_cl)
| upsert completion ($a.completion | append {value: $x.name, ns: $ns, cluster: $cluster})
})
{completion: $data.completion, max: {ns: $data.mx_ns, cluster: $data.mx_cl}} | save -f $cache
}
{completion: $data.completion, max: {ns: $data.mx_ns, cluster: $data.mx_cl}}
})
let data = (cat $cache | from json)
$data.completion | each {|x|
let ns = ($x.ns | fill -a l -w $data.max.ns -c ' ')
let cl = ($x.cluster | fill -a l -w $data.max.cluster -c ' ')

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" [] {

View file

@ -1,10 +1,25 @@
### kubernetes
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 $cache | get 0.modified })
if not (($cache | path exists) and ($ts < $tc)) {
mkdir (dirname $cache)
do $action | save -f $cache
}
open $cache
}
def "kube ctx" [] {
do -i {
kubectl config get-contexts
| from ssv -a
| where CURRENT == '*'
| get 0
let cache = $'($env.HOME)/.cache/nu-power/kube.json'
let file = if ($env.KUBECONFIG? | is-empty) { $"($env.HOME)/.kube/config" } else { $env.KUBECONFIG }
ensure-cache $cache $file {
do -i {
kubectl config get-contexts
| from ssv -a
| where CURRENT == '*'
| get 0
}
}
}