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

standardized parameter naming for --help and fix regex capture (#786)

standardized parameter naming for `--help`
|old|new|
|-|-|
|img|image|
|ctn|container|
|p|file|
|ctx|context|
|ns|namespace|
|k|kind|
|r, i|resource|
|r|pod, service, deployment|
|res|kind|
|svc|service|
|d, dpl|deployment|

change `(?P<name>)` to `(?<name>)`

Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
fj0r 2024-03-12 19:45:50 +08:00 committed by GitHub
parent 7fcbf543db
commit 56fe4b94ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 121 additions and 121 deletions

View file

@ -79,9 +79,9 @@ export def container-list [
# list images # list images
export def image-list [ export def image-list [
-n: string@"nu-complete docker ns" -n: string@"nu-complete docker ns"
img?: string@"nu-complete docker images" image?: string@"nu-complete docker images"
] { ] {
if ($img | is-empty) { if ($image | is-empty) {
^$env.docker-cli ...($n | with-flag -n) images ^$env.docker-cli ...($n | with-flag -n) images
| from ssv -a | from ssv -a
| each {|x| | each {|x|
@ -99,7 +99,7 @@ export def image-list [
} }
} }
} else { } else {
let r = ^$env.docker-cli ...($n | with-flag -n) inspect $img let r = ^$env.docker-cli ...($n | with-flag -n) inspect $image
| from json | from json
| get 0 | get 0
let e = $r.Config.Env? let e = $r.Config.Env?
@ -170,48 +170,48 @@ def "nu-complete docker images" [] {
# container log # container log
export def container-log [ export def container-log [
ctn: string@"nu-complete docker containers" container: string@"nu-complete docker containers"
-l: int = 100 # line -l: int = 100 # line
-n: string@"nu-complete docker ns" # namespace -n: string@"nu-complete docker ns" # namespace
] { ] {
let l = if $l == 0 { [] } else { [--tail $l] } let l = if $l == 0 { [] } else { [--tail $l] }
^$env.docker-cli ...($n | with-flag -n) logs -f ...$l $ctn ^$env.docker-cli ...($n | with-flag -n) logs -f ...$l $container
} }
export def container-log-trunc [ export def container-log-trunc [
ctn: string@"nu-complete docker containers" container: string@"nu-complete docker containers"
-n: string@"nu-complete docker ns" # namespace -n: string@"nu-complete docker ns" # namespace
] { ] {
if $env.docker-cli == 'podman' { if $env.docker-cli == 'podman' {
print -e $'(ansi yellow)podman(ansi dark_gray) isnt supported(ansi reset)' print -e $'(ansi yellow)podman(ansi dark_gray) isnt supported(ansi reset)'
} else { } else {
let f = ^$env.docker-cli ...($n | with-flag -n) inspect --format='{{.LogPath}}' $ctn let f = ^$env.docker-cli ...($n | with-flag -n) inspect --format='{{.LogPath}}' $container
truncate -s 0 $f truncate -s 0 $f
} }
} }
# attach container # attach container
export def --wrapped container-attach [ export def --wrapped container-attach [
ctn: string@"nu-complete docker containers" container: string@"nu-complete docker containers"
-n: string@"nu-complete docker ns" -n: string@"nu-complete docker ns"
...args ...args
] { ] {
let ns = $n | with-flag -n let ns = $n | with-flag -n
if ($args|is-empty) { if ($args|is-empty) {
^$env.docker-cli ...$ns exec -it $ctn /bin/sh -c "[ -e /bin/zsh ] && /bin/zsh || [ -e /bin/bash ] && /bin/bash || /bin/sh" ^$env.docker-cli ...$ns exec -it $container /bin/sh -c "[ -e /bin/zsh ] && /bin/zsh || [ -e /bin/bash ] && /bin/bash || /bin/sh"
} else { } else {
^$env.docker-cli ...$ns exec -it $ctn ...$args ^$env.docker-cli ...$ns exec -it $container ...$args
} }
} }
def "nu-complete docker cp" [cmd: string, offset: int] { def "nu-complete docker cp" [cmd: string, offset: int] {
let argv = $cmd | str substring ..$offset | split row ' ' let argv = $cmd | str substring ..$offset | split row ' '
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 } let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
let ctn = ^$env.docker-cli ps let container = ^$env.docker-cli ps
| from ssv -a | from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $"($x.NAMES):" }} | each {|x| {description: $x.'CONTAINER ID' value: $"($x.NAMES):" }}
let n = $p | split row ':' let n = $p | split row ':'
if $"($n | get 0):" in ($ctn | get value) { if $"($n | get 0):" in ($container | get value) {
^$env.docker-cli exec ($n | get 0) sh -c $"ls -dp ($n | get 1)*" ^$env.docker-cli exec ($n | get 0) sh -c $"ls -dp ($n | get 1)*"
| lines | lines
| each {|x| $"($n | get 0):($x)"} | each {|x| $"($n | get 0):($x)"}
@ -220,7 +220,7 @@ def "nu-complete docker cp" [cmd: string, offset: int] {
ls -a ($"($p)*" | into glob) ls -a ($"($p)*" | into glob)
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
} }
$files | append $ctn $files | append $container
} }
} }
@ -233,20 +233,20 @@ export def container-copy-file [
} }
# remove container # remove container
export def container-remove [ctn: string@"nu-complete docker containers" -n: string@"nu-complete docker ns"] { export def container-remove [container: string@"nu-complete docker containers" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ...($n | with-flag -n) container rm -f $ctn ^$env.docker-cli ...($n | with-flag -n) container rm -f $container
} }
# history # history
export def container-history [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { export def container-history [image: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ...($n | with-flag -n) history --no-trunc $img | from ssv -a ^$env.docker-cli ...($n | with-flag -n) history --no-trunc $image | from ssv -a
} }
# save images # save images
export def image-save [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] { export def image-save [-n: string@"nu-complete docker ns" ...image: string@"nu-complete docker images"] {
^$env.docker-cli ...($n | with-flag -n) save ...$img ^$env.docker-cli ...($n | with-flag -n) save ...$image
} }
# load images # load images
@ -265,8 +265,8 @@ export def system-prune-all [-n: string@"nu-complete docker ns"] {
} }
# remove image # remove image
export def image-remove [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { export def image-remove [image: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ...($n | with-flag -n) rmi $img ^$env.docker-cli ...($n | with-flag -n) rmi $image
} }
# add new tag # add new tag
@ -276,15 +276,15 @@ export def image-tag [from: string@"nu-complete docker images" to: string -n: s
# push image # push image
export def image-push [ export def image-push [
img: string@"nu-complete docker images" image: string@"nu-complete docker images"
--tag(-t): string --tag(-t): string
-n: string@"nu-complete docker ns" -i -n: string@"nu-complete docker ns" -i
] { ] {
let $insecure = if $i {[--insecure-registry]} else {[]} let $insecure = if $i {[--insecure-registry]} else {[]}
if ($tag | is-empty) { if ($tag | is-empty) {
^$env.docker-cli ...($n | with-flag -n) ...$insecure push $img ^$env.docker-cli ...($n | with-flag -n) ...$insecure push $image
} else { } else {
^$env.docker-cli ...($n | with-flag -n) tag $img $tag ^$env.docker-cli ...($n | with-flag -n) tag $image $tag
do -i { do -i {
^$env.docker-cli ...($n | with-flag -n) ...$insecure push $tag ^$env.docker-cli ...($n | with-flag -n) ...$insecure push $tag
} }
@ -293,9 +293,9 @@ export def image-push [
} }
# pull image # pull image
export def image-pull [img -n: string@"nu-complete docker ns" -i] { export def image-pull [image -n: string@"nu-complete docker ns" -i] {
let $insecure = if $i {[--insecure-registry]} else {[]} let $insecure = if $i {[--insecure-registry]} else {[]}
^$env.docker-cli ...($n | with-flag -n) ...$insecure pull $img ^$env.docker-cli ...($n | with-flag -n) ...$insecure pull $image
} }
### list volume ### list volume
@ -371,7 +371,7 @@ export def container-create [
--with-x --with-x
--privileged(-P) --privileged(-P)
--namespace(-n): string@"nu-complete docker ns" --namespace(-n): string@"nu-complete docker ns"
img: string@"nu-complete docker images" # image image: string@"nu-complete docker images" # image
...cmd # command args ...cmd # command args
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
@ -409,11 +409,11 @@ export def container-create [
$debug $appimage $netadmin $with_x $debug $appimage $netadmin $with_x
$mnt $vols $workdir $cache $mnt $vols $workdir $cache
] | flatten ] | flatten
let name = $"($img | split row '/' | last | str replace ':' '-')_(date now | format date %m%d%H%M)" let name = $"($image | split row '/' | last | str replace ':' '-')_(date now | format date %m%d%H%M)"
if $dry_run { if $dry_run {
echo ([docker $ns run --name $name $args $img $cmd] | flatten | str join ' ') echo ([docker $ns run --name $name $args $image $cmd] | flatten | str join ' ')
} else { } else {
^$env.docker-cli ...$ns run --name $name ...$args $img ...($cmd | flatten) ^$env.docker-cli ...$ns run --name $name ...$args $image ...($cmd | flatten)
} }
} }

View file

@ -1,4 +1,4 @@
use ../argx/argx.nu use argx.nu
def agree [ def agree [
prompt prompt
@ -119,7 +119,7 @@ export def gb [
} else if ($branch | is-empty) { } else if ($branch | is-empty) {
let merged = git branch --merged let merged = git branch --merged
| lines | lines
| each { $in | parse -r '\s*\*?\s*(?P<b>[^\s]+)' | get 0.b } | each { $in | parse -r '\s*\*?\s*(?<b>[^\s]+)' | get 0.b }
{ {
local: (git branch) local: (git branch)
remote: (git branch --remote) remote: (git branch --remote)
@ -128,7 +128,7 @@ export def gb [
| each {|x| | each {|x|
$x.v | lines $x.v | lines
| each {|n| | each {|n|
let n = $n | parse -r '\s*(?P<c>\*)?\s*(?P<b>[^\s]+)( -> )?(?P<r>[^\s]+)?' | get 0 let n = $n | parse -r '\s*(?<c>\*)?\s*(?<b>[^\s]+)( -> )?(?<r>[^\s]+)?' | get 0
let c = if ($n.c | is-empty) { null } else { true } let c = if ($n.c | is-empty) { null } else { true }
let r = if ($n.r | is-empty) { null } else { $n.r } let r = if ($n.r | is-empty) { null } else { $n.r }
let m = if $n.b in $merged { true } else { null } let m = if $n.b in $merged { true } else { null }
@ -595,7 +595,7 @@ export def _git_log_stat [n] {
| split row ',' | split row ','
| each {|x| $x | each {|x| $x
| str trim | str trim
| parse -r "(?P<num>[0-9]+) (?P<col>.+)" | parse -r "(?<num>[0-9]+) (?<col>.+)"
| get 0 | get 0
} }
| reduce -f {sha: $acc.c file:0 ins:0 del:0} {|i,a| | reduce -f {sha: $acc.c file:0 ins:0 del:0} {|i,a|

View file

@ -45,38 +45,38 @@ export-env {
# kubectl apply -f # kubectl apply -f
export def kaf [p: path] { export def kaf [file: path] {
kubectl apply -f $p kubectl apply -f $file
} }
# kubectl diff -f # kubectl diff -f
export def kdf [p: path] { export def kdf [file: path] {
kubectl diff -f $p kubectl diff -f $file
} }
# kubectl delete -f # kubectl delete -f
export def kdelf [p: path] { export def kdelf [file: path] {
kubectl delete -f $p kubectl delete -f $file
} }
# kubectl apply -k (kustomize) # kubectl apply -k (kustomize)
export def kak [p: path] { export def kak [file: path] {
kubectl apply -k $p kubectl apply -k $file
} }
# kubectl diff -k (kustomize) # kubectl diff -k (kustomize)
export def kdk [p: path] { export def kdk [file: path] {
kubectl diff -k $p kubectl diff -k $file
} }
# kubectl delete -k (kustomize) # kubectl delete -k (kustomize)
export def kdelk [p: path] { export def kdelk [file: path] {
kubectl delete -k $p kubectl delete -k $file
} }
# kubectl kustomize (template) # kubectl kustomize (template)
export def kk [p: path] { export def kk [file: path] {
kubectl kustomize $p kubectl kustomize $file
} }
### ctx ### ctx
@ -121,20 +121,20 @@ def "nu-complete kube ns" [] {
} }
# kubectl change context # kubectl change context
export def kcc [ctx: string@"nu-complete kube ctx"] { export def kcc [context: string@"nu-complete kube ctx"] {
kubectl config use-context $ctx kubectl config use-context $context
} }
# kubectl (change) namespace # kubectl (change) namespace
export def kn [ns: string@"nu-complete kube ns"] { export def kn [namespace: string@"nu-complete kube ns"] {
if not ($ns in (kubectl get namespace | from ssv -a | get NAME)) { if not ($namespace in (kubectl get namespace | from ssv -a | get NAME)) {
if ([no yes] | input list $"namespace '($ns)' doesn't exist, create it?") in [yes] { if ([no yes] | input list $"namespace '($namespace)' doesn't exist, create it?") in [yes] {
kubectl create namespace $ns kubectl create namespace $namespace
} else { } else {
return return
} }
} }
kubectl config set-context --current $"--namespace=($ns)" kubectl config set-context --current $"--namespace=($namespace)"
} }
def parse_cellpath [path] { def parse_cellpath [path] {
@ -312,8 +312,8 @@ def "nu-complete kube jsonpath" [context: string] {
# kubectl get # kubectl get
export def kg [ export def kg [
k: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
r?: string@"nu-complete kube res" resource?: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--jsonpath (-p): string@"nu-complete kube jsonpath" --jsonpath (-p): string@"nu-complete kube jsonpath"
--selector (-l): string --selector (-l): string
@ -329,12 +329,12 @@ export def kg [
} else { } else {
[-n $namespace] [-n $namespace]
} }
if ($r | is-empty) { if ($resource | is-empty) {
let l = $selector | with-flag -l let l = $selector | with-flag -l
if ($jsonpath | is-empty) { if ($jsonpath | is-empty) {
let wide = if $wide {[-o wide]} else {[]} let wide = if $wide {[-o wide]} else {[]}
if ($verbose) { if ($verbose) {
kubectl get -o json ...$n $k ...$l | from json | get items kubectl get -o json ...$n $kind ...$l | from json | get items
| each {|x| | each {|x|
{ {
name: $x.metadata.name name: $x.metadata.name
@ -348,74 +348,74 @@ export def kg [
} }
| normalize-column-names | normalize-column-names
} else if $watch { } else if $watch {
kubectl get ...$n $k ...$l ...$wide --watch kubectl get ...$n $kind ...$l ...$wide --watch
} else { } else {
kubectl get ...$n $k ...$l ...$wide | from ssv -a | normalize-column-names kubectl get ...$n $kind ...$l ...$wide | from ssv -a | normalize-column-names
} }
} else { } else {
kubectl get ...$n $k $"--output=jsonpath={($jsonpath)}" | from json kubectl get ...$n $kind $"--output=jsonpath={($jsonpath)}" | from json
} }
} else { } else {
kubectl get ...$n $k $r -o json | from json kubectl get ...$n $kind $resource -o json | from json
} }
} }
# kubectl describe # kubectl describe
export def kd [ export def kd [
r: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
i: string@"nu-complete kube res" resource: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
kubectl describe ...($namespace | with-flag -n) $r $i kubectl describe ...($namespace | with-flag -n) $kind $resource
} }
# kubectl create # kubectl create
export def kc [ export def kc [
r: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
name:string name:string
] { ] {
kubectl create ...($namespace | with-flag -n) $r $name kubectl create ...($namespace | with-flag -n) $kind $name
} }
# kubectl get -o yaml # kubectl get -o yaml
export def ky [ export def ky [
r: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
i: string@"nu-complete kube res" resource: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
kubectl get ...($namespace | with-flag -n) -o yaml $r $i kubectl get ...($namespace | with-flag -n) -o yaml $kind $resource
} }
# kubectl edit # kubectl edit
export def ke [ export def ke [
k: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
r?: string@"nu-complete kube res" resource?: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--selector(-l): string --selector(-l): string
] { ] {
let n = $namespace | with-flag -n let n = $namespace | with-flag -n
let r = if ($selector | is-empty) { $r } else { let r = if ($selector | is-empty) { $resource } else {
let res = kubectl get $k ...$n -l $selector | from ssv -a | each {|x| $x.NAME} let res = kubectl get $kind ...$n -l $selector | from ssv -a | each {|x| $x.NAME}
if ($res | length) == 1 { if ($res | length) == 1 {
$res.0 $res.0
} else if ($res | length) == 0 { } else if ($res | length) == 0 {
return return
} else { } else {
$res | input list $'select ($k) ' $res | input list $'select ($kind) '
} }
} }
kubectl edit ...$n $k $r kubectl edit ...$n $kind $r
} }
# kubectl delete # kubectl delete
export def kdel [ export def kdel [
r: string@"nu-complete kube kind" kind: string@"nu-complete kube kind"
i: string@"nu-complete kube res" resource: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--force(-f) --force(-f)
] { ] {
kubectl delete ...($namespace | with-flag -n) ...(if $force {[--grace-period=0 --force]} else {[]}) $r $i kubectl delete ...($namespace | with-flag -n) ...(if $force {[--grace-period=0 --force]} else {[]}) $kind $resource
} }
@ -457,36 +457,36 @@ def "nu-complete kube ctns" [context: string, offset: int] {
# kubectl get pods # kubectl get pods
export def kgp [ export def kgp [
r?: string@"nu-complete kube res via name" pod?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--jsonpath (-p): string@"nu-complete kube jsonpath" --jsonpath (-p): string@"nu-complete kube jsonpath"
--selector (-l): string --selector (-l): string
--all (-a) --all (-a)
] { ] {
if ($r | is-not-empty) { if ($pod | is-not-empty) {
kubectl get pods ...($namespace | with-flag -n) $r --output=json kubectl get pods ...($namespace | with-flag -n) $pod --output=json
| from json | from json
| {...$in.metadata, ...$in.spec, ...$in.status} | {...$in.metadata, ...$in.spec, ...$in.status}
} else if $all { } else if $all {
kg pods -a --wide kg pods -a --wide
} else { } else {
kg pods -n $namespace -p $jsonpath -l $selector --wide $r kg pods -n $namespace -p $jsonpath -l $selector --wide $pod
} }
} }
# kubectl get pods --watch # kubectl get pods --watch
export def kwp [ export def kwp [
r?: string@"nu-complete kube res via name" pod?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--selector (-l): string --selector (-l): string
] { ] {
kg pods -n $namespace -w -l $selector --wide $r kg pods -n $namespace -w -l $selector --wide $pod
} }
# kubectl edit pod # kubectl edit pod
export def kep [ export def kep [
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
pod?: string@"nu-complete kube res via name" pod: string@"nu-complete kube res via name"
--selector (-l): string --selector (-l): string
] { ] {
ke -n $namespace pod -l $selector $pod ke -n $namespace pod -l $selector $pod
@ -589,7 +589,7 @@ def "nu-complete kube port" [context: string, offset: int] {
# kubectl port-forward # kubectl port-forward
export def kpf [ export def kpf [
res: string@"nu-complete port forward type" kind: string@"nu-complete port forward type"
target: string@"nu-complete kube res" target: string@"nu-complete kube res"
port: string@"nu-complete kube port" port: string@"nu-complete kube port"
--local (-l): string --local (-l): string
@ -597,7 +597,7 @@ export def kpf [
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
let port = if ($local | is-empty) { $port } else { $"($local):($port)" } let port = if ($local | is-empty) { $port } else { $"($local):($port)" }
kubectl port-forward ...$ns $"($res)/($target)" $port kubectl port-forward ...$ns $"($kind)/($target)" $port
} }
def "nu-complete kube cp" [cmd: string, offset: int] { def "nu-complete kube cp" [cmd: string, offset: int] {
@ -630,15 +630,15 @@ export def kcp [
# kubectl get services # kubectl get services
export def kgs [ export def kgs [
r?: string@"nu-complete kube res via name" service?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--jsonpath (-p): string@"nu-complete kube jsonpath" --jsonpath (-p): string@"nu-complete kube jsonpath"
--selector (-l): string --selector (-l): string
] { ] {
if ($r | is-empty) { if ($service | is-empty) {
kg services -n $namespace -p $jsonpath -l $selector $r kg services -n $namespace -p $jsonpath -l $selector $service
} else { } else {
kubectl get svc ...($namespace | with-flag -n) $r --output=json kubectl get svc ...($namespace | with-flag -n) $service --output=json
| from json | from json
| {...$in.metadata, ...$in.spec, ...$in.status} | {...$in.metadata, ...$in.spec, ...$in.status}
} }
@ -646,44 +646,44 @@ export def kgs [
# kubectl edit service # kubectl edit service
export def kes [ export def kes [
svc?: string@"nu-complete kube res via name" service?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--selector (-l): string --selector (-l): string
] { ] {
ke -n $namespace service -l $selector $svc ke -n $namespace service -l $selector $service
} }
# kubectl delete service # kubectl delete service
export def kdels [ export def kdels [
svc: string@"nu-complete kube res via name" service: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
kdel -n $namespace service $svc kdel -n $namespace service $service
} }
# kubectl get deployments # kubectl get deployments
export def kgd [ export def kgd [
r?: string@"nu-complete kube res via name" deployment?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--jsonpath (-p): string@"nu-complete kube jsonpath" --jsonpath (-p): string@"nu-complete kube jsonpath"
--selector (-l): string --selector (-l): string
] { ] {
kg -n $namespace deployments -p $jsonpath -l $selector $r kg -n $namespace deployments -p $jsonpath -l $selector $deployment
} }
# kubectl edit deployment # kubectl edit deployment
export def ked [ export def ked [
d?: string@"nu-complete kube res via name" deployment?: string@"nu-complete kube res via name"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--selector (-l): string --selector (-l): string
] { ] {
ke -n $namespace deployments -l $selector $d ke -n $namespace deployments -l $selector $deployment
} }
def "nu-complete num9" [] { [0 1 2 3] } def "nu-complete num9" [] { [0 1 2 3] }
# kubectl scale deployment # kubectl scale deployment
export def ksd [ export def ksd [
d: string@"nu-complete kube deploys" deployment: string@"nu-complete kube deploys"
num: string@"nu-complete num9" num: string@"nu-complete num9"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
@ -691,13 +691,13 @@ export def ksd [
"too large" "too large"
} else { } else {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
kubectl scale ...$ns deployments $d --replicas $num kubectl scale ...$ns deployments $deployment --replicas $num
} }
} }
# kubectl scale deployment with reset # kubectl scale deployment with reset
export def ksdr [ export def ksdr [
d: string@"nu-complete kube deploys" deployment: string@"nu-complete kube deploys"
num: int@"nu-complete num9" num: int@"nu-complete num9"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
@ -707,8 +707,8 @@ export def ksdr [
"too small" "too small"
} else { } else {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
kubectl scale ...$ns deployments $d --replicas 0 kubectl scale ...$ns deployments $deployment --replicas 0
kubectl scale ...$ns deployments $d --replicas $num kubectl scale ...$ns deployments $deployment --replicas $num
} }
} }
@ -723,23 +723,23 @@ def "nu-complete kube kind with image" [] {
# kubectl set image # kubectl set image
export def ksi [ export def ksi [
k: string@"nu-complete kube kind with image" kind: string@"nu-complete kube kind with image"
r: string@"nu-complete kube res" resource: string@"nu-complete kube res"
--namespace(-n): string@"nu-complete kube ns" --namespace(-n): string@"nu-complete kube ns"
act?: any act?: any
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
let path = match $k { let path = match $kind {
_ => '.spec.template.spec.containers[*]' _ => '.spec.template.spec.containers[*]'
} }
let name = kubectl get ...$ns $k $r -o $"jsonpath={($path).name}" | split row ' ' let name = kubectl get ...$ns $kind $resource -o $"jsonpath={($path).name}" | split row ' '
let image = kubectl get ...$ns $k $r -o $"jsonpath={($path).image}" | split row ' ' let image = kubectl get ...$ns $kind $resource -o $"jsonpath={($path).image}" | split row ' '
let list = $name | zip $image | reduce -f {} {|it,acc| $acc | insert $it.0 $it.1 } let list = $name | zip $image | reduce -f {} {|it,acc| $acc | insert $it.0 $it.1 }
if ($act | describe -d).type == 'closure' { if ($act | describe -d).type == 'closure' {
let s = do $act $list let s = do $act $list
if ($s | describe -d).type == 'record' { if ($s | describe -d).type == 'record' {
let s = $s | transpose k v | each {|x| $"($x.k)=($x.v)"} let s = $s | transpose k v | each {|x| $"($x.k)=($x.v)"}
kubectl ...$ns set image $"($k)/($r)" ...$s kubectl ...$ns set image $"($kind)/($resource)" ...$s
} }
} else { } else {
$list $list
@ -748,14 +748,14 @@ export def ksi [
# kubectl redistribution deployment # kubectl redistribution deployment
export def krd [ export def krd [
d: string@"nu-complete kube deploys" deployment: string@"nu-complete kube deploys"
...nodes: string@"nu-complete kube nodes" ...nodes: string@"nu-complete kube nodes"
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
let nums = kubectl get nodes | from ssv -a | length let nums = kubectl get nodes | from ssv -a | length
kubectl scale ...$ns deployments $d --replicas $nums kubectl scale ...$ns deployments $deployment --replicas $nums
let labels = kubectl get ...$ns deploy $d --output=json let labels = kubectl get ...$ns deploy $deployment --output=json
| from json | from json
| get spec.selector.matchLabels | get spec.selector.matchLabels
| transpose k v | transpose k v
@ -765,7 +765,7 @@ export def krd [
for p in ($pods | where NODE not-in $nodes) { for p in ($pods | where NODE not-in $nodes) {
kubectl delete ...$ns pod --grace-period=0 --force $p.NAME kubectl delete ...$ns pod --grace-period=0 --force $p.NAME
} }
kubectl scale ...$ns deployments $d --replicas ($pods | where NODE in $nodes | length) kubectl scale ...$ns deployments $deployment --replicas ($pods | where NODE in $nodes | length)
} }
# kubectl rollout status deployment # kubectl rollout status deployment
@ -777,22 +777,22 @@ export alias kgrs = kubectl get rs
export def krhd [ export def krhd [
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--revision (-v): int --revision (-v): int
dpl: string@"nu-complete kube res via name" deployment: string@"nu-complete kube res via name"
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] } let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] }
kubectl ...$ns rollout history $"deployment/($dpl)" ...$v kubectl ...$ns rollout history $"deployment/($deployment)" ...$v
} }
# kubectl rollout undo # kubectl rollout undo
export def krud [ export def krud [
--namespace (-n): string@"nu-complete kube ns" --namespace (-n): string@"nu-complete kube ns"
--revision (-v): int --revision (-v): int
dpl: string@"nu-complete kube res via name" deployment: string@"nu-complete kube res via name"
] { ] {
let ns = $namespace | with-flag -n let ns = $namespace | with-flag -n
let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] } let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] }
kubectl ...$ns rollout undo $"deployment/($dpl)" ...$v kubectl ...$ns rollout undo $"deployment/($deployment)" ...$v
} }
export alias ksss = kubectl scale statefulset export alias ksss = kubectl scale statefulset
export alias krsss = kubectl rollout status statefulset export alias krsss = kubectl rollout status statefulset

View file

@ -31,7 +31,7 @@ def "nu-complete ssh host" [] {
export def parse-ssh-file [group] { export def parse-ssh-file [group] {
$in $in
| parse -r '(?P<k>Host|HostName|User|Port|IdentityFile)\s+(?P<v>.+)' | parse -r '(?<k>Host|HostName|User|Port|IdentityFile)\s+(?<v>.+)'
| append { k: Host, v: null} | append { k: Host, v: null}
| reduce -f { rst: [], item: {Host: null} } {|it, acc| | reduce -f { rst: [], item: {Host: null} } {|it, acc|
if $it.k == 'Host' { if $it.k == 'Host' {