diff --git a/modules/docker/docker.nu b/modules/docker/docker.nu index 848c4467..6875b1ab 100644 --- a/modules/docker/docker.nu +++ b/modules/docker/docker.nu @@ -7,30 +7,14 @@ 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 --wrapped with-flag [...ns] { + if ($in | is-empty) { [] } else { + [$ns $in] | flatten } } def local_image [name] { - let s = ($name | split row '/') + let s = $name | split row '/' if ($s | length) > 1 { $name } else { @@ -57,20 +41,20 @@ export def container-process-list [-n: string@"nu-complete docker ns"] { ^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}' | lines | each {|x| - let r = ($x | from json) - let t = ($r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z' ) + let r = $x | from json + let t = $r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z' $r | upsert created $t } } else if $cli == 'podman' { ^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":"{{.Command}}", "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.Created}}"}' | lines | each {|x| - let r = ($x | from json) - let t = ($r.created | str substring ..32 | into datetime ) + let r = $x | from json + let t = $r.created | str substring ..32 | into datetime $r | upsert created $t } } else { - ^$cli (spr [-n $n]) ps -a + ^$cli ($n | with-flag -n) ps -a | from ssv | rename id image cmd created status port name } @@ -78,13 +62,13 @@ export def container-process-list [-n: string@"nu-complete docker ns"] { # list images export def image-list [-n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) images + ^$env.docker-cli ($n | with-flag -n) images | from ssv -a | each {|x| - let size = ($x.SIZE | into filesize) - let path = ($x.REPOSITORY | split row '/') - let image = ($path | last) - let repo = ($path | range ..(($path|length) - 2) | str join '/') + let size = $x.SIZE | into filesize + let path = $x.REPOSITORY | split row '/' + let image = $path | last + let repo = $path | range ..(($path|length) - 2) | str join '/' { repo: $repo image: $image @@ -134,7 +118,7 @@ export def container-log-namespace [ctn: string@"nu-complete docker container" -n: string@"nu-complete docker ns" # namespace ] { let l = if $l == 0 { [] } else { [--tail $l] } - ^$env.docker-cli (spr [-n $n]) logs -f $l $ctn + ^$env.docker-cli ($n | with-flag -n) logs -f $l $ctn } # attach container @@ -143,7 +127,7 @@ export def container-attach [ -n: string@"nu-complete docker ns" ...args ] { - let ns = (spr [-n $n]) + let ns = $n | with-flag -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 { @@ -152,23 +136,21 @@ export def container-attach [ } 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 ctn = ( - ^$env.docker-cli ps + let ctn = ^$env.docker-cli ps | from ssv -a | 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) { ^$env.docker-cli exec ($n | get 0) sh -c $"ls -dp ($n | get 1)*" | lines | each {|x| $"($n | get 0):($x)"} } else { - let files = (do -i { + let files = do -i { ls -a $"($p)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} - }) + } $files | append $ctn } } @@ -183,62 +165,62 @@ export def container-copy-file [ # remove container export def container-remove [ctn: string@"nu-complete docker all container" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) container rm -f $ctn + ^$env.docker-cli ($n | with-flag -n) container rm -f $ctn } # inspect export def container-inspect [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) inspect $img + ^$env.docker-cli ($n | with-flag -n) inspect $img } # history export def container-history [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) history --no-trunc $img | from ssv -a + ^$env.docker-cli ($n | with-flag -n) history --no-trunc $img | from ssv -a } # save images export def image-save [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] { - ^$env.docker-cli (spr [-n $n]) save $img + ^$env.docker-cli ($n | with-flag -n) save $img } # load images export def image-load [-n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) load + ^$env.docker-cli ($n | with-flag -n) load } # system prune export def system-prune [-n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) system prune -f + ^$env.docker-cli ($n | with-flag -n) system prune -f } # system prune all export def system-prune-all [-n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) system prune --all --force --volumes + ^$env.docker-cli ($n | with-flag -n) system prune --all --force --volumes } # remove image export def image-remove [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) rmi $img + ^$env.docker-cli ($n | with-flag -n) rmi $img } # add new tag export def image-tag [from: string@"nu-complete docker images" to: string -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) tag $from $to + ^$env.docker-cli ($n | with-flag -n) tag $from $to } # push image export def image-push [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) push $img + ^$env.docker-cli ($n | with-flag -n) push $img } # pull image export def image-pull [img -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) pull $img + ^$env.docker-cli ($n | with-flag -n) pull $img } ### list volume export def volume-list [-n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) volume ls | from ssv -a + ^$env.docker-cli ($n | with-flag -n) volume ls | from ssv -a } def "nu-complete docker volume" [] { @@ -247,17 +229,17 @@ def "nu-complete docker volume" [] { # create volume export def volume-create [name: string -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) volume create + ^$env.docker-cli ($n | with-flag -n) volume create } # inspect volume export def volume-inspect [name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) volume inspect $name + ^$env.docker-cli ($n | with-flag -n) volume inspect $name } # remove volume export def volume-remove [...name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] { - ^$env.docker-cli (spr [-n $n]) volume rm $name + ^$env.docker-cli ($n | with-flag -n) volume rm $name } ### run @@ -275,7 +257,7 @@ def "nu-complete docker run sshkey" [ctx: string, pos: int] { } def "nu-complete docker run proxy" [] { - let hostaddr = (do -i { hostname -I | split row ' ' | get 0 }) + let hostaddr = do -i { hostname -I | split row ' ' | get 0 } [ $"http://($hostaddr):7890" $"http://($hostaddr):" ] } @@ -312,22 +294,22 @@ export def container-create [ img: string@"nu-complete docker images" # image ...cmd # command args ] { - let ns = (spr [-n $namespace]) - let entrypoint = (spr [--entrypoint $entrypoint]) + let ns = $namespace | with-flag -n + let entrypoint = $entrypoint | with-flag --entrypoint let daemon = if $daemon { [-d] } else { [--rm -it] } - let mnt = (spr [-v $mnt]) - let workdir = (spr [-w $workdir]) + let mnt = $mnt | with-flag -v + let workdir = $workdir | with-flag -w let vols = if ($vols|is-empty) { [] } else { $vols | transpose k v | each {|x| [-v $"(host-path $x.k):($x.v)"]} | flatten } let envs = if ($envs|is-empty) { [] } else { $envs | transpose k v | each {|x| [-e $"($x.k)=($x.v)"]} | flatten } let ports = if ($ports|is-empty) { [] } else { $ports | transpose k v | each {|x| [-p $"($x.k):($x.v)"] } | flatten } - 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 privileged = (sprb $privileged [--privileged]) - 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 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 privileged = if $privileged {[--privileged]} 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 ssh = if ($ssh|is-empty) { [] } else { - let sshkey = (cat ([$env.HOME .ssh $ssh] | path join) | split row ' ' | get 1) + let sshkey = cat ([$env.HOME .ssh $ssh] | path join) | split row ' ' | get 1 [-e $"ed25519_($sshuser)=($sshkey)"] } let proxy = if ($proxy|is-empty) { [] } else { @@ -337,13 +319,13 @@ export def container-create [ let c = $"container:($attach)" [--uts $c --ipc $c --pid $c --network $c] } - let cache = (spr [-v $cache]) - let args = ([ + let cache = $cache | with-flag -v + let args = [ $privileged $entrypoint $attach $daemon $ports $envs $ssh $proxy $debug $appimage $netadmin $clip $mnt $vols $workdir $cache - ] | flatten) + ] | flatten let name = $"($img | split row '/' | last | str replace ':' '-')_(date now | format date %m%d%H%M)" if $dry_run { echo $"docker ($ns | str join ' ') run --name ($name) ($args|str join ' ') ($img) ($cmd | flatten)" @@ -358,11 +340,11 @@ def has [name] { } def "nu-complete registry show" [cmd: string, offset: int] { - let new = ($cmd | str ends-with ' ') - let cmd = ($cmd | split row ' ') - let url = (do -i { $cmd | get 2 }) - let reg = (do -i { $cmd | get 3 }) - let tag = (do -i { $cmd | get 4 }) + let new = $cmd | str ends-with ' ' + let cmd = $cmd | split row ' ' + let url = do -i { $cmd | get 2 } + let reg = do -i { $cmd | get 3 } + let tag = do -i { $cmd | get 4 } let auth = if ($env | has 'REGISTRY_TOKEN') { [authorization $"Basic ($env.REGISTRY_TOKEN)"] } else { diff --git a/modules/git/git-v2.nu b/modules/git/git-v2.nu index 64723564..9f2cf38d 100644 --- a/modules/git/git-v2.nu +++ b/modules/git/git-v2.nu @@ -12,29 +12,12 @@ def agree [ ( if $default_not { [no yes] } else { [yes no] } | input list $prompt) in [yes] } -def sprb [flag, args] { - if $flag { - $args - } else { - [] +def --wrapped with-flag [...ns] { + if ($in | is-empty) { [] } else { + [$ns $in] | flatten } } -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 export def gs [] { git status @@ -64,7 +47,7 @@ export def gst [ } else if $show { git stash show --text } else if $all { - git stash --all (sprb $include_untracked [--include-untracked]) + git stash --all (if $include_untracked {[--include-untracked]} else {[]}) } else { git stash } @@ -90,7 +73,7 @@ export def gb [ --delete (-d) --no-merged (-n) ] { - let bs = (git branch | lines | each {|x| $x | str substring 2..}) + let bs = git branch | lines | each {|x| $x | str substring 2..} if ($branch | is-empty) { let d = { local: (git branch | lines) @@ -139,7 +122,7 @@ export def --env gn [ } else { $local } - git clone (sprb $submodule [--recurse-submodules]) $repo $local + git clone (if $submodule {[--recurse-submodules]} else {[]}) $repo $local cd $local } } @@ -170,11 +153,11 @@ export def gp [ git push --force } else { let m = if $merge { [] } else { [--rebase] } - let a = (sprb $autostash [--autostash]) + let a = if $autostash {[--autostash]} else {[]} let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch } let remote = if ($remote|is-empty) { 'origin' } else { $remote } - let lbs = (git branch | lines | each {|x| $x | str substring 2..}) - let rbs = (remote_braches | each {|x| $x.1}) + let lbs = git branch | lines | each {|x| $x | str substring 2..} + let rbs = remote_braches | each {|x| $x.1} let prev = (_git_status).branch if $branch in $rbs { if $branch in $lbs { @@ -200,7 +183,7 @@ export def gp [ } } else { let bmsg = "* remote doesn't have that branch" - let force = (sprb $force [--force]) + let force = if $force {[--force]} else {[]} if $branch in $lbs { print $'($bmsg), set upstream and push' git checkout $branch @@ -238,19 +221,19 @@ export def ga [ --source (-o): string ] { if $delete { - let c = (sprb $cached [--cached]) - let f = (sprb $force [--force]) + let c = if $cached {[--cached]} else {[]} + let f = if $force {[--force]} else {[]} git rm $c $f -r $file } else if $restore { - let o = (spr [--source $source]) - let s = (sprb $staged [--staged]) + let o = $source | with-flag --source + let s = if $staged {[--staged]} else {[]} let file = if ($file | is-empty) { [.] } else { [$file] } git restore $o $s $file } else { - let a = (sprb $all [--all]) - let p = (sprb $patch [--patch]) - let u = (sprb $update [--update]) - let v = (sprb $verbose [--verbose]) + 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 file = if ($file | is-empty) { [.] } else { [$file] } git add $a $p $u $v $file } @@ -264,10 +247,10 @@ export def gc [ --amend (-a) --keep (-k) ] { - let m = (spr [-m $message]) - let a = (sprb $all [--all]) - let n = (sprb $amend [--amend]) - let k = (sprb $keep [--no-edit]) + let m = $message | with-flag -m + let a = if $all {[--all]} else {[]} + let n = if $amend {[--amend]} else {[]} + let k = if $keep {[--no-edit]} else {[]} git commit -v $m $a $n $k } @@ -278,10 +261,10 @@ export def gd [ --word-diff (-w) # word-diff --staged (-s) # staged ] { - let w = (sprb $word_diff [--word-diff]) - let c = (sprb $cached [--cached]) - let s = (sprb $staged [--staged]) - git diff $c $s $w (spr [$file]) + let w = if $word_diff {[--word-diff]} else {[]} + let c = if $cached {[--cached]} else {[]} + let s = if $staged {[--staged]} else {[]} + git diff $c $s $w ($file | with-flag) } # git merge @@ -325,7 +308,7 @@ export def gr [ } else if not ($onto | is-empty) { git rebase --onto $branch } else { - let i = (sprb $interactive [--interactive]) + let i = if $interactive {[--interactive]} else {[]} if ($branch | is-empty) { git rebase $i (git_main_branch) } else { @@ -369,8 +352,8 @@ export def grs [ --hard (-h) --clean (-c) ] { - let h = (sprb $hard [--hard]) - let cm = (spr [$commit]) + let h = if $hard {[--hard]} else {[]} + let cm = $commit | with-flag git reset $h $cm if $clean { git clean -fd @@ -448,9 +431,9 @@ export alias gts = git tag -s export def _git_status [] { # TODO: show-stash - let raw_status = (do -i { git --no-optional-locks status --porcelain=2 --branch | lines }) + let raw_status = do -i { git --no-optional-locks status --porcelain=2 --branch | lines } - let stashes = (do -i { git stash list | lines | length }) + let stashes = do -i { git stash list | lines | length } mut status = { idx_added_staged : 0 @@ -477,7 +460,7 @@ export def _git_status [] { if ($raw_status | is-empty) { return $status } for s in $raw_status { - let r = ($s | split row ' ') + let r = $s | split row ' ' match $r.0 { '#' => { match ($r.1 | str substring 7..) { @@ -551,8 +534,7 @@ export def _git_log_stat [n] { } else if ($it | find -r '[0-9]+ file.+change' | is-empty) { $acc } else { - let x = ( - $it + let x = $it | split row ',' | each {|x| $x | str trim @@ -565,10 +547,9 @@ export def _git_log_stat [n] { } else { $i.col | str substring ..3 } - let num = ($i.num | into int) + let num = $i.num | into int $a | upsert $col $num } - ) $acc | upsert r ($acc.r | append $x) } } @@ -580,12 +561,12 @@ export def _git_log [v num] { let stat = if $v { _git_log_stat $num } else { {} } - let r = (do -i { + let r = do -i { git log --reverse -n $num --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" sha message author email date | each {|x| ($x| upsert date ($x.date | into datetime))} - }) + } if $v { $r | merge $stat } else { @@ -632,7 +613,7 @@ export def remote_braches [] { } def "nu-complete git remote branches" [context: string, offset: int] { - let ctx = ($context | argx parse) + let ctx = $context | argx parse let rb = (remote_braches) if ($ctx._args | length) < 3 { $rb | each {|x| {value: $x.1, description: $x.0} } diff --git a/modules/kubernetes/kubernetes.nu b/modules/kubernetes/kubernetes.nu index 32b211e8..f514d01d 100644 --- a/modules/kubernetes/kubernetes.nu +++ b/modules/kubernetes/kubernetes.nu @@ -1,9 +1,9 @@ use argx.nu export def ensure-cache-by-lines [cache path action] { - let ls = (do -i { open $path | lines | length }) + let ls = do -i { open $path | lines | length } if ($ls | is-empty) { return false } - let lc = (do -i { open $cache | get lines}) + let lc = do -i { open $cache | get lines} if not (($cache | path exists) and (not ($lc | is-empty)) and ($ls == $lc)) { mkdir ($cache | path dirname) { @@ -16,7 +16,7 @@ export def ensure-cache-by-lines [cache path action] { export def normalize-column-names [ ] { let i = $in - let cols = ($i | columns) + let cols = $i | columns mut t = $i for c in $cols { $t = ($t | rename -c {$c: ($c | str downcase | str replace ' ' '_')}) @@ -24,25 +24,9 @@ export def normalize-column-names [ ] { $t } -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 --wrapped with-flag [...ns] { + if ($in | is-empty) { [] } else { + [$ns $in] | flatten } } @@ -106,7 +90,7 @@ export def kgh [ --all (-a) ] { if ($name | is-empty) { - let ns = if $all { [--all] } else { (spr [-n $namespace]) } + let ns = if $all { [--all] } else { $namespace | with-flag -n } helm list $ns --output json | from json | update updated {|x| @@ -116,25 +100,25 @@ export def kgh [ } } else { if $manifest { - helm get manifest $name (spr [-n $namespace]) + helm get manifest $name ($namespace | with-flag -n) } else if $values { - helm get values $name (spr [-n $namespace]) + helm get values $name ($namespace | with-flag -n) } else { - helm get notes $name (spr [-n $namespace]) + helm get notes $name ($namespace | with-flag -n) } } } def "nu-complete helm list" [context: string, offset: int] { - let ctx = ($context | argx parse) + let ctx = $context | argx parse kgh -n $ctx.namespace? | each {|x| {value: $x.name description: $x.updated} } } def "nu-complete helm charts" [context: string, offset: int] { - let ctx = ($context | argx parse) - let path = ($ctx | get _pos.chart) + let ctx = $context | argx parse + let path = $ctx | get _pos.chart let path = if ($path | is-empty) { '.' } else { $path } - let paths = (do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} }) + let paths = do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} } helm repo list | from ssv -a | rename value description | append $paths } @@ -154,12 +138,12 @@ export def kah [ --namespace (-n): string@"nu-complete kube ns" ] { let update = $name in ( - helm list (spr [-n $namespace]) --output json + helm list ($namespace | with-flag -n) --output json | from json | get name ) let act = if $update { [upgrade] } else { [install] } let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] } - helm $act $name $chart -f $valuefile $values (spr [-n $namespace]) + helm $act $name $chart -f $valuefile $values ($namespace | with-flag -n) } # helm diff @@ -173,10 +157,10 @@ export def kdh [ --has-plugin (-h) ] { if $has_plugin { - helm diff $name $chart -f $valuefile (spr [-n $namespace]) + helm diff $name $chart -f $valuefile ($namespace | with-flag -n) } else { let update = $name in ( - helm list (spr [-n $namespace]) --output json + helm list ($namespace | with-flag -n) --output json | from json | get name ) if not $update { @@ -186,7 +170,7 @@ export def kdh [ let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] } let target = $'/tmp/($chart | path basename).($name).out.yaml' - helm template --debug $name $chart -f $valuefile $values (spr [-n $namespace]) | save -f $target + helm template --debug $name $chart -f $valuefile $values ($namespace | with-flag -n) | save -f $target if $ignore_image { do -i { yq -i ea 'del(.spec.template.spec.containers.[].image)' $target } } @@ -199,7 +183,7 @@ export def kdelh [ name: string@"nu-complete helm list" --namespace (-n): string@"nu-complete kube ns" ] { - helm uninstall $name (spr [-n $namespace]) + helm uninstall $name ($namespace | with-flag -n) } # helm template @@ -211,9 +195,9 @@ export def kh [ --app (-a): string='test' ] { let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] } - let target = ($valuefile | split row '.' | range ..-2 | append [out yaml] | str join '.') + let target = $valuefile | split row '.' | range ..-2 | append [out yaml] | str join '.' if (not ($target | path exists)) and (([yes no] | input list $'create ($target)?') in [no]) { return } - helm template --debug $app $chart -f $valuefile $values (spr [-n $namespace]) + helm template --debug $app $chart -f $valuefile $values ($namespace | with-flag -n) | save -f $target } @@ -226,26 +210,26 @@ export def "kube-config" [] { def "nu-complete kube ctx" [] { let k = (kube-config) let cache = $'($env.HOME)/.cache/nu-complete/k8s/($k.path | path basename).json' - let data = (ensure-cache-by-lines $cache $k.path { || - let clusters = ($k.data | get clusters | select name cluster.server) - let data = ($k.data + let data = ensure-cache-by-lines $cache $k.path { || + let clusters = $k.data | get clusters | select name cluster.server + let data = $k.data | get contexts | reduce -f {completion:[], mx_ns: 0, mx_cl: 0} {|x, a| - let ns = (if ($x.context.namespace? | is-empty) { '' } else { $x.context.namespace }) - let max_ns = ($ns | str length) - let cluster = ($"($x.context.user)@($clusters | where name == $x.context.cluster | get cluster_server.0)") - let max_cl = ($cluster | str length) + let ns = if ($x.context.namespace? | is-empty) { '' } else { $x.context.namespace } + let max_ns = $ns | str length + let cluster = $"($x.context.user)@($clusters | where name == $x.context.cluster | get cluster_server.0)" + let max_cl = $cluster | str length $a | upsert mx_ns (if $max_ns > $a.mx_ns { $max_ns } else $a.mx_ns) | 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}} - }) + } $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 ' ') + let ns = $x.ns | fill -a l -w $data.max.ns -c ' ' + let cl = $x.cluster | fill -a l -w $data.max.cluster -c ' ' {value: $x.value, description: $"\t($ns) ($cl)"} } } @@ -320,7 +304,7 @@ def upsert_row [table col mask id value] { export def 'kconf import' [name: string, path: string] { let k = (kube-config) mut d = $k.data - let i = (cat $path | from yaml) + let i = cat $path | from yaml let c = { context: { cluster: $name, @@ -340,9 +324,9 @@ export def 'kconf delete' [name: string@"nu-complete kube ctx"] { export def 'kconf export' [name: string@"nu-complete kube ctx"] { let d = (kube-config).data - let ctx = ($d | get contexts | where name == $name | get 0) - let user = ($d | get users | where name == $ctx.context.user) - let cluster = ($d | get clusters | where name == $ctx.context.cluster) + let ctx = $d | get contexts | where name == $name | get 0 + let user = $d | get users | where name == $ctx.context.user + let cluster = $d | get clusters | where name == $ctx.context.cluster { apiVersion: 'v1', current-context: $ctx.name, @@ -378,23 +362,23 @@ def "nu-complete kube kind" [] { } def "nu-complete kube res" [context: string, offset: int] { - let ctx = ($context | argx parse) - let kind = ($ctx | get _args.1) + let ctx = $context | argx parse + let kind = $ctx | get _args.1 let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] } kubectl get $ns $kind | from ssv -a | get NAME } def "nu-complete kube res via name" [context: string, offset: int] { - let ctx = ($context | argx parse) - let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..))) + let ctx = $context | argx parse + let kind = $env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..)) let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] } kubectl get $ns $kind | from ssv -a | get NAME } def "nu-complete kube jsonpath" [context: string] { - let ctx = ($context | argx parse) - let kind = ($ctx | get _args.1) - let res = ($ctx | get _args.2) + let ctx = $context | argx parse + let kind = $ctx | get _args.1 + let res = $ctx | get _args.2 let path = $ctx.jsonpath? let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] } mut r = [] @@ -405,8 +389,8 @@ def "nu-complete kube jsonpath" [context: string] { $r = [''] } } else if ($path | str starts-with '.') { - let row = ($path | split row '.') - let p = ($row | range ..-2 | str join '.') + let row = $path | split row '.' + let p = $row | range ..-2 | str join '.' if ($p | is-empty) { $r = ( kubectl get $ns -o json $kind $res | from json @@ -414,9 +398,9 @@ def "nu-complete kube jsonpath" [context: string] { | each {|x| $'($p).($x)'} ) } else { - let m = (kubectl get $ns $kind $res $"--output=jsonpath={($p)}" | from json) - let l = ($row | last) - let c = (do -i {$m | get $l}) + let m = kubectl get $ns $kind $res $"--output=jsonpath={($p)}" | from json + let l = $row | last + let c = do -i {$m | get $l} if (not ($c | is-empty)) and ($c | describe | str substring 0..5) == 'table' { $r = (0..(($c | length) - 1) | each {|x| $'($p).($l)[($x)]'}) } else { @@ -448,10 +432,10 @@ export def kg [ } else { [-n $namespace] } - let r = (spr [$r]) - let l = (spr [-l $selector]) + let r = $r | with-flag + let l = $selector | with-flag -l if ($jsonpath | is-empty) { - let wide = (sprb $wide [-o wide]) + let wide = if $wide {[-o wide]} else {[]} if ($verbose) { kubectl get -o json $n $k $r $l | from json | get items | each {|x| @@ -482,7 +466,7 @@ export def kd [ i: string@"nu-complete kube res" --namespace (-n): string@"nu-complete kube ns" ] { - kubectl describe (spr [-n $namespace]) $r $i + kubectl describe ($namespace | with-flag -n) $r $i } # kubectl create @@ -491,7 +475,7 @@ export def kc [ --namespace (-n): string@"nu-complete kube ns" name:string ] { - kubectl create (spr [-n $namespace]) $r $name + kubectl create ($namespace | with-flag -n) $r $name } # kubectl get -o yaml @@ -500,7 +484,7 @@ export def ky [ i: string@"nu-complete kube res" --namespace (-n): string@"nu-complete kube ns" ] { - kubectl get (spr [-n $namespace]) -o yaml $r $i + kubectl get ($namespace | with-flag -n) -o yaml $r $i } # kubectl edit @@ -510,9 +494,9 @@ export def ke [ --namespace (-n): string@"nu-complete kube ns" --selector(-l): string ] { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n let r = if ($selector | is-empty) { $r } else { - let res = (kubectl get $k $n -l $selector | from ssv -a | each {|x| $x.NAME}) + let res = kubectl get $k $n -l $selector | from ssv -a | each {|x| $x.NAME} if ($res | length) == 1 { $res.0 } else if ($res | length) == 0 { @@ -531,7 +515,7 @@ export def kdel [ --namespace (-n): string@"nu-complete kube ns" --force(-f) ] { - kubectl delete (spr [-n $namespace]) (sprb $force [--grace-period=0 --force]) $r $i + kubectl delete ($namespace | with-flag -n) (if $force {[--grace-period=0 --force]} else {[]}) $r $i } @@ -541,29 +525,21 @@ export def kgno [] { | rename name status roles age version internal-ip external-ip os kernel runtime } -def "nu-complete kube pods" [context: string, offset: int] { - let ctx = ($context | argx parse) - let ns = (do -i { $ctx | get namespace }) - let ns = (spr [-n $ns]) - kubectl get $ns pods | from ssv -a | get NAME -} - -def "nu-complete kube pods or deploys" [context: string, offset: int] { - let ctx = ($context | argx parse) - let ns = (do -i { $ctx | get namespace }) - let ns = (spr [-n $ns]) - let pods = (kubectl $ns get pods -o name|lines) - let deploys = (kubectl $ns get deploy -o name|lines) - $pods | append $deploys +def "nu-complete kube deploys and pods" [context: string, offset: int] { + let ctx = $context | argx parse + let ns = $ctx.namespace? | with-flag -n + if ($ctx._pos.pod? | default '' | str ends-with '-') { + kubectl get $ns pods | from ssv -a | get NAME + } else { + kubectl get $ns deployments | from ssv -a | get NAME | each {|x| $"($x)-"} + } } def "nu-complete kube ctns" [context: string, offset: int] { - let ctx = ($context | argx parse) - let ns = (do -i { $ctx | get namespace }) - let ns = (spr [-n $ns]) - let ctn = (do -i { $ctx | get container }) - let ctn = (spr [-c $ctn]) - let pod = ($ctx | get _args.1) + let ctx = $context | argx parse + let ns = $ctx.namespace? | with-flag -n + let ctn = $ctx.container? | with-flag -c + let pod = $ctx | get _args.1 kubectl get $ns pod $pod -o jsonpath={.spec.containers[*].name} | split row ' ' } @@ -610,21 +586,25 @@ export def kdp [ # kubectl attach (exec -it) export def ka [ - pod?: string@"nu-complete kube pods or deploys" + pod?: string@"nu-complete kube deploys and pods" --namespace (-n): string@"nu-complete kube ns" --container(-c): string@"nu-complete kube ctns" --selector(-l): string ...args ] { - let n = (spr [-n $namespace]) - let pod = if ($selector | is-empty) { $pod } else { - let pods = ( - kubectl get pods $n -o wide -l $selector + let n = $namespace | with-flag -n + let pod = if ($selector | is-empty) { + if ($pod | str ends-with '-') { + $"deployment/($pod | str substring ..-1)" + } else { + $pod + } + } else { + let pods = kubectl get pods $n -o wide -l $selector | from ssv -a | where STATUS == Running | select NAME IP NODE | rename name ip node - ) if ($pods | length) == 1 { ($pods.0).name } else if ($pods | length) == 0 { @@ -635,7 +615,7 @@ export def ka [ } let c = if ($container | is-empty) { if ($selector | is-empty) { [] } else { - let cs = (kgp -n $n $pod -p '.spec.containers[*].name' | split row ' ') + let cs = kgp -n $n $pod -p '.spec.containers[*].name' | split row ' ' let ctn = if ($cs | length) == 1 { $cs.0 } else { @@ -651,17 +631,22 @@ export def ka [ # kubectl logs export def kl [ - pod: string@"nu-complete kube pods or deploys" + pod: string@"nu-complete kube deploys and pods" --namespace(-n): string@"nu-complete kube ns" --container(-c): string@"nu-complete kube ctns" --follow(-f) --previous(-p) ] { - let n = (spr [-n $namespace]) - let c = (spr [-c $container]) - let f = (sprb $follow [-f]) - let p = (sprb $previous [-p]) - kubectl logs $n $f $p $pod $c + let n = $namespace | with-flag -n + let c = $container | with-flag -c + let f = if $follow {[-f]} else {[]} + let p = if $previous {[-p]} else {[]} + let trg = if ($pod | str ends-with '-') { + $"deployment/($pod | str substring ..-1)" + } else { + $pod + } + kubectl logs $n $f $p $trg $c } def "nu-complete port forward type" [] { @@ -669,10 +654,10 @@ def "nu-complete port forward type" [] { } def "nu-complete kube port" [context: string, offset: int] { - let ctx = ($context | argx parse) - let kind = ($ctx | get _args.1) + let ctx = $context | argx parse + let kind = $ctx | get _args.1 let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] } - let res = ($ctx | get _args.2) + let res = $ctx | get _args.2 if ($kind | str starts-with 's') { kubectl get $ns svc $res --output=jsonpath="{.spec.ports}" | from json @@ -692,28 +677,26 @@ export def kpf [ --local (-l): string --namespace (-n): string@"nu-complete kube ns" ] { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n let port = if ($local | is-empty) { $port } else { $"($local):($port)" } kubectl port-forward $n $"($res)/($target)" $port } def "nu-complete kube cp" [cmd: string, offset: int] { - let ctx = ($cmd | str substring ..$offset | argx parse) - let p = ($ctx._args | get (($ctx._args | length) - 1)) - let ns = (do -i { $ctx | get namespace }) - let ns = (spr [-n $ns]) - let c = (do -i { $ctx | get container }) - 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 ':') + let ctx = $cmd | str substring ..$offset | argx parse + let p = $ctx._args | get (($ctx._args | length) - 1) + let ns = $ctx.namespace? | with-flag -n + let c = $ctx.container? | with-flag -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) { kubectl exec $ns ($n | get 0) $c -- sh -c $"ls -dp ($n | get 1)*" | lines | each {|x| $"($n | get 0):($x)"} } else { - let files = (do -i { ls -a $"($p)*" + let files = do -i { ls -a $"($p)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} - }) + } $files | append $ctn } } @@ -724,7 +707,7 @@ export def kcp [ --container (-c): string@"nu-complete kube ctns" --namespace (-n): string@"nu-complete kube ns" ] { - kubectl cp (spr [-n $namespace]) $lhs (spr [-c $container]) $rhs + kubectl cp ($namespace | with-flag -n) $lhs ($container | with-flag -c) $rhs } # kubectl get services @@ -783,7 +766,7 @@ export def ksd [ if ($num | into int) > 9 { "too large" } else { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n kubectl scale $n deployments $d --replicas $num } } @@ -798,7 +781,7 @@ export def ksdr [ } else if $num <= 0 { "too small" } else { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n kubectl scale $n deployments $d --replicas 0 kubectl scale $n deployments $d --replicas $num } @@ -815,7 +798,7 @@ export def krhd [ --revision (-v): int dpl: string@"nu-complete kube res via name" ] { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] } kubectl $n rollout history $"deployment/($dpl)" $v } @@ -826,7 +809,7 @@ export def krud [ --revision (-v): int dpl: string@"nu-complete kube res via name" ] { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] } kubectl $n rollout undo $"deployment/($dpl)" $v } @@ -849,7 +832,7 @@ export def ktp [ } } } else { - let n = (spr [-n $namespace]) + let n = $namespace | with-flag -n kubectl top pod $n | from ssv -a | rename name cpu mem | each {|x| { @@ -891,7 +874,8 @@ export def "kclean stucked ns" [ns: string] { export alias "kclean finalizer" = kubectl patch -p '{\"metadata\":{\"finalizers\":null}}' -export alias "kadm renew" = kubeadm alpha certs renew all +export alias "kadm check" = kubeadm certs check-expiration +export alias "kadm renew" = kubeadm certs renew all ### cert-manager export def kgcert [] {