mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 07:07: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:
parent
cfdc7b725c
commit
c1e798aad4
3 changed files with 37 additions and 23 deletions
|
@ -22,14 +22,15 @@ export def "parse cmd" [] {
|
||||||
| reject sw
|
| 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 })
|
let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
|
||||||
if ($ts | is-empty) { return false }
|
if ($ts | is-empty) { return false }
|
||||||
let tc = (do -i { ls $index | get 0.modified })
|
let tc = (do -i { ls $cache | get 0.modified })
|
||||||
if not (($index | path exists) and ($ts < $tc)) {
|
if not (($cache | path exists) and ($ts < $tc)) {
|
||||||
mkdir (dirname $index)
|
mkdir (dirname $cache)
|
||||||
do $action
|
do $action | save -f $cache
|
||||||
}
|
}
|
||||||
|
open $cache
|
||||||
}
|
}
|
||||||
|
|
||||||
export-env {
|
export-env {
|
||||||
|
@ -75,7 +76,7 @@ export def "kube-config" [] {
|
||||||
def "nu-complete kube ctx" [] {
|
def "nu-complete kube ctx" [] {
|
||||||
let k = (kube-config)
|
let k = (kube-config)
|
||||||
let cache = $'($env.HOME)/.cache/nu-complete/k8s/(basename $k.path).json'
|
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 clusters = ($k.data | get clusters | select name cluster.server)
|
||||||
let data = ($k.data
|
let data = ($k.data
|
||||||
| get contexts
|
| 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 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})
|
| 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|
|
$data.completion | each {|x|
|
||||||
let ns = ($x.ns | fill -a l -w $data.max.ns -c ' ')
|
let ns = ($x.ns | fill -a l -w $data.max.ns -c ' ')
|
||||||
let cl = ($x.cluster | fill -a l -w $data.max.cluster -c ' ')
|
let cl = ($x.cluster | fill -a l -w $data.max.cluster -c ' ')
|
||||||
|
|
|
@ -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 })
|
let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
|
||||||
if ($ts | is-empty) { return false }
|
if ($ts | is-empty) { return false }
|
||||||
let tc = (do -i { ls $index | get 0.modified })
|
let tc = (do -i { ls $cache | get 0.modified })
|
||||||
if not (($index | path exists) and ($ts < $tc)) {
|
if not (($cache | path exists) and ($ts < $tc)) {
|
||||||
mkdir (dirname $index)
|
mkdir (dirname $cache)
|
||||||
do $action
|
do $action | save -f $cache
|
||||||
}
|
}
|
||||||
|
open $cache
|
||||||
}
|
}
|
||||||
|
|
||||||
export def 'str max-length' [] {
|
export def 'str max-length' [] {
|
||||||
|
@ -49,7 +50,7 @@ def fmt-group [p] {
|
||||||
|
|
||||||
def "ssh-hosts" [] {
|
def "ssh-hosts" [] {
|
||||||
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
|
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
|
||||||
ensure-index $cache ~/.ssh/**/* { ||
|
ensure-cache $cache ~/.ssh/**/* { ||
|
||||||
let data = (ssh-list | each {|x|
|
let data = (ssh-list | each {|x|
|
||||||
let uri = $"($x.User)@($x.HostName):($x.Port)"
|
let uri = $"($x.User)@($x.HostName):($x.Port)"
|
||||||
{
|
{
|
||||||
|
@ -67,10 +68,8 @@ def "ssh-hosts" [] {
|
||||||
identfile: ($data.identfile | str max-length),
|
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" [] {
|
def "nu-complete ssh" [] {
|
||||||
|
|
|
@ -1,11 +1,26 @@
|
||||||
### kubernetes
|
### 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" [] {
|
def "kube ctx" [] {
|
||||||
|
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 {
|
do -i {
|
||||||
kubectl config get-contexts
|
kubectl config get-contexts
|
||||||
| from ssv -a
|
| from ssv -a
|
||||||
| where CURRENT == '*'
|
| where CURRENT == '*'
|
||||||
| get 0
|
| get 0
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def kube_stat [] {
|
def kube_stat [] {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue