mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 15:17:47 +00:00
update docker, git-v2, kubernetes (#517)
- fix `dr -v` - `gs` split into `gs` and `gst` (stash) - `gb` add `remote` parameter - `gn` for clone, init - tweak `gp`, more complete condition of branches - `gc` message as positional parameters - `gm` split into `gm` and `gr` (rebase) - `gm` default squash with commit - `gr`(reset) rename to `grs` - `gr -c` remove `-x` - `grmt` rename to `grm` - `gsq` rename to `ggc` - `kah` allow `--set-json` - `kh` using conventions instead of arguments - `kconf import` update existing key - `ka` (attach) support selector and interactive Co-authored-by: agent <agent@nuc>
This commit is contained in:
parent
80e9d07d80
commit
4a1b4c73c2
4 changed files with 301 additions and 126 deletions
|
@ -68,18 +68,17 @@ export def dp [-n: string@"nu-complete docker ns"] {
|
||||||
export def di [-n: string@"nu-complete docker ns"] {
|
export def di [-n: string@"nu-complete docker ns"] {
|
||||||
^$env.docker-cli (spr [-n $n]) images
|
^$env.docker-cli (spr [-n $n]) images
|
||||||
| from ssv -a
|
| from ssv -a
|
||||||
| rename repo tag id created size
|
|
||||||
| each {|x|
|
| each {|x|
|
||||||
let size = ($x.size | into filesize)
|
let size = ($x.SIZE | into filesize)
|
||||||
let path = ($x.repo | split row '/')
|
let path = ($x.REPOSITORY | split row '/')
|
||||||
let image = ($path | last)
|
let image = ($path | last)
|
||||||
let repo = ($path | range ..(($path|length) - 2) | str join '/')
|
let repo = ($path | range ..(($path|length) - 2) | str join '/')
|
||||||
{
|
{
|
||||||
repo: $repo
|
repo: $repo
|
||||||
image: $image
|
image: $image
|
||||||
tag: $x.tag
|
tag: $x.TAG
|
||||||
id: $x.id
|
id: $x.'IMAGE ID'
|
||||||
created: $x.created
|
created: $x.CREATED
|
||||||
size: $size
|
size: $size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,9 +285,9 @@ export def dr [
|
||||||
let daemon = if $daemon { [-d] } else { [--rm -it] }
|
let daemon = if $daemon { [-d] } else { [--rm -it] }
|
||||||
let mnt = (spr [-v $mnt])
|
let mnt = (spr [-v $mnt])
|
||||||
let workdir = (spr [-w $workdir])
|
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 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)"} }
|
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 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 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 --security-opt apparmor:unconfined])
|
||||||
let appimage = (sprb $appimage [--device /dev/fuse])
|
let appimage = (sprb $appimage [--device /dev/fuse])
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
def agree [prompt] {
|
def agree [
|
||||||
|
prompt
|
||||||
|
--default-not (-n): bool
|
||||||
|
] {
|
||||||
let prompt = if ($prompt | str ends-with '!') {
|
let prompt = if ($prompt | str ends-with '!') {
|
||||||
$'(ansi red)($prompt)(ansi reset)'
|
$'(ansi red)($prompt)(ansi reset)'
|
||||||
} else {
|
} else {
|
||||||
$'($prompt)'
|
$'($prompt)'
|
||||||
}
|
}
|
||||||
([yes no] | input list $prompt) in [yes]
|
( if $default_not { [no yes] } else { [yes no] } | input list $prompt) in [yes]
|
||||||
}
|
}
|
||||||
|
|
||||||
def sprb [flag, args] {
|
def sprb [flag, args] {
|
||||||
|
@ -29,8 +32,13 @@ def spr [args] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# git status and stash
|
# git status
|
||||||
export def gs [
|
export def gs [] {
|
||||||
|
git status
|
||||||
|
}
|
||||||
|
|
||||||
|
# git stash
|
||||||
|
export def gst [
|
||||||
--apply (-a): bool
|
--apply (-a): bool
|
||||||
--clear (-c): bool
|
--clear (-c): bool
|
||||||
--drop (-d): bool
|
--drop (-d): bool
|
||||||
|
@ -55,7 +63,7 @@ export def gs [
|
||||||
} else if $all {
|
} else if $all {
|
||||||
git stash --all (sprb $include_untracked [--include-untracked])
|
git stash --all (sprb $include_untracked [--include-untracked])
|
||||||
} else {
|
} else {
|
||||||
git status
|
git stash
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,27 +83,28 @@ export def gl [
|
||||||
# git branch
|
# git branch
|
||||||
export def gb [
|
export def gb [
|
||||||
branch?: string@"nu-complete git branches"
|
branch?: string@"nu-complete git branches"
|
||||||
|
remote?: string@"nu-complete git remote branches"
|
||||||
--delete (-d): bool
|
--delete (-d): bool
|
||||||
--remote (-r): bool
|
|
||||||
--no-merged (-n): bool
|
--no-merged (-n): bool
|
||||||
] {
|
] {
|
||||||
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) {
|
if ($branch | is-empty) {
|
||||||
if $remote {
|
let d = {
|
||||||
git branch --remote
|
local: (git branch | lines)
|
||||||
} else if $no_merged {
|
remote: (git branch --remote | lines)
|
||||||
git branch --no-merged
|
no-merged: (git branch --no-merged | lines)
|
||||||
} else {
|
|
||||||
git branch
|
|
||||||
}
|
}
|
||||||
} else if $branch in $bs {
|
print ($d | table -n 1 -e)
|
||||||
if $delete {
|
} else if $delete {
|
||||||
if (agree 'branch will be delete!') {
|
if $branch in $bs and (agree 'branch will be delete!') {
|
||||||
git branch -D $branch
|
git branch -D $branch
|
||||||
}
|
}
|
||||||
} else {
|
if $branch in (remote_braches | each {|x| $x.1}) and (agree -n 'delete remote branch?!') {
|
||||||
git checkout $branch
|
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
||||||
|
git push $remote -d $branch
|
||||||
}
|
}
|
||||||
|
} else if $branch in $bs {
|
||||||
|
git checkout $branch
|
||||||
} else {
|
} else {
|
||||||
if (agree 'create new branch?') {
|
if (agree 'create new branch?') {
|
||||||
git checkout -b $branch
|
git checkout -b $branch
|
||||||
|
@ -103,50 +112,93 @@ export def gb [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# git pull, push and switch
|
# git clone, init
|
||||||
export def gp [
|
export def-env gn [
|
||||||
branch?: string@"nu-complete git branches"
|
repo?: string@"nu-complete git branches"
|
||||||
remote?: string@"nu-complete git remotes"
|
local?: path
|
||||||
--force (-f): bool # git push -f
|
|
||||||
--set-upstream (-u): bool # git push -u
|
|
||||||
--override: bool
|
|
||||||
--clone (-c): string # git clone
|
|
||||||
--submodule (-s): bool # git submodule
|
--submodule (-s): bool # git submodule
|
||||||
--init (-i): bool # git init
|
--init (-i): bool # git init
|
||||||
--rebase (-r): bool # git pull --rebase
|
] {
|
||||||
|
if $init {
|
||||||
|
git init $repo
|
||||||
|
cd $repo
|
||||||
|
if $submodule {
|
||||||
|
git submodule init
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let local = if ($local | is-empty) {
|
||||||
|
$repo | path basename | split row '.' | get 0
|
||||||
|
} else {
|
||||||
|
$local
|
||||||
|
}
|
||||||
|
git clone (sprb $submodule [--recurse-submodules]) $repo $local
|
||||||
|
cd $local
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# git pull, push and switch
|
||||||
|
export def gp [
|
||||||
|
branch?: string@"nu-complete git remote branches"
|
||||||
|
remote?: string@"nu-complete git remote branches"
|
||||||
|
--force (-f): bool # git push -f
|
||||||
|
--override: bool
|
||||||
|
--submodule (-s): bool # git submodule
|
||||||
|
--init (-i): bool # git init
|
||||||
|
--merge (-m): bool # git pull (no)--rebase
|
||||||
--autostash (-a): bool # git pull --autostash
|
--autostash (-a): bool # git pull --autostash
|
||||||
] {
|
] {
|
||||||
if not ($clone | is-empty) {
|
if $submodule {
|
||||||
git clone (sprb $submodule [--recurse-submodules]) $clone
|
|
||||||
} else if $submodule {
|
|
||||||
if $init {
|
|
||||||
git submodule init
|
|
||||||
} else {
|
|
||||||
git submodule update
|
git submodule update
|
||||||
}
|
|
||||||
} else if $init {
|
|
||||||
let repo = $branch
|
|
||||||
git init $repo
|
|
||||||
} else if $force {
|
|
||||||
git push --force
|
|
||||||
} else if $override {
|
} else if $override {
|
||||||
git pull
|
git pull --rebase
|
||||||
git add --all
|
git add --all
|
||||||
git commit -v -a --no-edit --amend
|
git commit -v -a --no-edit --amend
|
||||||
git push --force
|
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 }
|
|
||||||
git fetch $remote $branch
|
|
||||||
} else {
|
} else {
|
||||||
let r = (sprb $rebase [--rebase])
|
let m = if $merge { [] } else { [--rebase] }
|
||||||
let a = (sprb $autostash [--autostash])
|
let a = (sprb $autostash [--autostash])
|
||||||
git pull $r $a -v
|
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})
|
||||||
|
if $branch in $rbs {
|
||||||
|
if $branch in $lbs {
|
||||||
|
let bmsg = '* both local and remote have the branch'
|
||||||
|
if $force {
|
||||||
|
print $'($bmsg), with --force, push'
|
||||||
|
git branch -u $'($remote)/($branch)' $branch
|
||||||
|
git push --force
|
||||||
|
} else {
|
||||||
|
print $'($bmsg), pull'
|
||||||
|
if (_git_status).branch != $branch {
|
||||||
|
print $'* switch to ($branch)'
|
||||||
|
git checkout $branch
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let bmsg = "* remote doesn't have that branch"
|
||||||
|
let force = (sprb $force [--force])
|
||||||
|
if $branch in $lbs {
|
||||||
|
print $'($bmsg), set upstream and push'
|
||||||
|
git checkout $branch
|
||||||
|
} else {
|
||||||
|
print $'($bmsg), create and push'
|
||||||
|
git checkout -b $branch
|
||||||
|
}
|
||||||
|
git push $force --set-upstream $remote $branch
|
||||||
|
}
|
||||||
|
|
||||||
let s = (_git_status)
|
let s = (_git_status)
|
||||||
if $s.ahead > 0 {
|
if $s.ahead > 0 {
|
||||||
|
print '* remote is behind, push'
|
||||||
git push
|
git push
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,6 +225,7 @@ export def ga [
|
||||||
} else if $restore {
|
} else if $restore {
|
||||||
let o = (spr [--source $source])
|
let o = (spr [--source $source])
|
||||||
let s = (sprb $staged [--staged])
|
let s = (sprb $staged [--staged])
|
||||||
|
let file = if ($file | is-empty) { [.] } else { [$file] }
|
||||||
git restore $o $s $file
|
git restore $o $s $file
|
||||||
} else {
|
} else {
|
||||||
let a = (sprb $all [--all])
|
let a = (sprb $all [--all])
|
||||||
|
@ -187,7 +240,7 @@ export def ga [
|
||||||
|
|
||||||
# git commit
|
# git commit
|
||||||
export def gc [
|
export def gc [
|
||||||
--message (-m): string
|
message?: string
|
||||||
--all (-A): bool
|
--all (-A): bool
|
||||||
--amend (-a): bool
|
--amend (-a): bool
|
||||||
--keep (-k): bool
|
--keep (-k): bool
|
||||||
|
@ -201,6 +254,7 @@ export def gc [
|
||||||
|
|
||||||
# git diff
|
# git diff
|
||||||
export def gd [
|
export def gd [
|
||||||
|
file?: path
|
||||||
--cached (-c): bool # cached
|
--cached (-c): bool # cached
|
||||||
--word-diff (-w): bool # word-diff
|
--word-diff (-w): bool # word-diff
|
||||||
--staged (-s): bool # staged
|
--staged (-s): bool # staged
|
||||||
|
@ -208,20 +262,39 @@ export def gd [
|
||||||
let w = (sprb $word_diff [--word-diff])
|
let w = (sprb $word_diff [--word-diff])
|
||||||
let c = (sprb $cached [--cached])
|
let c = (sprb $cached [--cached])
|
||||||
let s = (sprb $staged [--staged])
|
let s = (sprb $staged [--staged])
|
||||||
git diff $c $s $w
|
git diff $c $s $w (spr [$file])
|
||||||
}
|
}
|
||||||
|
|
||||||
# git merge and rebase
|
# git merge
|
||||||
export def gm [
|
export def gm [
|
||||||
branch?: string@"nu-complete git branches"
|
branch?: string@"nu-complete git branches"
|
||||||
--rebase (-r): bool # git rebase
|
--abort (-a): bool
|
||||||
|
--continue (-c): bool
|
||||||
|
--quit (-q): bool
|
||||||
|
--no-squash (-n): bool # git merge (no)--squash
|
||||||
|
] {
|
||||||
|
let x = if $no_squash { [] } else { [--squash] }
|
||||||
|
if ($branch | is-empty) {
|
||||||
|
git merge $x $"origin/(git_main_branch)"
|
||||||
|
} else {
|
||||||
|
git merge $x $branch
|
||||||
|
}
|
||||||
|
if not $no_squash {
|
||||||
|
git commit -v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# git rebase
|
||||||
|
# TODO: --onto: (commit_id)
|
||||||
|
export def gr [
|
||||||
|
branch?: string@"nu-complete git branches"
|
||||||
|
--interactive (-i): bool
|
||||||
--onto (-o): string
|
--onto (-o): string
|
||||||
--abort (-a): bool
|
--abort (-a): bool
|
||||||
--continue (-c): bool
|
--continue (-c): bool
|
||||||
--skip (-s): bool
|
--skip (-s): bool
|
||||||
--quit (-q): bool
|
--quit (-q): bool
|
||||||
] {
|
] {
|
||||||
if $rebase {
|
|
||||||
if $abort {
|
if $abort {
|
||||||
git rebase --abort
|
git rebase --abort
|
||||||
} else if $continue {
|
} else if $continue {
|
||||||
|
@ -230,20 +303,14 @@ export def gm [
|
||||||
git rebase --skip
|
git rebase --skip
|
||||||
} else if $quit {
|
} else if $quit {
|
||||||
git rebase --quit
|
git rebase --quit
|
||||||
} else if $onto {
|
} else if not ($onto | is-empty) {
|
||||||
git rebase --onto $branch
|
git rebase --onto $branch
|
||||||
} else {
|
} else {
|
||||||
|
let i = (sprb $interactive [--interactive])
|
||||||
if ($branch | is-empty) {
|
if ($branch | is-empty) {
|
||||||
git rebase (git_main_branch)
|
git rebase $i (git_main_branch)
|
||||||
} else {
|
} else {
|
||||||
git rebase $branch
|
git rebase $i $branch
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($branch | is-empty) {
|
|
||||||
git merge $"origin/(git_main_branch)"
|
|
||||||
} else {
|
|
||||||
git merge $branch
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,18 +337,22 @@ export def gcp [
|
||||||
}
|
}
|
||||||
|
|
||||||
# git reset
|
# git reset
|
||||||
export def gr [
|
export def grs [
|
||||||
commit?: string@"nu-complete git log"
|
commit?: string@"nu-complete git log"
|
||||||
--hard (-h): bool
|
--hard (-h): bool
|
||||||
|
--clean (-c): bool
|
||||||
] {
|
] {
|
||||||
let h = (sprb $hard [--hard])
|
let h = (sprb $hard [--hard])
|
||||||
let c = (spr [$commit])
|
let cm = (spr [$commit])
|
||||||
git reset $h $c
|
git reset $h $cm
|
||||||
|
if $clean {
|
||||||
|
git clean -fd
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# git remote
|
# git remote
|
||||||
export def grmt [
|
export def grm [
|
||||||
remote?: string@"nu-complete git remotes"
|
remote?: string@"nu-complete git remotes"
|
||||||
uri?: string
|
uri?: string
|
||||||
--add (-a): bool
|
--add (-a): bool
|
||||||
|
@ -338,7 +409,7 @@ export def gha [] {
|
||||||
| reverse
|
| reverse
|
||||||
}
|
}
|
||||||
|
|
||||||
export def gsq [] {
|
export def ggc [] {
|
||||||
git reflog expire --all --expire=now
|
git reflog expire --all --expire=now
|
||||||
git gc --prune=now --aggressive
|
git gc --prune=now --aggressive
|
||||||
}
|
}
|
||||||
|
@ -506,6 +577,24 @@ def "nu-complete git branches" [] {
|
||||||
| each {|x| $"($x|str trim)"}
|
| each {|x| $"($x|str trim)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export def remote_braches [] {
|
||||||
|
git branch -r
|
||||||
|
| lines
|
||||||
|
| str trim
|
||||||
|
| filter {|x| not ($x | str starts-with 'origin/HEAD') }
|
||||||
|
| each {|x| $x | split row '/'}
|
||||||
|
}
|
||||||
|
|
||||||
|
def "nu-complete git remote branches" [context: string, offset: int] {
|
||||||
|
let ctx = ($context | split row ' ')
|
||||||
|
let rb = (remote_braches)
|
||||||
|
if ($ctx | length) < 3 {
|
||||||
|
$rb | each {|x| {value: $x.1, description: $x.0} }
|
||||||
|
} else {
|
||||||
|
$rb | filter {|x| $x.1 == $ctx.1 } | each {|x| $x.0}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def "nu-complete git remotes" [] {
|
def "nu-complete git remotes" [] {
|
||||||
^git remote | lines | each { |line| $line | str trim }
|
^git remote | lines | each { |line| $line | str trim }
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,7 @@ export def kgh [
|
||||||
name?: string@"nu-complete helm list"
|
name?: string@"nu-complete helm list"
|
||||||
--namespace (-n): string@"nu-complete kube ns"
|
--namespace (-n): string@"nu-complete kube ns"
|
||||||
--manifest (-m): bool
|
--manifest (-m): bool
|
||||||
|
--values(-v): bool
|
||||||
--all (-a): bool
|
--all (-a): bool
|
||||||
] {
|
] {
|
||||||
if ($name | is-empty) {
|
if ($name | is-empty) {
|
||||||
|
@ -128,9 +129,10 @@ export def kgh [
|
||||||
} else {
|
} else {
|
||||||
if $manifest {
|
if $manifest {
|
||||||
helm get manifest $name (spr [-n $namespace])
|
helm get manifest $name (spr [-n $namespace])
|
||||||
|
} else if $values {
|
||||||
|
helm get values $name (spr [-n $namespace])
|
||||||
} else {
|
} else {
|
||||||
helm get notes $name (spr [-n $namespace])
|
helm get notes $name (spr [-n $namespace])
|
||||||
helm get values $name (spr [-n $namespace])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,11 +146,18 @@ def "nu-complete helm charts" [context: string, offset: int] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def record-to-set-json [value] {
|
||||||
|
$value | transpose k v
|
||||||
|
| each {|x| $"($x.k)=($x.v | to json -r)"}
|
||||||
|
| str join ','
|
||||||
|
}
|
||||||
|
|
||||||
# helm install or upgrade via values file
|
# helm install or upgrade via values file
|
||||||
export def kah [
|
export def kah [
|
||||||
name: string@"nu-complete helm list"
|
name: string@"nu-complete helm list"
|
||||||
chart: string@"nu-complete helm charts"
|
chart: string@"nu-complete helm charts"
|
||||||
values: path
|
valuefile: path
|
||||||
|
--values (-v): any
|
||||||
--namespace (-n): string@"nu-complete kube ns"
|
--namespace (-n): string@"nu-complete kube ns"
|
||||||
] {
|
] {
|
||||||
let update = $name in (
|
let update = $name in (
|
||||||
|
@ -156,7 +165,8 @@ export def kah [
|
||||||
| from json | get name
|
| from json | get name
|
||||||
)
|
)
|
||||||
let act = if $update { [upgrade] } else { [install] }
|
let act = if $update { [upgrade] } else { [install] }
|
||||||
helm $act $name $chart -f $values (spr [-n $namespace])
|
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 install or upgrade via values file
|
# helm install or upgrade via values file
|
||||||
|
@ -179,12 +189,17 @@ export def kdelh [
|
||||||
|
|
||||||
# helm template
|
# helm template
|
||||||
export def kh [
|
export def kh [
|
||||||
name: string
|
|
||||||
chart: string@"nu-complete helm charts"
|
chart: string@"nu-complete helm charts"
|
||||||
values: path
|
valuefile: path
|
||||||
--namespace (-n): string@"nu-complete kube ns"
|
--values (-v): any
|
||||||
|
--namespace (-n): string@"nu-complete kube ns"='test'
|
||||||
|
--app (-a): string='test'
|
||||||
] {
|
] {
|
||||||
kubectl template $name $chart -f $values (spr [-n $namespace])
|
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 $app $chart -f $valuefile $values (spr [-n $namespace])
|
||||||
|
| save -f $target
|
||||||
}
|
}
|
||||||
|
|
||||||
### ctx
|
### ctx
|
||||||
|
@ -238,23 +253,67 @@ export def kn [ns: string@"nu-complete kube ns"] {
|
||||||
kubectl config set-context --current $"--namespace=($ns)"
|
kubectl config set-context --current $"--namespace=($ns)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def parse_cellpath [path] {
|
||||||
|
$path | split row '.' | each {|x|
|
||||||
|
if ($x | find --regex "^[0-9]+$" | is-empty) {
|
||||||
|
$x
|
||||||
|
} else {
|
||||||
|
$x | into int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_cellpath [record path] {
|
||||||
|
$path | reduce -f $record {|it, acc| $acc | get $it }
|
||||||
|
}
|
||||||
|
|
||||||
|
def set_cellpath [record path value] {
|
||||||
|
if ($path | length) > 1 {
|
||||||
|
$record | upsert ($path | first) {|it|
|
||||||
|
set_cellpath ($it | get ($path | first)) ($path | range 1..) $value
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$record | upsert ($path | last) $value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def upsert_row [table col mask id value] {
|
||||||
|
# let value = ($mask | reduce -f $value {|it, acc|
|
||||||
|
# let path = (parse_cellpath $it)
|
||||||
|
# set_cellpath $value $path (get_cellpath $table $path)
|
||||||
|
# })
|
||||||
|
if $id in ($table | get $col) {
|
||||||
|
$table | each {|x|
|
||||||
|
if ($x | get $col) == $id {
|
||||||
|
$value
|
||||||
|
} else {
|
||||||
|
$x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$table | append $value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export def 'kconf import' [name: string, path: string] {
|
export def 'kconf import' [name: string, path: string] {
|
||||||
let k = (kube-config)
|
let k = (kube-config)
|
||||||
let d = $k.data
|
mut d = $k.data
|
||||||
let i = (cat $path | from yaml)
|
let i = (cat $path | from yaml)
|
||||||
let c = {
|
let c = {
|
||||||
name: $name,
|
|
||||||
context: {
|
context: {
|
||||||
cluster: $name,
|
cluster: $name,
|
||||||
namespace: default,
|
namespace: default,
|
||||||
user: $name
|
user: $name
|
||||||
}
|
}
|
||||||
|
name: $name,
|
||||||
}
|
}
|
||||||
$d
|
$d.clusters = (upsert_row $d.clusters name [] $name ($i.clusters.0 | upsert name $name))
|
||||||
| upsert contexts ($d.contexts | append $c)
|
$d.users = (upsert_row $d.users name [] $name ($i.users.0 | upsert name $name))
|
||||||
| upsert clusters ($d.clusters | append ($i.clusters.0 | upsert name $name))
|
$d.contexts = (upsert_row $d.contexts name [] $name $c)
|
||||||
| upsert users ($d.users | append ($i.users.0 | upsert name $name))
|
$d | to yaml
|
||||||
| to yaml
|
}
|
||||||
|
|
||||||
|
export def 'kconf delete' [name: string@"nu-complete kube ctx"] {
|
||||||
}
|
}
|
||||||
|
|
||||||
export def 'kconf export' [name: string@"nu-complete kube ctx"] {
|
export def 'kconf export' [name: string@"nu-complete kube ctx"] {
|
||||||
|
@ -372,7 +431,7 @@ export def kg [
|
||||||
if ($jsonpath | is-empty) {
|
if ($jsonpath | is-empty) {
|
||||||
let wide = (sprb $wide [-o wide])
|
let wide = (sprb $wide [-o wide])
|
||||||
if ($verbose) {
|
if ($verbose) {
|
||||||
kubectl get -o json $n $k $r | from json | get items
|
kubectl get -o json $n $k $r $l | from json | get items
|
||||||
| each {|x|
|
| each {|x|
|
||||||
{
|
{
|
||||||
name: $x.metadata.name
|
name: $x.metadata.name
|
||||||
|
@ -385,15 +444,24 @@ export def kg [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if $watch {
|
} else if $watch {
|
||||||
kubectl get $n $k $r $wide --watch
|
kubectl get $n $k $r $l $wide --watch
|
||||||
} else {
|
} else {
|
||||||
kubectl get $n $k $r $wide | from ssv -a
|
kubectl get $n $k $r $l $wide | from ssv -a
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
kubectl get $n $k $r $"--output=jsonpath={($jsonpath)}" | from json
|
kubectl get $n $k $r $"--output=jsonpath={($jsonpath)}" | from json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# kubectl describe
|
||||||
|
export def kd [
|
||||||
|
r: string@"nu-complete kube kind"
|
||||||
|
i: string@"nu-complete kube res"
|
||||||
|
-n: string@"nu-complete kube ns"
|
||||||
|
] {
|
||||||
|
kubectl describe (spr [-n $n]) $r $i
|
||||||
|
}
|
||||||
|
|
||||||
# kubectl create
|
# kubectl create
|
||||||
export def kc [
|
export def kc [
|
||||||
r: string@"nu-complete kube kind"
|
r: string@"nu-complete kube kind"
|
||||||
|
@ -412,15 +480,6 @@ export def ky [
|
||||||
kubectl get (spr [-n $n]) -o yaml $r $i
|
kubectl get (spr [-n $n]) -o yaml $r $i
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl describe
|
|
||||||
export def kd [
|
|
||||||
r: string@"nu-complete kube kind"
|
|
||||||
i: string@"nu-complete kube res"
|
|
||||||
-n: string@"nu-complete kube ns"
|
|
||||||
] {
|
|
||||||
kubectl describe (spr [-n $n]) $r $i
|
|
||||||
}
|
|
||||||
|
|
||||||
# kubectl edit
|
# kubectl edit
|
||||||
export def ke [
|
export def ke [
|
||||||
k: string@"nu-complete kube kind"
|
k: string@"nu-complete kube kind"
|
||||||
|
@ -500,13 +559,34 @@ export def kdp [-n: string@"nu-complete kube ns", pod: string@"nu-complete kube
|
||||||
|
|
||||||
# kubectl attach (exec -it)
|
# kubectl attach (exec -it)
|
||||||
export def ka [
|
export def ka [
|
||||||
pod: string@"nu-complete kube pods"
|
pod?: string@"nu-complete kube pods"
|
||||||
-n: string@"nu-complete kube ns"
|
-n: string@"nu-complete kube ns"
|
||||||
--container(-c): string@"nu-complete kube ctns"
|
--container(-c): string@"nu-complete kube ctns"
|
||||||
|
--selector(-l): string
|
||||||
...args
|
...args
|
||||||
] {
|
] {
|
||||||
let n = (spr [-n $n])
|
let n = (spr [-n $n])
|
||||||
let c = (spr [-c $container])
|
let pod = if ($selector | is-empty) { $pod } else {
|
||||||
|
let pods = (kgp -n $n -l $selector | each {|x| $x.NAME})
|
||||||
|
if ($pods | length) == 1 {
|
||||||
|
$pods.0
|
||||||
|
} else {
|
||||||
|
$pods | input list 'select pod '
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let c = if ($container | is-empty) {
|
||||||
|
if ($selector | is-empty) { [] } else {
|
||||||
|
let cs = (kgp -n $n $pod -p '.spec.containers[*].name' | split row ' ')
|
||||||
|
let ctn = if ($cs | length) == 1 {
|
||||||
|
$cs.0
|
||||||
|
} else {
|
||||||
|
$cs | input list 'select container '
|
||||||
|
}
|
||||||
|
[-c $ctn]
|
||||||
|
}
|
||||||
|
} 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 })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,15 @@ export def ensure-cache [cache path action] {
|
||||||
}
|
}
|
||||||
|
|
||||||
def "kube ctx" [] {
|
def "kube ctx" [] {
|
||||||
let cache = $'($env.HOME)/.cache/nu-power/kube.json'
|
mut cache = ''
|
||||||
let file = if ($env.KUBECONFIG? | is-empty) { $"($env.HOME)/.kube/config" } else { $env.KUBECONFIG }
|
mut file = ''
|
||||||
|
if ($env.KUBECONFIG? | is-empty) {
|
||||||
|
$cache = $'($env.HOME)/.cache/nu-power/kube.json'
|
||||||
|
$file = $"($env.HOME)/.kube/config"
|
||||||
|
} else {
|
||||||
|
$cache = $"($env.HOME)/.cache/nu-power/kube-($env.KUBECONFIG | str replace -a '/' ':').json"
|
||||||
|
$file = $env.KUBECONFIG
|
||||||
|
}
|
||||||
if not ($file | path exists) { return $nothing }
|
if not ($file | path exists) { return $nothing }
|
||||||
ensure-cache $cache $file {
|
ensure-cache $cache $file {
|
||||||
do -i {
|
do -i {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue