mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
refactor: introduce spr
and sprb
for spread (#504)
* refactor: introduce `spr` and `sprb` for spread - eliminate `if ($n|is-empty) { [] } else { [-n $n] }` pattern - `gp -u` can omit branch - delete `grb` - improve the premise in the description * commands for helm as `kxh` - add `(kustomize)` to description of kxk - add commands for helm - kgh - kdh - kah - kdelh - kh - add `nu-complete kube port` of `kpf` - add `--local` flag for `kpf` --------- Co-authored-by: agent <agent@nuc>
This commit is contained in:
parent
fe594febea
commit
ebc16108fb
4 changed files with 264 additions and 138 deletions
|
@ -7,6 +7,28 @@ export-env {
|
|||
}
|
||||
}
|
||||
|
||||
def sprb [flag, args] {
|
||||
if $flag {
|
||||
$args
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
def spr [args] {
|
||||
let lst = ($args | last)
|
||||
if ($lst | is-empty) {
|
||||
[]
|
||||
} else {
|
||||
let init = ($args | range ..-2)
|
||||
if ($init | is-empty) {
|
||||
[ $lst ]
|
||||
} else {
|
||||
$init | append $lst
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def "nu-complete docker ns" [] {
|
||||
if $env.docker-cli == 'nerdctl' {
|
||||
^$env.docker-cli namespace list
|
||||
|
@ -37,16 +59,14 @@ export def dp [-n: string@"nu-complete docker ns"] {
|
|||
$r | upsert created $t
|
||||
}
|
||||
} else {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$cli $ns ps -a
|
||||
^$cli (spr [-n $n]) ps -a
|
||||
| from ssv
|
||||
| rename id image cmd created status port name
|
||||
}
|
||||
}
|
||||
|
||||
export def di [-n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns images
|
||||
^$env.docker-cli (spr [-n $n]) images
|
||||
| from ssv -a
|
||||
| rename repo tag id created size
|
||||
| each {|x|
|
||||
|
@ -101,8 +121,7 @@ export def dln [ctn: string@"nu-complete docker container"
|
|||
-n: string@"nu-complete docker ns" # namespace
|
||||
] {
|
||||
let l = if $l == 0 { [] } else { [--tail $l] }
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns logs -f $l $ctn
|
||||
^$env.docker-cli (spr [-n $n]) logs -f $l $ctn
|
||||
}
|
||||
|
||||
export def da [
|
||||
|
@ -110,7 +129,7 @@ export def da [
|
|||
-n: string@"nu-complete docker ns"
|
||||
...args
|
||||
] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let ns = (spr [-n $n])
|
||||
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"
|
||||
} else {
|
||||
|
@ -148,64 +167,52 @@ export def dcp [
|
|||
}
|
||||
|
||||
export def dcr [ctn: string@"nu-complete docker all container" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns container rm -f $ctn
|
||||
^$env.docker-cli (spr [-n $n]) container rm -f $ctn
|
||||
}
|
||||
|
||||
export def dis [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns inspect $img
|
||||
^$env.docker-cli (spr [-n $n]) inspect $img
|
||||
}
|
||||
|
||||
export def dh [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns history --no-trunc $img | from ssv -a
|
||||
^$env.docker-cli (spr [-n $n]) history --no-trunc $img | from ssv -a
|
||||
}
|
||||
|
||||
export def dsv [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns save $img
|
||||
^$env.docker-cli (spr [-n $n]) save $img
|
||||
}
|
||||
|
||||
export def dld [-n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns load
|
||||
^$env.docker-cli (spr [-n $n]) load
|
||||
}
|
||||
|
||||
export def dsp [-n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns system prune -f
|
||||
^$env.docker-cli (spr [-n $n]) system prune -f
|
||||
}
|
||||
|
||||
export def dspall [-n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns system prune --all --force --volumes
|
||||
^$env.docker-cli (spr [-n $n]) system prune --all --force --volumes
|
||||
}
|
||||
|
||||
export def drmi [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns rmi $img
|
||||
^$env.docker-cli (spr [-n $n]) rmi $img
|
||||
}
|
||||
|
||||
export def dt [from: string@"nu-complete docker images" to: string -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns tag $from $to
|
||||
^$env.docker-cli (spr [-n $n]) tag $from $to
|
||||
}
|
||||
|
||||
export def dps [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns push $img
|
||||
^$env.docker-cli (spr [-n $n]) push $img
|
||||
}
|
||||
|
||||
export def dpl [img -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns pull $img
|
||||
^$env.docker-cli (spr [-n $n]) pull $img
|
||||
}
|
||||
|
||||
### volume
|
||||
export def dvl [-n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns volume ls | from ssv -a
|
||||
^$env.docker-cli (spr [-n $n]) volume ls | from ssv -a
|
||||
}
|
||||
|
||||
def "nu-complete docker volume" [] {
|
||||
|
@ -213,18 +220,15 @@ def "nu-complete docker volume" [] {
|
|||
}
|
||||
|
||||
export def dvc [name: string -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns volume create
|
||||
^$env.docker-cli (spr [-n $n]) volume create
|
||||
}
|
||||
|
||||
export def dvi [name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns volume inspect $name
|
||||
^$env.docker-cli (spr [-n $n]) volume inspect $name
|
||||
}
|
||||
|
||||
export def dvr [...name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] {
|
||||
let ns = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
^$env.docker-cli $ns volume rm $name
|
||||
^$env.docker-cli (spr [-n $n]) volume rm $name
|
||||
}
|
||||
|
||||
### run
|
||||
|
@ -277,19 +281,19 @@ export def dr [
|
|||
img: string@"nu-complete docker images" # image
|
||||
...cmd # command args
|
||||
] {
|
||||
let ns = if ($namespace|is-empty) { [] } else { [-n $namespace] }
|
||||
let entrypoint = if ($entrypoint|is-empty) { [] } else { [--entrypoint $entrypoint] }
|
||||
let ns = (spr [-n $namespace])
|
||||
let entrypoint = (spr [--entrypoint $entrypoint])
|
||||
let daemon = if $daemon { [-d] } else { [--rm -it] }
|
||||
let mnt = if ($mnt|is-empty) { [] } else { [-v $mnt] }
|
||||
let workdir = if ($workdir|is-empty) { [] } else { [-w $workdir] }
|
||||
let mnt = (spr [-v $mnt])
|
||||
let workdir = (spr [-w $workdir])
|
||||
let vols = if ($vols|is-empty) { [] } else { $vols | transpose k v | each {|x| $"-v '(host-path $x.k):($x.v)'"} }
|
||||
let envs = if ($envs|is-empty) { [] } else { $envs | transpose k v | each {|x| $"-e ($x.k)=($x.v)"} }
|
||||
let ports = if ($ports|is-empty) { [] } else { $ports | transpose k v | each {|x|[-p $"($x.k):($x.v)"]} | flatten }
|
||||
let debug = if $debug { [--cap-add=SYS_ADMIN --cap-add=SYS_PTRACE --security-opt seccomp=unconfined] } else { [] }
|
||||
#let appimage = if $appimage { [--device /dev/fuse --security-opt apparmor:unconfined] } else { [] }
|
||||
let appimage = if $appimage { [--device /dev/fuse] } else { [] }
|
||||
let netadmin = if $netadmin { [--cap-add=NET_ADMIN --device /dev/net/tun] } else { [] }
|
||||
let clip = if $with_x { [-e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix] } else { [] }
|
||||
let debug = (sprb $debug [--cap-add=SYS_ADMIN --cap-add=SYS_PTRACE --security-opt seccomp=unconfined])
|
||||
#let appimage = (sprb $appimage [--device /dev/fuse --security-opt apparmor:unconfined])
|
||||
let appimage = (sprb $appimage [--device /dev/fuse])
|
||||
let netadmin = (sprb $netadmin [--cap-add=NET_ADMIN --device /dev/net/tun])
|
||||
let clip = (sprb $with_x [-e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix])
|
||||
let ssh = if ($ssh|is-empty) { [] } else {
|
||||
let sshkey = (cat ([$env.HOME .ssh $ssh] | path join) | split row ' ' | get 1)
|
||||
[-e $"ed25519_($sshuser)=($sshkey)"]
|
||||
|
@ -301,7 +305,7 @@ export def dr [
|
|||
let c = $"container:($attach)"
|
||||
[--uts $c --ipc $c --pid $c --network $c]
|
||||
}
|
||||
let cache = if ($cache|is-empty) { [] } else { [-v $cache] }
|
||||
let cache = (spr [-v $cache])
|
||||
let args = ([
|
||||
$entrypoint $attach $daemon
|
||||
$ports $envs $ssh $proxy
|
||||
|
|
|
@ -34,17 +34,18 @@ If there is no branch as an argument, the branch is displayed.
|
|||
### gp
|
||||
Pull, push and other related to remote repositories
|
||||
|
||||
> We assume that the upstream and downstream branches keep the same name and do not operate across branches.
|
||||
|
||||
- `--clone` to clone
|
||||
- `--submodule` submodule update and submodule init (with `--init`)
|
||||
- `--force` push --force (assume `pull --force` doesn't make sense)
|
||||
- `--init` git init
|
||||
- `--override` just used to trigger a github actions event (in fact, webhooks can also be used)
|
||||
- if branch is specified, we assume it is `git fetch`
|
||||
- unless -u is specified: `git push -u`
|
||||
- `--set-upstream` push --set-upstream
|
||||
- if branch is specified, it is `git fetch` (let's assume you don't like pulling from a different branch)
|
||||
- finally, if no branch and above parameters are specified
|
||||
- `git fetch` to update status.
|
||||
- `git pull` or `git push` will be executed according to the current state.
|
||||
- if both `ahead` and `behind` exist, only `pull`
|
||||
- `git pull` to update.
|
||||
- if `ahead`, `git push` will be executed.
|
||||
|
||||
### ga
|
||||
Git add, rm and restore. about files.
|
||||
|
@ -79,3 +80,10 @@ Git remote
|
|||
|
||||
### gbs
|
||||
Git bisect
|
||||
|
||||
## changelog
|
||||
|
||||
#### 2023-05-18
|
||||
- `gp -u` can omit branch
|
||||
- delete `grb`
|
||||
- improve the premise in the description
|
||||
|
|
|
@ -7,6 +7,28 @@ def agree [prompt] {
|
|||
([yes no] | input list $prompt) in [yes]
|
||||
}
|
||||
|
||||
def sprb [flag, args] {
|
||||
if $flag {
|
||||
$args
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
def spr [args] {
|
||||
let lst = ($args | last)
|
||||
if ($lst | is-empty) {
|
||||
[]
|
||||
} else {
|
||||
let init = ($args | range ..-2)
|
||||
if ($init | is-empty) {
|
||||
[ $lst ]
|
||||
} else {
|
||||
$init | append $lst
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# git status and stash
|
||||
export def gs [
|
||||
--apply (-a): bool
|
||||
|
@ -31,8 +53,7 @@ export def gs [
|
|||
} else if $show {
|
||||
git stash show --text
|
||||
} else if $all {
|
||||
let iu = if $include_untracked { [--include-untracked] } else { [] }
|
||||
git stash --all $iu
|
||||
git stash --all (sprb $include_untracked [--include-untracked])
|
||||
} else {
|
||||
git status
|
||||
}
|
||||
|
@ -96,8 +117,7 @@ export def gp [
|
|||
--autostash (-a): bool # git pull --autostash
|
||||
] {
|
||||
if not ($clone | is-empty) {
|
||||
let s = if $submodule { [--recurse-submodules] } else { [] }
|
||||
git clone $s $clone
|
||||
git clone (sprb $submodule [--recurse-submodules]) $clone
|
||||
} else if $submodule {
|
||||
if $init {
|
||||
git submodule init
|
||||
|
@ -114,16 +134,16 @@ export def gp [
|
|||
git add --all
|
||||
git commit -v -a --no-edit --amend
|
||||
git push --force
|
||||
} else if $set_upstream {
|
||||
let remote = if ($remote | is-empty) { 'origin' } else { $remote }
|
||||
let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch }
|
||||
git push -u $remote $branch
|
||||
} else if not ($branch | is-empty) {
|
||||
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
||||
if $set_upstream {
|
||||
git push -u $remote $branch
|
||||
} else {
|
||||
git fetch $remote $branch
|
||||
}
|
||||
git fetch $remote $branch
|
||||
} else {
|
||||
let r = if $rebase { [--rebase] } else { [] }
|
||||
let a = if $autostash { [--autostash] } else { [] }
|
||||
let r = (sprb $rebase [--rebase])
|
||||
let a = (sprb $autostash [--autostash])
|
||||
git pull $r $a -v
|
||||
let s = (_git_status)
|
||||
if $s.ahead > 0 {
|
||||
|
@ -147,18 +167,18 @@ export def ga [
|
|||
--source (-o): string
|
||||
] {
|
||||
if $delete {
|
||||
let c = if $cached { [--cached] } else { [] }
|
||||
let f = if $force { [--force] } else { [] }
|
||||
git rm $c $file
|
||||
let c = (sprb $cached [--cached])
|
||||
let f = (sprb $force [--force])
|
||||
git rm $c $f $file
|
||||
} else if $restore {
|
||||
let o = if ($source | is-empty) { [] } else { [--source $source] }
|
||||
let s = if $staged { [--staged] } else { [] }
|
||||
let o = (spr [--source $source])
|
||||
let s = (sprb $staged [--staged])
|
||||
git restore $o $s $file
|
||||
} else {
|
||||
let a = if $all { [--all] } else { [] }
|
||||
let p = if $patch { [--patch] } else { [] }
|
||||
let u = if $update { [--update] } else { [] }
|
||||
let v = if $verbose { [--verbose] } else { [] }
|
||||
let a = (sprb $all [--all])
|
||||
let p = (sprb $patch [--patch])
|
||||
let u = (sprb $update [--update])
|
||||
let v = (sprb $verbose [--verbose])
|
||||
let file = if ($file | is-empty) { [.] } else { [$file] }
|
||||
git add $a $p $u $v $file
|
||||
}
|
||||
|
@ -172,10 +192,10 @@ export def gc [
|
|||
--amend (-a): bool
|
||||
--keep (-k): bool
|
||||
] {
|
||||
let m = if ($message | is-empty) { [] } else { [-m $message] }
|
||||
let a = if $all { [--all] } else { [] }
|
||||
let n = if $amend { [--amend] } else { [] }
|
||||
let k = if $keep { [--no-edit] } else { [] }
|
||||
let m = (spr [-m $message])
|
||||
let a = (sprb $all [--all])
|
||||
let n = (sprb $amend [--amend])
|
||||
let k = (sprb $keep [--no-edit])
|
||||
git commit -v $m $a $n $k
|
||||
}
|
||||
|
||||
|
@ -185,9 +205,9 @@ export def gd [
|
|||
--word-diff (-w): bool # word-diff
|
||||
--staged (-s): bool # staged
|
||||
] {
|
||||
let w = if $word_diff { [--word-diff] } else { [] }
|
||||
let c = if $cached { [--cached] } else { [] }
|
||||
let s = if $staged { [--staged] } else { [] }
|
||||
let w = (sprb $word_diff [--word-diff])
|
||||
let c = (sprb $cached [--cached])
|
||||
let s = (sprb $staged [--staged])
|
||||
git diff $c $s $w
|
||||
}
|
||||
|
||||
|
@ -254,8 +274,8 @@ export def gr [
|
|||
commit?: string@"nu-complete git log"
|
||||
--hard (-h): bool
|
||||
] {
|
||||
let h = if $hard { [--hard] } else { [] }
|
||||
let c = if ($commit | is-empty) { [] } else { [$commit] }
|
||||
let h = (sprb $hard [--hard])
|
||||
let c = (spr [$commit])
|
||||
git reset $h $c
|
||||
}
|
||||
|
||||
|
@ -323,11 +343,6 @@ export def gsq [] {
|
|||
git gc --prune=now --aggressive
|
||||
}
|
||||
|
||||
export def grb [branch:string@"nu-complete git branches"] {
|
||||
git rebase (gstat).branch $branch
|
||||
}
|
||||
|
||||
|
||||
export alias gcf = git config --list
|
||||
export alias gsw = git switch
|
||||
export alias gswc = git switch -c
|
||||
|
@ -504,6 +519,3 @@ def git_main_branch [] {
|
|||
| str replace 'HEAD .*?[:: ](.+)' '$1'
|
||||
}
|
||||
|
||||
def git_current_branch [] {
|
||||
(gstat).branch
|
||||
}
|
||||
|
|
|
@ -36,6 +36,28 @@ export def ensure-cache-by-lines [cache path action] {
|
|||
(open $cache).payload
|
||||
}
|
||||
|
||||
def sprb [flag, args] {
|
||||
if $flag {
|
||||
$args
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
def spr [args] {
|
||||
let lst = ($args | last)
|
||||
if ($lst | is-empty) {
|
||||
[]
|
||||
} else {
|
||||
let init = ($args | range ..-2)
|
||||
if ($init | is-empty) {
|
||||
[ $lst ]
|
||||
} else {
|
||||
$init | append $lst
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export def `kcache flush` [] {
|
||||
rm -rf ~/.cache/nu-complete/k8s/
|
||||
nu-complete kube ctx
|
||||
|
@ -67,17 +89,17 @@ export def kdelf [p: path] {
|
|||
kubectl delete -f $p
|
||||
}
|
||||
|
||||
# kubectl apply -k
|
||||
# kubectl apply -k (kustomize)
|
||||
export def kak [p: path] {
|
||||
kubectl apply -k $p
|
||||
}
|
||||
|
||||
# kubectl diff -k
|
||||
# kubectl diff -k (kustomize)
|
||||
export def kdk [p: path] {
|
||||
kubectl diff -k $p
|
||||
}
|
||||
|
||||
# kubectl delete -k
|
||||
# kubectl delete -k (kustomize)
|
||||
export def kdelk [p: path] {
|
||||
kubectl delete -k $p
|
||||
}
|
||||
|
@ -87,6 +109,84 @@ export def kk [p: path] {
|
|||
kubectl kustomize $p
|
||||
}
|
||||
|
||||
# helm list and get
|
||||
export def kgh [
|
||||
name?: string@"nu-complete helm list"
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
--manifest (-m): bool
|
||||
--all (-a): bool
|
||||
] {
|
||||
if ($name | is-empty) {
|
||||
let ns = if $all { [--all] } else { (spr [-n $namespace]) }
|
||||
helm list $ns --output json
|
||||
| from json
|
||||
| update updated {|x|
|
||||
$x.updated
|
||||
| str substring ..-4
|
||||
| into datetime -f '%Y-%m-%d %H:%M:%S.%f %z'
|
||||
}
|
||||
} else {
|
||||
if $manifest {
|
||||
helm get manifest $name (spr [-n $namespace])
|
||||
} else {
|
||||
helm get notes $name (spr [-n $namespace])
|
||||
helm get values $name (spr [-n $namespace])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def "nu-complete helm list" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
kgh -n $ctx.-n? | each {|x| {value: $x.name description: $x.updated} }
|
||||
}
|
||||
|
||||
def "nu-complete helm charts" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
}
|
||||
|
||||
# helm install or upgrade via values file
|
||||
export def kah [
|
||||
name: string@"nu-complete helm list"
|
||||
chart: string@"nu-complete helm charts"
|
||||
values: path
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
let update = $name in (
|
||||
helm list (spr [-n $namespace]) --output json
|
||||
| from json | get name
|
||||
)
|
||||
let act = if $update { [upgrade] } else { [install] }
|
||||
helm $act $name $chart -f $values (spr [-n $namespace])
|
||||
}
|
||||
|
||||
# helm install or upgrade via values file
|
||||
export def kdh [
|
||||
name: string@"nu-complete helm list"
|
||||
chart: string@"nu-complete helm charts"
|
||||
values: path
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
helm diff upgrade $name $chart -f $values (spr [-n $namespace])
|
||||
}
|
||||
|
||||
# helm delete
|
||||
export def kdelh [
|
||||
name: string@"nu-complete helm list"
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
helm uninstall $name (spr [-n $namespace])
|
||||
}
|
||||
|
||||
# helm template
|
||||
export def kh [
|
||||
name: string
|
||||
chart: string@"nu-complete helm charts"
|
||||
values: path
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
kubectl template $name $chart -f $values (spr [-n $namespace])
|
||||
}
|
||||
|
||||
### ctx
|
||||
export def "kube-config" [] {
|
||||
let file = if ($env.KUBECONFIG? | is-empty) { $"($env.HOME)/.kube/config" } else { $env.KUBECONFIG }
|
||||
|
@ -267,10 +367,10 @@ export def kg [
|
|||
} else {
|
||||
[-n $namespace]
|
||||
}
|
||||
let r = if ($r | is-empty) { [] } else { [$r] }
|
||||
let l = if ($selector | is-empty) { [] } else { [-l $selector] }
|
||||
let r = (spr [$r])
|
||||
let l = (spr [-l $selector])
|
||||
if ($jsonpath | is-empty) {
|
||||
let wide = if $wide { [-o wide] } else { [] }
|
||||
let wide = (sprb $wide [-o wide])
|
||||
if ($verbose) {
|
||||
kubectl get -o json $n $k $r | from json | get items
|
||||
| each {|x|
|
||||
|
@ -300,8 +400,7 @@ export def kc [
|
|||
-n: string@"nu-complete kube ns"
|
||||
name:string
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
kubectl create $n $r $name
|
||||
kubectl create (spr [-n $n]) $r $name
|
||||
}
|
||||
|
||||
# kubectl get -o yaml
|
||||
|
@ -310,8 +409,7 @@ export def ky [
|
|||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
kubectl get $n -o yaml $r $i
|
||||
kubectl get (spr [-n $n]) -o yaml $r $i
|
||||
}
|
||||
|
||||
# kubectl describe
|
||||
|
@ -320,8 +418,7 @@ export def kd [
|
|||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
kubectl describe $n $r $i
|
||||
kubectl describe (spr [-n $n]) $r $i
|
||||
}
|
||||
|
||||
# kubectl edit
|
||||
|
@ -330,8 +427,7 @@ export def ke [
|
|||
r: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
kubectl edit $n $k $r
|
||||
kubectl edit (spr [-n $n]) $k $r
|
||||
}
|
||||
|
||||
# kubectl delete
|
||||
|
@ -341,9 +437,7 @@ export def kdel [
|
|||
-n: string@"nu-complete kube ns"
|
||||
--force(-f): bool
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let f = if $force { [--grace-period=0 --force] } else { [] }
|
||||
kubectl delete $n $f $r $i
|
||||
kubectl delete (spr [-n $n]) (sprb $force [--grace-period=0 --force]) $r $i
|
||||
}
|
||||
|
||||
|
||||
|
@ -356,16 +450,16 @@ export def kgno [] {
|
|||
def "nu-complete kube pods" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ns = (do -i { $ctx | get '-n' })
|
||||
let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
|
||||
let ns = (spr [-n $ns])
|
||||
kubectl get $ns pods | from ssv -a | get NAME
|
||||
}
|
||||
|
||||
def "nu-complete kube ctns" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ns = (do -i { $ctx | get '-n' })
|
||||
let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
|
||||
let ns = (spr [-n $ns])
|
||||
let ctn = (do -i { $ctx | get '-c' })
|
||||
let ctn = if ($ctn|is-empty) { [] } else { [-c $ctn] }
|
||||
let ctn = (spr [-c $ctn])
|
||||
let pod = ($ctx | get args.1)
|
||||
kubectl get $ns pod $pod -o jsonpath={.spec.containers[*].name} | split row ' '
|
||||
}
|
||||
|
@ -411,8 +505,8 @@ export def ka [
|
|||
--container(-c): string@"nu-complete kube ctns"
|
||||
...args
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let c = if ($container|is-empty) { [] } else { [-c $container] }
|
||||
let n = (spr [-n $n])
|
||||
let c = (spr [-c $container])
|
||||
kubectl exec $n -it $pod $c -- (if ($args|is-empty) { 'bash' } else { $args })
|
||||
}
|
||||
|
||||
|
@ -421,34 +515,44 @@ export def kl [
|
|||
pod: string@"nu-complete kube pods"
|
||||
--namespace(-n): string@"nu-complete kube ns"
|
||||
--container(-c): string@"nu-complete kube ctns"
|
||||
--follow(-f): bool
|
||||
] {
|
||||
let n = if ($namespace|is-empty) { [] } else { [-n $namespace] }
|
||||
let c = if ($container|is-empty) { [] } else { [-c $container] }
|
||||
kubectl logs $n $pod $c
|
||||
}
|
||||
|
||||
# kubectl logs -f
|
||||
export def klf [
|
||||
pod: string@"nu-complete kube pods"
|
||||
--namespace(-n): string@"nu-complete kube ns"
|
||||
--container(-c): string@"nu-complete kube ctns"
|
||||
] {
|
||||
let n = if ($namespace|is-empty) { [] } else { [-n $namespace] }
|
||||
let c = if ($container|is-empty) { [] } else { [-c $container] }
|
||||
kubectl logs $n -f $pod $c
|
||||
let n = (spr [-n $namespace])
|
||||
let c = (spr [-c $container])
|
||||
let f = (sprb $follow [-f])
|
||||
kubectl logs $n $f $pod $c
|
||||
}
|
||||
|
||||
def "nu-complete port forward type" [] {
|
||||
[pod svc]
|
||||
}
|
||||
|
||||
def "nu-complete kube port" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let kind = ($ctx | get args.1)
|
||||
let ns = if ($ctx.-n? | is-empty) { [] } else { [-n $ctx.-n] }
|
||||
let res = ($ctx | get args.2)
|
||||
if ($kind | str starts-with 's') {
|
||||
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
|
||||
| from json
|
||||
| each {|x| {value: $x.port description: $x.name} }
|
||||
} else {
|
||||
kubectl get $ns pods $res --output=jsonpath="{.spec.containers[].ports}"
|
||||
| from json
|
||||
| each {|x| {value: $x.containerPort description: $x.name?} }
|
||||
}
|
||||
}
|
||||
|
||||
# kubectl port-forward
|
||||
export def kpf [
|
||||
res: string@"nu-complete port forward type"
|
||||
target: string@"nu-complete kube res"
|
||||
port: string@"nu-complete kube port"
|
||||
--local (-l): string
|
||||
-n: string@"nu-complete kube ns"
|
||||
port: string ### reflect port num
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
let port = if ($local | is-empty) { $port } else { $"($local):($port)" }
|
||||
kubectl port-forward $n $"($res)/($target)" $port
|
||||
}
|
||||
|
||||
|
@ -456,9 +560,9 @@ def "nu-complete kube cp" [cmd: string, offset: int] {
|
|||
let ctx = ($cmd | str substring ..$offset | parse cmd)
|
||||
let p = ($ctx.args | get (($ctx.args | length) - 1))
|
||||
let ns = (do -i { $ctx | get '-n' })
|
||||
let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
|
||||
let ns = (spr [-n $ns])
|
||||
let c = (do -i { $ctx | get '-c' })
|
||||
let c = if ($c|is-empty) { [] } else { [-c $c] }
|
||||
let c = (spr [-c $c])
|
||||
let ctn = (kubectl get pod $ns | from ssv -a | each {|x| {description: $x.READY value: $"($x.NAME):" }})
|
||||
let n = ($p | split row ':')
|
||||
if $"($n | get 0):" in ($ctn | get value) {
|
||||
|
@ -479,9 +583,7 @@ export def kcp [
|
|||
-c: string@"nu-complete kube ctns"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let c = if ($c|is-empty) { [] } else { [-c $c] }
|
||||
kubectl cp $n $lhs $c $rhs
|
||||
kubectl cp (spr [-n $n]) $lhs (spr [-c $c]) $rhs
|
||||
}
|
||||
|
||||
# kubectl get services
|
||||
|
@ -519,7 +621,7 @@ export def ked [d: string@"nu-complete kube res via name", -n: string@"nu-comple
|
|||
ke -n $n deployments $d
|
||||
}
|
||||
|
||||
def "nu-complete num9" [] { [1 2 3] }
|
||||
def "nu-complete num9" [] { [0 1 2 3] }
|
||||
# kubectl scale deployment
|
||||
export def ksd [
|
||||
d: string@"nu-complete kube res via name"
|
||||
|
@ -529,7 +631,7 @@ export def ksd [
|
|||
if ($num | into int) > 9 {
|
||||
"too large"
|
||||
} else {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
kubectl scale $n deployments $d --replicas $num
|
||||
}
|
||||
}
|
||||
|
@ -544,7 +646,7 @@ export def ksdr [
|
|||
} else if $num <= 0 {
|
||||
"too small"
|
||||
} else {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
kubectl scale $n deployments $d --replicas 0
|
||||
kubectl scale $n deployments $d --replicas $num
|
||||
}
|
||||
|
@ -557,14 +659,14 @@ export alias kgrs = kubectl get rs
|
|||
|
||||
# kubectl rollout history
|
||||
export def krhd [-n: string@"nu-complete kube ns", --revision (-v): int, dpl: string@"nu-complete kube res via name"] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] }
|
||||
kubectl $n rollout history $"deployment/($dpl)" $v
|
||||
}
|
||||
|
||||
# kubectl rollout undo
|
||||
export def krud [-n: string@"nu-complete kube ns", --revision (-v): int, dpl: string@"nu-complete kube res via name"] {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] }
|
||||
kubectl $n rollout undo $"deployment/($dpl)" $v
|
||||
}
|
||||
|
@ -584,7 +686,7 @@ export def ktp [-n: string@"nu-complete kube ns" --all(-a): bool] {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||
let n = (spr [-n $n])
|
||||
kubectl top pod $n | from ssv -a | rename name cpu mem
|
||||
| each {|x|
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue