mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
parent
0632778754
commit
30d386e363
5 changed files with 190 additions and 152 deletions
|
@ -45,21 +45,28 @@ module utils {
|
|||
$r
|
||||
}
|
||||
|
||||
export def 'os type' [] {
|
||||
let info = cat /etc/os-release
|
||||
| lines
|
||||
| reduce -f {} {|x, acc|
|
||||
let a = $x | split row '='
|
||||
$acc | upsert $a.0 ($a.1| str replace -a '"' '')
|
||||
}
|
||||
if 'ID_LIKE' in $info {
|
||||
if not ($info.ID_LIKE | parse -r '(rhel|fedora|redhat)' | is-empty) {
|
||||
'redhat'
|
||||
} else {
|
||||
$info.ID_LIKE
|
||||
export def distro [] {
|
||||
match $nu.os-info.name {
|
||||
'linux' => {
|
||||
let info = cat /etc/os-release
|
||||
| lines
|
||||
| reduce -f {} {|x, acc|
|
||||
let a = $x | split row '='
|
||||
$acc | upsert $a.0 ($a.1| str replace -a '"' '')
|
||||
}
|
||||
if 'ID_LIKE' in $info {
|
||||
if not ($info.ID_LIKE | parse -r '(rhel|fedora|redhat)' | is-empty) {
|
||||
'redhat'
|
||||
} else {
|
||||
$info.ID_LIKE
|
||||
}
|
||||
} else {
|
||||
$info.ID
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
$nu.os-info.name
|
||||
}
|
||||
} else {
|
||||
$info.ID
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -257,7 +264,7 @@ module run {
|
|||
}
|
||||
}
|
||||
|
||||
export def main [tbl --watch: bool] {
|
||||
export def main [tbl --opt: record] {
|
||||
let n = $in
|
||||
use tree
|
||||
use resolve
|
||||
|
@ -268,7 +275,7 @@ module run {
|
|||
return
|
||||
}
|
||||
let flt = if $_.flt in $n.node { [...$n.filter ...($n.node | get $_.flt)] } else { $n.filter }
|
||||
let wth = if $watch {
|
||||
let wth = if $opt.watch {
|
||||
if $_.wth in $n.node {
|
||||
[...$n.watch ($n.node | get $_.watch)]
|
||||
} else {
|
||||
|
@ -495,7 +502,7 @@ module test {
|
|||
}
|
||||
|
||||
|
||||
export def run [tbl --watch: bool] {
|
||||
export def run [tbl --opt: record] {
|
||||
let argv = $in
|
||||
let _ = $env.comma_index
|
||||
use tree
|
||||
|
@ -540,7 +547,7 @@ module test {
|
|||
}
|
||||
}
|
||||
| tree map $cb $bc
|
||||
if ($watch | default false) {
|
||||
if ($opt.watch? | default false) {
|
||||
use run watches
|
||||
watches {
|
||||
$specs | suit
|
||||
|
@ -552,9 +559,9 @@ module test {
|
|||
}
|
||||
|
||||
module vscode-tasks {
|
||||
export def merge [args tbl --json: bool] {
|
||||
export def merge [args tbl --opt: record] {
|
||||
let c = $args | gen $tbl
|
||||
if $json {
|
||||
if $opt.json {
|
||||
$c | to json
|
||||
} else {
|
||||
$c
|
||||
|
@ -669,10 +676,6 @@ def 'find parent' [] {
|
|||
|
||||
def 'comma file' [] {
|
||||
[
|
||||
{
|
||||
condition: {|_, after| not ($after | path join ',.nu' | path exists)}
|
||||
code: "$env.comma = null"
|
||||
}
|
||||
{
|
||||
condition: {|_, after| $after | path join ',.nu' | path exists}
|
||||
code: "
|
||||
|
@ -753,8 +756,7 @@ export-env {
|
|||
watch_separator: $"(ansi dark_gray)------------------------------(ansi reset)"
|
||||
}
|
||||
}
|
||||
os: (os type)
|
||||
arch: (uname -m)
|
||||
distro: (distro)
|
||||
lg: {$in | lg}
|
||||
batch: {|mod|
|
||||
let o = $in
|
||||
|
@ -850,13 +852,18 @@ def expose [t, a, tbl] {
|
|||
}
|
||||
|
||||
# perform or print
|
||||
export def --wrapped pp [...x --print] {
|
||||
export def --wrapped pp [...x --print --as-str] {
|
||||
if $print or (do -i { $env.comma_index | get $env.comma_index.dry_run } | default false) {
|
||||
use run
|
||||
run dry $x --strip
|
||||
let r = run dry $x --strip
|
||||
if $as_str {
|
||||
$r
|
||||
} else {
|
||||
print -e $"(ansi light_gray)($r)(ansi reset)(char newline)"
|
||||
}
|
||||
} else {
|
||||
use tree spread
|
||||
^$x.0 (spread ($x | range 1..))
|
||||
^$x.0 ...(spread ($x | range 1..))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -884,7 +891,7 @@ export def --wrapped , [
|
|||
if ($args | is-empty) {
|
||||
if $vscode {
|
||||
use vscode-tasks
|
||||
vscode-tasks merge $args (resolve comma) --json $json
|
||||
vscode-tasks merge $args (resolve comma) --opt {json: $json}
|
||||
} else if ([$env.PWD, ',.nu'] | path join | path exists) {
|
||||
^$env.EDITOR ,.nu
|
||||
} else {
|
||||
|
@ -921,7 +928,7 @@ export def --wrapped , [
|
|||
}
|
||||
} else if $test {
|
||||
use test
|
||||
$args | flatten | test run $tbl --watch $watch
|
||||
$args | flatten | test run $tbl --opt {watch: $watch}
|
||||
} else if $expose {
|
||||
expose $args.0 ($args | range 1..) $tbl
|
||||
} else {
|
||||
|
@ -929,7 +936,7 @@ export def --wrapped , [
|
|||
$env.comma_index = ($env.comma_index | upsert $env.comma_index.dry_run true)
|
||||
}
|
||||
use run
|
||||
$args | flatten | run $tbl --watch $watch
|
||||
$args | flatten | run $tbl --opt {watch: $watch}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,13 +31,9 @@ $env.comma = {|_|{
|
|||
$_.a: {, suit dry_run a }
|
||||
$_.x: {|r| ($r | lines | get 0) == $env.PWD }
|
||||
}
|
||||
yes: {
|
||||
$_.a: {, -p suit dry_run a }
|
||||
$_.x: {|r| $r == 'pwd'}
|
||||
}
|
||||
ee: {
|
||||
pp: {
|
||||
$_.a: {
|
||||
pp --print aaa bbbb ccccc dddddd eeeeee [
|
||||
pp --print --as-str aaa bbbb ccccc dddddd eeeeee [
|
||||
ffffff gggggggggg [
|
||||
hhhhhhhhh iiiiiiiiii lllllllll
|
||||
] mmmmmmmmmmmmm nnnnnnnnnnnn
|
||||
|
@ -48,7 +44,7 @@ $env.comma = {|_|{
|
|||
jjjjjjjjjjjjj
|
||||
] oooooooo ppppppppp [qqqqqq [rrrrrr ssssss tttttt] uuuuuu]
|
||||
}
|
||||
$_.x: ( '
|
||||
$_.x: ('
|
||||
aaa bbbb ccccc dddddd eeeeee \
|
||||
ffffff gggggggggg \
|
||||
hhhhhhhhh iiiiiiiiii lllllllll \
|
||||
|
|
|
@ -32,15 +32,17 @@ def "nu-complete docker ns" [] {
|
|||
}
|
||||
|
||||
# list containers
|
||||
export def container-process-list [
|
||||
export def container-list [
|
||||
-n: string@"nu-complete docker ns"
|
||||
container?: string@"nu-complete docker containers"
|
||||
--all(-a)
|
||||
] {
|
||||
let cli = $env.docker-cli
|
||||
if ($container | is-empty) {
|
||||
let fmt = '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}'
|
||||
let fmt = if $cli == 'podman' { $fmt | str replace '{{.Command}}' '"{{.Command}}"' | str replace '{{.CreatedAt}}' '{{.Created}}' } else { $fmt }
|
||||
^$cli ps -a --format $fmt
|
||||
let all = if $all {[-a]} else {[]}
|
||||
^$cli ps ...$all --format $fmt
|
||||
| lines
|
||||
| each {|x|
|
||||
let r = $x | from json
|
||||
|
@ -48,11 +50,11 @@ export def container-process-list [
|
|||
$r | upsert created $t
|
||||
}
|
||||
} else {
|
||||
let r = ^$cli ($n | with-flag -n) inspect $container
|
||||
let r = ^$cli ...($n | with-flag -n) inspect $container
|
||||
| from json
|
||||
| get 0
|
||||
let image = $r.Image
|
||||
let img = ^$cli ($n | with-flag -n) inspect $image
|
||||
let img = ^$cli ...($n | with-flag -n) inspect $image
|
||||
| from json
|
||||
| get 0
|
||||
let imgEnv = $img.Config.Env?
|
||||
|
@ -89,7 +91,7 @@ export def image-list [
|
|||
img?: string@"nu-complete docker images"
|
||||
] {
|
||||
if ($img | is-empty) {
|
||||
^$env.docker-cli ($n | with-flag -n) images
|
||||
^$env.docker-cli ...($n | with-flag -n) images
|
||||
| from ssv -a
|
||||
| each {|x|
|
||||
let size = $x.SIZE | into filesize
|
||||
|
@ -106,7 +108,7 @@ export def image-list [
|
|||
}
|
||||
}
|
||||
} else {
|
||||
let r = ^$env.docker-cli ($n | with-flag -n) inspect $img
|
||||
let r = ^$env.docker-cli ...($n | with-flag -n) inspect $img
|
||||
| from json
|
||||
| get 0
|
||||
let e = $r.Config.Env?
|
||||
|
@ -167,7 +169,7 @@ export def container-log-namespace [ctn: string@"nu-complete docker containers"
|
|||
-n: string@"nu-complete docker ns" # namespace
|
||||
] {
|
||||
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 $ctn
|
||||
}
|
||||
|
||||
# attach container
|
||||
|
@ -178,9 +180,9 @@ export def container-attach [
|
|||
] {
|
||||
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"
|
||||
^$env.docker-cli ...$ns exec -it $ctn /bin/sh -c "[ -e /bin/zsh ] && /bin/zsh || [ -e /bin/bash ] && /bin/bash || /bin/sh"
|
||||
} else {
|
||||
^$env.docker-cli $ns exec -it $ctn $args
|
||||
^$env.docker-cli ...$ns exec -it $ctn ...$args
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -214,61 +216,61 @@ export def container-copy-file [
|
|||
|
||||
# remove container
|
||||
export def container-remove [ctn: string@"nu-complete docker all 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 $ctn
|
||||
}
|
||||
|
||||
|
||||
# history
|
||||
export def container-history [img: 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 $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 ($n | with-flag -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 ($n | with-flag -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 ($n | with-flag -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 ($n | with-flag -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 ($n | with-flag -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 ($n | with-flag -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" -i] {
|
||||
let $insecure = if $i {[--insecure-registry]} else {[]}
|
||||
^$env.docker-cli ($n | with-flag -n) $insecure push $img
|
||||
^$env.docker-cli ...($n | with-flag -n) $insecure push $img
|
||||
}
|
||||
|
||||
# pull image
|
||||
export def image-pull [img -n: string@"nu-complete docker ns" -i] {
|
||||
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 $img
|
||||
}
|
||||
|
||||
### list volume
|
||||
export def volume-list [-n: string@"nu-complete docker ns"] {
|
||||
^$env.docker-cli ($n | with-flag -n) volume ls | from ssv -a
|
||||
^$env.docker-cli ...($n | with-flag -n) volume ls | from ssv -a
|
||||
}
|
||||
|
||||
def "nu-complete docker volume" [] {
|
||||
|
@ -277,17 +279,17 @@ def "nu-complete docker volume" [] {
|
|||
|
||||
# create volume
|
||||
export def volume-create [name: string -n: string@"nu-complete docker ns"] {
|
||||
^$env.docker-cli ($n | with-flag -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 ($n | with-flag -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 ($n | with-flag -n) volume rm $name
|
||||
^$env.docker-cli ...($n | with-flag -n) volume rm $name
|
||||
}
|
||||
|
||||
### run
|
||||
|
@ -382,7 +384,7 @@ export def container-create [
|
|||
echo ([docker $ns run --name $name $args $img $cmd] | flatten | str join ' ')
|
||||
} else {
|
||||
let $img = if $env.docker-cli == 'nerdctl' { local_image $img } else { $img }
|
||||
^$env.docker-cli $ns run --name $name $args $img ($cmd | flatten)
|
||||
^$env.docker-cli ...$ns run --name $name ...$args $img ...($cmd | flatten)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -402,10 +404,10 @@ def "nu-complete registry show" [cmd: string, offset: int] {
|
|||
[]
|
||||
}
|
||||
if ($tag | is-empty) and (not $new) or ($reg | is-empty) {
|
||||
curl -sSL $auth $"($url)/v2/_catalog"
|
||||
curl -sSL ...$auth $"($url)/v2/_catalog"
|
||||
| from json | get repositories
|
||||
} else {
|
||||
curl -sSL $auth $"($url)/v2/($reg)/tags/list"
|
||||
curl -sSL ...$auth $"($url)/v2/($reg)/tags/list"
|
||||
| from json | get tags
|
||||
}
|
||||
}
|
||||
|
@ -422,11 +424,11 @@ export def "registry show" [
|
|||
[]
|
||||
}
|
||||
if ($reg | is-empty) {
|
||||
curl -sSL $auth $"($url)/v2/_catalog" | from json | get repositories
|
||||
curl -sSL ...$auth $"($url)/v2/_catalog" | from json | get repositories
|
||||
} else if ($tag | is-empty) {
|
||||
curl -sSL $auth $"($url)/v2/($reg)/tags/list" | from json | get tags
|
||||
curl -sSL ...$auth $"($url)/v2/($reg)/tags/list" | from json | get tags
|
||||
} else {
|
||||
curl -sSL -H 'Accept: application/vnd.oci.image.manifest.v1+json' $auth $"($url)/v2/($reg)/manifests/($tag)" | from json
|
||||
curl -sSL -H 'Accept: application/vnd.oci.image.manifest.v1+json' ...$auth $"($url)/v2/($reg)/manifests/($tag)" | from json
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -463,7 +465,7 @@ export def "bud rm" [
|
|||
buildah rm $id
|
||||
}
|
||||
|
||||
export alias dp = container-process-list
|
||||
export alias dp = container-list
|
||||
export alias di = image-list
|
||||
export alias dl = container-log
|
||||
export alias dln = container-log-namespace
|
||||
|
|
|
@ -45,7 +45,7 @@ export def gst [
|
|||
} else if $show {
|
||||
git stash show --text
|
||||
} else if $all {
|
||||
git stash --all (if $include_untracked {[--include-untracked]} else {[]})
|
||||
git stash --all ...(if $include_untracked {[--include-untracked]} else {[]})
|
||||
} else {
|
||||
git stash
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ export def --env gn [
|
|||
} else {
|
||||
$local
|
||||
}
|
||||
git clone (if $submodule {[--recurse-submodules]} else {[]}) $repo $local
|
||||
git clone ...(if $submodule {[--recurse-submodules]} else {[]}) $repo $local
|
||||
cd $local
|
||||
}
|
||||
}
|
||||
|
@ -170,14 +170,14 @@ export def gp [
|
|||
print $'* switch to ($branch)'
|
||||
git checkout $branch
|
||||
}
|
||||
git pull $m $a
|
||||
git pull ...$m ...$a
|
||||
}
|
||||
} else {
|
||||
print "* local doesn't have that branch, fetch"
|
||||
git checkout -b $branch
|
||||
git fetch $remote $branch
|
||||
git branch -u $'($remote)/($branch)' $branch
|
||||
git pull $m $a -v
|
||||
git pull ...$m ...$a -v
|
||||
}
|
||||
} else {
|
||||
let bmsg = "* remote doesn't have that branch"
|
||||
|
@ -221,19 +221,19 @@ export def ga [
|
|||
if $delete {
|
||||
let c = if $cached {[--cached]} else {[]}
|
||||
let f = if $force {[--force]} else {[]}
|
||||
git rm $c $f -r $file
|
||||
git rm ...$c ...$f -r ...$file
|
||||
} else if $restore {
|
||||
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
|
||||
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 file = if ($file | is-empty) { [.] } else { [$file] }
|
||||
git add $a $p $u $v $file
|
||||
git add ...$a ...$p ...$u ...$v ...$file
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ export def gc [
|
|||
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
|
||||
git commit -v ...$m ...$a ...$n ...$k
|
||||
}
|
||||
|
||||
# git diff
|
||||
|
@ -262,7 +262,7 @@ export def gd [
|
|||
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 diff ...$c ...$s ...$w ...($file | with-flag)
|
||||
}
|
||||
|
||||
# git merge
|
||||
|
@ -275,9 +275,9 @@ export def gm [
|
|||
] {
|
||||
let x = if $no_squash { [] } else { [--squash] }
|
||||
if ($branch | is-empty) {
|
||||
git merge $x $"origin/(git_main_branch)"
|
||||
git merge ...$x $"origin/(git_main_branch)"
|
||||
} else {
|
||||
git merge $x $branch
|
||||
git merge ...$x $branch
|
||||
}
|
||||
if not $no_squash {
|
||||
git commit -v
|
||||
|
@ -308,9 +308,9 @@ export def gr [
|
|||
} else {
|
||||
let i = if $interactive {[--interactive]} else {[]}
|
||||
if ($branch | is-empty) {
|
||||
git rebase $i (git_main_branch)
|
||||
git rebase ...$i (git_main_branch)
|
||||
} else {
|
||||
git rebase $i $branch
|
||||
git rebase ...$i $branch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -352,7 +352,7 @@ export def grs [
|
|||
] {
|
||||
let h = if $hard {[--hard]} else {[]}
|
||||
let cm = $commit | with-flag
|
||||
git reset $h $cm
|
||||
git reset ...$h ...$cm
|
||||
if $clean {
|
||||
git clean -fd
|
||||
}
|
||||
|
|
|
@ -198,6 +198,25 @@ export def 'kconf import' [name: string, path: string] {
|
|||
}
|
||||
|
||||
export def 'kconf delete' [name: string@"nu-complete kube ctx"] {
|
||||
let kc = (kube-config)
|
||||
let d = $kc.data
|
||||
let ctx = $d | get contexts | where name == $name | get 0
|
||||
let rctx = $d | get contexts | where name != $name
|
||||
let user = if ($ctx.context.user in ($rctx | get context.user)) {
|
||||
$d | get users
|
||||
} else {
|
||||
$d | get users | where name != $ctx.context.user
|
||||
}
|
||||
let cluster = if ($ctx.context.cluster in ($rctx | get context.cluster)) {
|
||||
$d | get clusters
|
||||
} else {
|
||||
$d | get clusters | where name != $ctx.context.cluster
|
||||
}
|
||||
$d
|
||||
| update contexts $rctx
|
||||
| update users $user
|
||||
| update clusters $cluster
|
||||
| to yaml
|
||||
}
|
||||
|
||||
export def 'kconf export' [name: string@"nu-complete kube ctx"] {
|
||||
|
@ -243,14 +262,14 @@ def "nu-complete kube res" [context: string, offset: int] {
|
|||
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
|
||||
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 ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||
kubectl get $ns $kind | from ssv -a | get NAME
|
||||
kubectl get ...$ns $kind | from ssv -a | get NAME
|
||||
}
|
||||
|
||||
def "nu-complete kube jsonpath" [context: string] {
|
||||
|
@ -270,13 +289,13 @@ def "nu-complete kube jsonpath" [context: string] {
|
|||
let row = $path | split row '.'
|
||||
let p = $row | range ..-2 | str join '.'
|
||||
if ($p | is-empty) {
|
||||
$r = ( kubectl get $ns -o json $kind $res
|
||||
$r = ( kubectl get ...$ns -o json $kind $res
|
||||
| from json
|
||||
| columns
|
||||
| each {|x| $'($p).($x)'}
|
||||
)
|
||||
} else {
|
||||
let m = kubectl get $ns $kind $res $"--output=jsonpath={($p)}" | from json
|
||||
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' {
|
||||
|
@ -310,31 +329,34 @@ export def kg [
|
|||
} else {
|
||||
[-n $namespace]
|
||||
}
|
||||
let r = $r | with-flag
|
||||
let l = $selector | with-flag -l
|
||||
if ($jsonpath | is-empty) {
|
||||
let wide = if $wide {[-o wide]} else {[]}
|
||||
if ($verbose) {
|
||||
kubectl get -o json $n $k $r $l | from json | get items
|
||||
| each {|x|
|
||||
{
|
||||
name: $x.metadata.name
|
||||
kind: $x.kind
|
||||
ns: $x.metadata.namespace
|
||||
created: ($x.metadata.creationTimestamp | into datetime)
|
||||
metadata: $x.metadata
|
||||
status: $x.status
|
||||
spec: $x.spec
|
||||
if ($r | is-empty) {
|
||||
let l = $selector | with-flag -l
|
||||
if ($jsonpath | is-empty) {
|
||||
let wide = if $wide {[-o wide]} else {[]}
|
||||
if ($verbose) {
|
||||
kubectl get -o json ...$n $k ...$l | from json | get items
|
||||
| each {|x|
|
||||
{
|
||||
name: $x.metadata.name
|
||||
kind: $x.kind
|
||||
ns: $x.metadata.namespace
|
||||
created: ($x.metadata.creationTimestamp | into datetime)
|
||||
metadata: $x.metadata
|
||||
status: $x.status
|
||||
spec: $x.spec
|
||||
}
|
||||
}
|
||||
| normalize-column-names
|
||||
} else if $watch {
|
||||
kubectl get ...$n $k ...$l ...$wide --watch
|
||||
} else {
|
||||
kubectl get ...$n $k ...$l ...$wide | from ssv -a | normalize-column-names
|
||||
}
|
||||
| normalize-column-names
|
||||
} else if $watch {
|
||||
kubectl get $n $k $r $l $wide --watch
|
||||
} else {
|
||||
kubectl get $n $k $r $l $wide | from ssv -a | normalize-column-names
|
||||
kubectl get ...$n $k $"--output=jsonpath={($jsonpath)}" | from json
|
||||
}
|
||||
} else {
|
||||
kubectl get $n $k $r $"--output=jsonpath={($jsonpath)}" | from json
|
||||
kubectl get ...$n $k $r -o json | from json
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -344,7 +366,7 @@ export def kd [
|
|||
i: string@"nu-complete kube res"
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
kubectl describe ($namespace | with-flag -n) $r $i
|
||||
kubectl describe ...($namespace | with-flag -n) $r $i
|
||||
}
|
||||
|
||||
# kubectl create
|
||||
|
@ -353,7 +375,7 @@ export def kc [
|
|||
--namespace (-n): string@"nu-complete kube ns"
|
||||
name:string
|
||||
] {
|
||||
kubectl create ($namespace | with-flag -n) $r $name
|
||||
kubectl create ...($namespace | with-flag -n) $r $name
|
||||
}
|
||||
|
||||
# kubectl get -o yaml
|
||||
|
@ -362,7 +384,7 @@ export def ky [
|
|||
i: string@"nu-complete kube res"
|
||||
--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 $r $i
|
||||
}
|
||||
|
||||
# kubectl edit
|
||||
|
@ -374,7 +396,7 @@ export def ke [
|
|||
] {
|
||||
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 {
|
||||
|
@ -383,7 +405,7 @@ export def ke [
|
|||
$res | input list $'select ($k) '
|
||||
}
|
||||
}
|
||||
kubectl edit $n $k $r
|
||||
kubectl edit ...$n $k $r
|
||||
}
|
||||
|
||||
# kubectl delete
|
||||
|
@ -393,7 +415,7 @@ export def kdel [
|
|||
--namespace (-n): string@"nu-complete kube ns"
|
||||
--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 {[]}) $r $i
|
||||
}
|
||||
|
||||
|
||||
|
@ -406,19 +428,19 @@ export def kgno [] {
|
|||
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.a? | default false) or ($ctx._pos.pod? | default '' | str ends-with '-') {
|
||||
kubectl get $ns pods | from ssv -a | get NAME
|
||||
let all_pods = ($ctx.a? | default false) or ($ctx.all-pods? | default false)
|
||||
if $all_pods or ($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)-"}
|
||||
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 = $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 ' '
|
||||
kubectl get ...$ns pod $pod -o jsonpath={.spec.containers[*].name} | split row ' '
|
||||
}
|
||||
|
||||
# kubectl get pods
|
||||
|
@ -429,7 +451,11 @@ export def kgp [
|
|||
--selector (-l): string
|
||||
--all (-a)
|
||||
] {
|
||||
if $all {
|
||||
if not ($r | is-empty) {
|
||||
kubectl get pods ...($namespace | with-flag -n) $r --output=json
|
||||
| from json
|
||||
| {...$in.metadata, ...$in.spec, ...$in.status}
|
||||
} else if $all {
|
||||
kg pods -a --wide
|
||||
} else {
|
||||
kg pods -n $namespace -p $jsonpath -l $selector --wide $r
|
||||
|
@ -468,7 +494,7 @@ export def --wrapped ka [
|
|||
--namespace (-n): string@"nu-complete kube ns"
|
||||
--container(-c): string@"nu-complete kube ctns"
|
||||
--selector(-l): string
|
||||
--all-pods(-a)
|
||||
--all-pods(-a) # for completion
|
||||
...args
|
||||
] {
|
||||
let n = $namespace | with-flag -n
|
||||
|
@ -505,7 +531,7 @@ export def --wrapped ka [
|
|||
} else {
|
||||
[-c $container]
|
||||
}
|
||||
kubectl exec $n -it $pod $c -- (if ($args|is-empty) { 'bash' } else { $args })
|
||||
kubectl exec ...$n -it $pod ...$c -- ...(if ($args|is-empty) {['bash']} else { $args })
|
||||
}
|
||||
|
||||
# kubectl logs
|
||||
|
@ -515,7 +541,7 @@ export def kl [
|
|||
--container(-c): string@"nu-complete kube ctns"
|
||||
--follow(-f)
|
||||
--previous(-p)
|
||||
--all-pods(-a)
|
||||
--all-pods(-a) # for completion
|
||||
] {
|
||||
let n = $namespace | with-flag -n
|
||||
let c = $container | with-flag -c
|
||||
|
@ -526,7 +552,7 @@ export def kl [
|
|||
} else {
|
||||
$pod
|
||||
}
|
||||
kubectl logs $n $f $p $trg $c
|
||||
kubectl logs ...$n ...$f ...$p $trg ...$c
|
||||
}
|
||||
|
||||
def "nu-complete port forward type" [] {
|
||||
|
@ -539,11 +565,11 @@ def "nu-complete kube port" [context: string, offset: int] {
|
|||
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||
let res = $ctx | get _args.2
|
||||
if ($kind | str starts-with 's') {
|
||||
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
|
||||
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}"
|
||||
kubectl get ...$ns pods $res --output=jsonpath="{.spec.containers[].ports}"
|
||||
| from json
|
||||
| each {|x| {value: $x.containerPort description: $x.name?} }
|
||||
}
|
||||
|
@ -557,9 +583,9 @@ export def kpf [
|
|||
--local (-l): string
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
let n = $namespace | with-flag -n
|
||||
let ns = $namespace | with-flag -n
|
||||
let port = if ($local | is-empty) { $port } else { $"($local):($port)" }
|
||||
kubectl port-forward $n $"($res)/($target)" $port
|
||||
kubectl port-forward ...$ns $"($res)/($target)" $port
|
||||
}
|
||||
|
||||
def "nu-complete kube cp" [cmd: string, offset: int] {
|
||||
|
@ -567,10 +593,10 @@ def "nu-complete kube cp" [cmd: string, offset: int] {
|
|||
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 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)*"
|
||||
kubectl exec ...$ns ($n | get 0) ...$c -- sh -c $"ls -dp ($n | get 1)*"
|
||||
| lines
|
||||
| each {|x| $"($n | get 0):($x)"}
|
||||
} else {
|
||||
|
@ -587,7 +613,7 @@ export def kcp [
|
|||
--container (-c): string@"nu-complete kube ctns"
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
] {
|
||||
kubectl cp ($namespace | with-flag -n) $lhs ($container | with-flag -c) $rhs
|
||||
kubectl cp ...($namespace | with-flag -n) $lhs ...($container | with-flag -c) $rhs
|
||||
}
|
||||
|
||||
# kubectl get services
|
||||
|
@ -597,7 +623,13 @@ export def kgs [
|
|||
--jsonpath (-p): string@"nu-complete kube jsonpath"
|
||||
--selector (-l): string
|
||||
] {
|
||||
kg services -n $namespace -p $jsonpath -l $selector $r
|
||||
if ($r | is-empty) {
|
||||
kg services -n $namespace -p $jsonpath -l $selector $r
|
||||
} else {
|
||||
kubectl get svc ...($namespace | with-flag -n) $r --output=json
|
||||
| from json
|
||||
| {...$in.metadata, ...$in.spec, ...$in.status}
|
||||
}
|
||||
}
|
||||
|
||||
# kubectl edit service
|
||||
|
@ -646,8 +678,8 @@ export def ksd [
|
|||
if ($num | into int) > 9 {
|
||||
"too large"
|
||||
} else {
|
||||
let n = $namespace | with-flag -n
|
||||
kubectl scale $n deployments $d --replicas $num
|
||||
let ns = $namespace | with-flag -n
|
||||
kubectl scale ...$ns deployments $d --replicas $num
|
||||
}
|
||||
}
|
||||
# kubectl scale deployment with reset
|
||||
|
@ -661,9 +693,9 @@ export def ksdr [
|
|||
} else if $num <= 0 {
|
||||
"too small"
|
||||
} else {
|
||||
let n = $namespace | with-flag -n
|
||||
kubectl scale $n deployments $d --replicas 0
|
||||
kubectl scale $n deployments $d --replicas $num
|
||||
let ns = $namespace | with-flag -n
|
||||
kubectl scale ...$ns deployments $d --replicas 0
|
||||
kubectl scale ...$ns deployments $d --replicas $num
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -678,9 +710,9 @@ export def krhd [
|
|||
--revision (-v): int
|
||||
dpl: string@"nu-complete kube res via name"
|
||||
] {
|
||||
let n = $namespace | with-flag -n
|
||||
let ns = $namespace | with-flag -n
|
||||
let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] }
|
||||
kubectl $n rollout history $"deployment/($dpl)" $v
|
||||
kubectl ...$ns rollout history $"deployment/($dpl)" ...$v
|
||||
}
|
||||
|
||||
# kubectl rollout undo
|
||||
|
@ -689,9 +721,9 @@ export def krud [
|
|||
--revision (-v): int
|
||||
dpl: string@"nu-complete kube res via name"
|
||||
] {
|
||||
let n = $namespace | with-flag -n
|
||||
let ns = $namespace | with-flag -n
|
||||
let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] }
|
||||
kubectl $n rollout undo $"deployment/($dpl)" $v
|
||||
kubectl ...$ns rollout undo $"deployment/($dpl)" ...$v
|
||||
}
|
||||
export alias ksss = kubectl scale statefulset
|
||||
export alias krsss = kubectl rollout status statefulset
|
||||
|
@ -712,8 +744,8 @@ export def ktp [
|
|||
}
|
||||
}
|
||||
} else {
|
||||
let n = $namespace | with-flag -n
|
||||
kubectl top pod $n | from ssv -a | rename name cpu mem
|
||||
let ns = $namespace | with-flag -n
|
||||
kubectl top pod ...$ns | from ssv -a | rename name cpu mem
|
||||
| each {|x|
|
||||
{
|
||||
name: $x.name
|
||||
|
@ -931,7 +963,7 @@ export def kgh [
|
|||
] {
|
||||
if ($name | is-empty) {
|
||||
let ns = if $all { [--all] } else { $namespace | with-flag -n }
|
||||
helm list $ns --output json
|
||||
helm list ...$ns --output json
|
||||
| from json
|
||||
| update updated {|x|
|
||||
$x.updated
|
||||
|
@ -940,11 +972,11 @@ export def kgh [
|
|||
}
|
||||
} else {
|
||||
if $manifest {
|
||||
helm get manifest $name ($namespace | with-flag -n)
|
||||
helm get manifest $name ...($namespace | with-flag -n)
|
||||
} else if $values {
|
||||
helm get values $name ($namespace | with-flag -n)
|
||||
helm get values $name ...($namespace | with-flag -n)
|
||||
} else {
|
||||
helm get notes $name ($namespace | with-flag -n)
|
||||
helm get notes $name ...($namespace | with-flag -n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -976,14 +1008,15 @@ export def kah [
|
|||
valuefile: path
|
||||
--values (-v): any
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
--ignore-image (-i) # for kdh
|
||||
] {
|
||||
let update = $name in (
|
||||
helm list ($namespace | with-flag -n) --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 ($namespace | with-flag -n)
|
||||
helm ...$act $name $chart -f $valuefile ...$values ...($namespace | with-flag -n)
|
||||
}
|
||||
|
||||
# helm diff
|
||||
|
@ -997,10 +1030,10 @@ export def kdh [
|
|||
--has-plugin (-h)
|
||||
] {
|
||||
if $has_plugin {
|
||||
helm diff $name $chart -f $valuefile ($namespace | with-flag -n)
|
||||
helm diff $name $chart -f $valuefile ...($namespace | with-flag -n)
|
||||
} else {
|
||||
let update = $name in (
|
||||
helm list ($namespace | with-flag -n) --output json
|
||||
helm list ...($namespace | with-flag -n) --output json
|
||||
| from json | get name
|
||||
)
|
||||
if not $update {
|
||||
|
@ -1010,7 +1043,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 ($namespace | with-flag -n) | 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 }
|
||||
}
|
||||
|
@ -1037,7 +1070,7 @@ export def kh [
|
|||
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 '.'
|
||||
if (not ($target | path exists)) and (([yes no] | input list $'create ($target)?') in [no]) { return }
|
||||
helm template --debug $app $chart -f $valuefile $values ($namespace | with-flag -n)
|
||||
helm template --debug $app $chart -f $valuefile ...$values ...($namespace | with-flag -n)
|
||||
| save -f $target
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue