1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +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:
fj0r 2023-05-31 09:55:03 +08:00 committed by GitHub
parent 80e9d07d80
commit 4a1b4c73c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 301 additions and 126 deletions

View file

@ -68,18 +68,17 @@ export def dp [-n: string@"nu-complete docker ns"] {
export def di [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) images
| from ssv -a
| rename repo tag id created size
| each {|x|
let size = ($x.size | into filesize)
let path = ($x.repo | split row '/')
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
tag: $x.tag
id: $x.id
created: $x.created
tag: $x.TAG
id: $x.'IMAGE ID'
created: $x.CREATED
size: $size
}
}
@ -286,9 +285,9 @@ export def dr [
let daemon = if $daemon { [-d] } else { [--rm -it] }
let mnt = (spr [-v $mnt])
let workdir = (spr [-w $workdir])
let vols = if ($vols|is-empty) { [] } else { $vols | transpose k v | each {|x| $"-v '(host-path $x.k):($x.v)'"} }
let envs = if ($envs|is-empty) { [] } else { $envs | transpose k v | each {|x| $"-e ($x.k)=($x.v)"} }
let ports = if ($ports|is-empty) { [] } else { $ports | transpose k v | each {|x|[-p $"($x.k):($x.v)"]} | flatten }
let 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 appimage = (sprb $appimage [--device /dev/fuse])

View file

@ -1,10 +1,13 @@
def agree [prompt] {
def agree [
prompt
--default-not (-n): bool
] {
let prompt = if ($prompt | str ends-with '!') {
$'(ansi red)($prompt)(ansi reset)'
} else {
$'($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] {
@ -29,8 +32,13 @@ def spr [args] {
}
}
# git status and stash
export def gs [
# git status
export def gs [] {
git status
}
# git stash
export def gst [
--apply (-a): bool
--clear (-c): bool
--drop (-d): bool
@ -55,7 +63,7 @@ export def gs [
} else if $all {
git stash --all (sprb $include_untracked [--include-untracked])
} else {
git status
git stash
}
}
@ -75,27 +83,28 @@ export def gl [
# git branch
export def gb [
branch?: string@"nu-complete git branches"
remote?: string@"nu-complete git remote branches"
--delete (-d): bool
--remote (-r): bool
--no-merged (-n): bool
] {
let bs = (git branch | lines | each {|x| $x | str substring 2..})
if ($branch | is-empty) {
if $remote {
git branch --remote
} else if $no_merged {
git branch --no-merged
} else {
git branch
let d = {
local: (git branch | lines)
remote: (git branch --remote | lines)
no-merged: (git branch --no-merged | lines)
}
print ($d | table -n 1 -e)
} else if $delete {
if $branch in $bs and (agree 'branch will be delete!') {
git branch -D $branch
}
if $branch in (remote_braches | each {|x| $x.1}) and (agree -n 'delete remote branch?!') {
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
git push $remote -d $branch
}
} else if $branch in $bs {
if $delete {
if (agree 'branch will be delete!') {
git branch -D $branch
}
} else {
git checkout $branch
}
git checkout $branch
} else {
if (agree 'create new branch?') {
git checkout -b $branch
@ -103,50 +112,93 @@ export def gb [
}
}
# git clone, init
export def-env gn [
repo?: string@"nu-complete git branches"
local?: path
--submodule (-s): bool # git submodule
--init (-i): bool # git init
] {
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 branches"
remote?: string@"nu-complete git remotes"
branch?: string@"nu-complete git remote branches"
remote?: string@"nu-complete git remote branches"
--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
--init (-i): bool # git init
--rebase (-r): bool # git pull --rebase
--merge (-m): bool # git pull (no)--rebase
--autostash (-a): bool # git pull --autostash
] {
if not ($clone | is-empty) {
git clone (sprb $submodule [--recurse-submodules]) $clone
} else if $submodule {
if $init {
git submodule init
} else {
git submodule update
}
} else if $init {
let repo = $branch
git init $repo
} else if $force {
git push --force
if $submodule {
git submodule update
} else if $override {
git pull
git pull --rebase
git add --all
git commit -v -a --no-edit --amend
git push --force
} else if $set_upstream {
let remote = if ($remote | is-empty) { 'origin' } else { $remote }
let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch }
git push -u $remote $branch
} else if not ($branch | is-empty) {
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
git fetch $remote $branch
} else {
let r = (sprb $rebase [--rebase])
let m = if $merge { [] } else { [--rebase] }
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)
if $s.ahead > 0 {
print '* remote is behind, push'
git push
}
}
@ -173,6 +225,7 @@ export def ga [
} else if $restore {
let o = (spr [--source $source])
let s = (sprb $staged [--staged])
let file = if ($file | is-empty) { [.] } else { [$file] }
git restore $o $s $file
} else {
let a = (sprb $all [--all])
@ -187,10 +240,10 @@ export def ga [
# git commit
export def gc [
--message (-m): string
--all (-A): bool
--amend (-a): bool
--keep (-k): bool
message?: string
--all (-A): bool
--amend (-a): bool
--keep (-k): bool
] {
let m = (spr [-m $message])
let a = (sprb $all [--all])
@ -201,6 +254,7 @@ export def gc [
# git diff
export def gd [
file?: path
--cached (-c): bool # cached
--word-diff (-w): bool # word-diff
--staged (-s): bool # staged
@ -208,42 +262,55 @@ export def gd [
let w = (sprb $word_diff [--word-diff])
let c = (sprb $cached [--cached])
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 [
branch?: string@"nu-complete git branches"
--rebase (-r): bool # git rebase
--onto (-o): string
--abort (-a): bool
--continue (-c): bool
--skip (-s): bool
--quit (-q): bool
branch?: string@"nu-complete git branches"
--abort (-a): bool
--continue (-c): bool
--quit (-q): bool
--no-squash (-n): bool # git merge (no)--squash
] {
if $rebase {
if $abort {
git rebase --abort
} else if $continue {
git rebase --continue
} else if $skip {
git rebase --skip
} else if $quit {
git rebase --quit
} else if $onto {
git rebase --onto $branch
} else {
if ($branch | is-empty) {
git rebase (git_main_branch)
} else {
git rebase $branch
}
}
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
--abort (-a): bool
--continue (-c): bool
--skip (-s): bool
--quit (-q): bool
] {
if $abort {
git rebase --abort
} else if $continue {
git rebase --continue
} else if $skip {
git rebase --skip
} else if $quit {
git rebase --quit
} else if not ($onto | is-empty) {
git rebase --onto $branch
} else {
let i = (sprb $interactive [--interactive])
if ($branch | is-empty) {
git merge $"origin/(git_main_branch)"
git rebase $i (git_main_branch)
} else {
git merge $branch
git rebase $i $branch
}
}
}
@ -270,18 +337,22 @@ export def gcp [
}
# git reset
export def gr [
commit?: string@"nu-complete git log"
--hard (-h): bool
export def grs [
commit?: string@"nu-complete git log"
--hard (-h): bool
--clean (-c): bool
] {
let h = (sprb $hard [--hard])
let c = (spr [$commit])
git reset $h $c
let cm = (spr [$commit])
git reset $h $cm
if $clean {
git clean -fd
}
}
# git remote
export def grmt [
export def grm [
remote?: string@"nu-complete git remotes"
uri?: string
--add (-a): bool
@ -338,7 +409,7 @@ export def gha [] {
| reverse
}
export def gsq [] {
export def ggc [] {
git reflog expire --all --expire=now
git gc --prune=now --aggressive
}
@ -506,6 +577,24 @@ def "nu-complete git branches" [] {
| 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" [] {
^git remote | lines | each { |line| $line | str trim }
}

View file

@ -114,6 +114,7 @@ export def kgh [
name?: string@"nu-complete helm list"
--namespace (-n): string@"nu-complete kube ns"
--manifest (-m): bool
--values(-v): bool
--all (-a): bool
] {
if ($name | is-empty) {
@ -128,9 +129,10 @@ export def kgh [
} else {
if $manifest {
helm get manifest $name (spr [-n $namespace])
} else if $values {
helm get values $name (spr [-n $namespace])
} else {
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)
}
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
export def kah [
name: string@"nu-complete helm list"
chart: string@"nu-complete helm charts"
values: path
valuefile: path
--values (-v): any
--namespace (-n): string@"nu-complete kube ns"
] {
let update = $name in (
@ -156,7 +165,8 @@ export def kah [
| from json | get name
)
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
@ -179,12 +189,17 @@ export def kdelh [
# helm template
export def kh [
name: string
chart: string@"nu-complete helm charts"
values: path
--namespace (-n): string@"nu-complete kube ns"
valuefile: path
--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
@ -238,23 +253,67 @@ export def kn [ns: string@"nu-complete kube 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] {
let k = (kube-config)
let d = $k.data
mut d = $k.data
let i = (cat $path | from yaml)
let c = {
name: $name,
context: {
cluster: $name,
namespace: default,
user: $name
}
name: $name,
}
$d
| upsert contexts ($d.contexts | append $c)
| upsert clusters ($d.clusters | append ($i.clusters.0 | upsert name $name))
| upsert users ($d.users | append ($i.users.0 | upsert name $name))
| to yaml
$d.clusters = (upsert_row $d.clusters name [] $name ($i.clusters.0 | upsert name $name))
$d.users = (upsert_row $d.users name [] $name ($i.users.0 | upsert name $name))
$d.contexts = (upsert_row $d.contexts name [] $name $c)
$d | to yaml
}
export def 'kconf delete' [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) {
let wide = (sprb $wide [-o wide])
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|
{
name: $x.metadata.name
@ -385,15 +444,24 @@ export def kg [
}
}
} else if $watch {
kubectl get $n $k $r $wide --watch
kubectl get $n $k $r $l $wide --watch
} else {
kubectl get $n $k $r $wide | from ssv -a
kubectl get $n $k $r $l $wide | from ssv -a
}
} else {
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
export def kc [
r: string@"nu-complete kube kind"
@ -412,15 +480,6 @@ export def ky [
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
export def ke [
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)
export def ka [
pod: string@"nu-complete kube pods"
pod?: string@"nu-complete kube pods"
-n: string@"nu-complete kube ns"
--container(-c): string@"nu-complete kube ctns"
--selector(-l): string
...args
] {
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 })
}

View file

@ -11,8 +11,15 @@ export def ensure-cache [cache path action] {
}
def "kube ctx" [] {
let cache = $'($env.HOME)/.cache/nu-power/kube.json'
let file = if ($env.KUBECONFIG? | is-empty) { $"($env.HOME)/.kube/config" } else { $env.KUBECONFIG }
mut cache = ''
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 }
ensure-cache $cache $file {
do -i {