mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
more precise parsing of cmd through the information in nu.scope.commands
(#522)
- refactor `after` to `after`, `before` - `gp` support `back-to-prev` for merging scenes - `ka`, `ke`, `kep`, `ked`, `kes` supports labels selector Co-authored-by: agent <agent@nuc>
This commit is contained in:
parent
b92eb7c03f
commit
140f556049
4 changed files with 190 additions and 42 deletions
|
@ -2,8 +2,20 @@ def "nu-complete ps" [] {
|
||||||
ps -l | each {|x| { value: $"($x.pid)", description: $x.command } }
|
ps -l | each {|x| { value: $"($x.pid)", description: $x.command } }
|
||||||
}
|
}
|
||||||
|
|
||||||
# after { do something ... } <pid>
|
# after <pid> {|| do something ... }
|
||||||
export def main [action, pid: string@"nu-complete ps"] {
|
export def main [
|
||||||
|
pid: string@"nu-complete ps"
|
||||||
|
action
|
||||||
|
] {
|
||||||
|
do -i { tail --pid $pid -f /dev/null }
|
||||||
|
do $action
|
||||||
|
}
|
||||||
|
|
||||||
|
# before {|| do something ... } <pid>
|
||||||
|
export def before [
|
||||||
|
action
|
||||||
|
pid: string@"nu-complete ps"
|
||||||
|
] {
|
||||||
do -i { tail --pid $pid -f /dev/null }
|
do -i { tail --pid $pid -f /dev/null }
|
||||||
do $action
|
do $action
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,63 @@ def spr [args] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def get-sign [cmd] {
|
||||||
|
let x = ($nu.scope.commands | where name == $cmd).signatures?.0?.any?
|
||||||
|
mut s = []
|
||||||
|
mut n = {}
|
||||||
|
for it in $x {
|
||||||
|
if $it.parameter_type in ['switch' 'named'] {
|
||||||
|
let name = $it.parameter_name
|
||||||
|
if not ($it.short_flag | is-empty) {
|
||||||
|
$n = ($n | upsert $it.short_flag $name)
|
||||||
|
}
|
||||||
|
if $it.parameter_type == 'switch' {
|
||||||
|
$s = ($s | append $name)
|
||||||
|
if not ($it.short_flag | is-empty) {
|
||||||
|
$s = ($s | append $it.short_flag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ switch: $s, name: $n }
|
||||||
|
}
|
||||||
|
|
||||||
|
def "parse cmd" [] {
|
||||||
|
let cmd = ($in | split row ' ')
|
||||||
|
let sign = (get-sign $cmd.0)
|
||||||
|
mut sw = ''
|
||||||
|
mut pos = []
|
||||||
|
mut opt = {}
|
||||||
|
for c in $cmd {
|
||||||
|
if ($sw | is-empty) {
|
||||||
|
if ($c | str starts-with '-') {
|
||||||
|
let c = if ($c | str substring 1..2) != '-' {
|
||||||
|
let k = ($c | str substring 1..)
|
||||||
|
if $k in $sign.name {
|
||||||
|
$'($sign.name | get $k)'
|
||||||
|
} else {
|
||||||
|
$k
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$c | str substring 2..
|
||||||
|
}
|
||||||
|
if $c in $sign.switch {
|
||||||
|
$opt = ($opt | upsert $c true)
|
||||||
|
} else {
|
||||||
|
$sw = $c
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$pos ++= [$c]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$opt = ($opt | upsert $sw $c)
|
||||||
|
$sw = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$opt.args = $pos
|
||||||
|
$opt
|
||||||
|
}
|
||||||
|
|
||||||
# git status
|
# git status
|
||||||
export def gs [] {
|
export def gs [] {
|
||||||
git status
|
git status
|
||||||
|
@ -102,6 +159,7 @@ export def gb [
|
||||||
if $branch in (remote_braches | each {|x| $x.1}) and (agree -n 'delete remote 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 }
|
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
||||||
git push $remote -d $branch
|
git push $remote -d $branch
|
||||||
|
git branch -D -r $'($remote)/($branch)'
|
||||||
}
|
}
|
||||||
} else if $branch in $bs {
|
} else if $branch in $bs {
|
||||||
git checkout $branch
|
git checkout $branch
|
||||||
|
@ -146,6 +204,7 @@ export def gp [
|
||||||
--init (-i): bool # git init
|
--init (-i): bool # git init
|
||||||
--merge (-m): bool # git pull (no)--rebase
|
--merge (-m): bool # git pull (no)--rebase
|
||||||
--autostash (-a): bool # git pull --autostash
|
--autostash (-a): bool # git pull --autostash
|
||||||
|
--back-to-prev (-b): bool # back to branch
|
||||||
] {
|
] {
|
||||||
if $submodule {
|
if $submodule {
|
||||||
git submodule update
|
git submodule update
|
||||||
|
@ -169,21 +228,30 @@ export def gp [
|
||||||
git branch -u $'($remote)/($branch)' $branch
|
git branch -u $'($remote)/($branch)' $branch
|
||||||
git push --force
|
git push --force
|
||||||
} else {
|
} else {
|
||||||
|
let prev = (_git_status).branch
|
||||||
print $'($bmsg), pull'
|
print $'($bmsg), pull'
|
||||||
if (_git_status).branch != $branch {
|
if $prev != $branch {
|
||||||
print $'* switch to ($branch)'
|
print $'* switch to ($branch)'
|
||||||
git checkout $branch
|
git checkout $branch
|
||||||
}
|
}
|
||||||
git pull $m $a
|
git pull $m $a
|
||||||
|
if $back_to_prev {
|
||||||
|
git checkout $prev
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let prev = (_git_status).branch
|
||||||
print "* local doesn't have that branch, fetch"
|
print "* local doesn't have that branch, fetch"
|
||||||
git checkout -b $branch
|
git checkout -b $branch
|
||||||
git fetch $remote $branch
|
git fetch $remote $branch
|
||||||
git branch -u $'($remote)/($branch)' $branch
|
git branch -u $'($remote)/($branch)' $branch
|
||||||
git pull $m $a -v
|
git pull $m $a -v
|
||||||
|
if $back_to_prev {
|
||||||
|
git checkout $prev
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let prev = (_git_status).branch
|
||||||
let bmsg = "* remote doesn't have that branch"
|
let bmsg = "* remote doesn't have that branch"
|
||||||
let force = (sprb $force [--force])
|
let force = (sprb $force [--force])
|
||||||
if $branch in $lbs {
|
if $branch in $lbs {
|
||||||
|
@ -194,6 +262,9 @@ export def gp [
|
||||||
git checkout -b $branch
|
git checkout -b $branch
|
||||||
}
|
}
|
||||||
git push $force --set-upstream $remote $branch
|
git push $force --set-upstream $remote $branch
|
||||||
|
if $back_to_prev {
|
||||||
|
git checkout $prev
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let s = (_git_status)
|
let s = (_git_status)
|
||||||
|
@ -586,9 +657,9 @@ export def remote_braches [] {
|
||||||
}
|
}
|
||||||
|
|
||||||
def "nu-complete git remote branches" [context: string, offset: int] {
|
def "nu-complete git remote branches" [context: string, offset: int] {
|
||||||
let ctx = ($context | split row ' ')
|
let ctx = ($context | parse cmd)
|
||||||
let rb = (remote_braches)
|
let rb = (remote_braches)
|
||||||
if ($ctx | length) < 3 {
|
if ($ctx.args | length) < 3 {
|
||||||
$rb | each {|x| {value: $x.1, description: $x.0} }
|
$rb | each {|x| {value: $x.1, description: $x.0} }
|
||||||
} else {
|
} else {
|
||||||
$rb | filter {|x| $x.1 == $ctx.1 } | each {|x| $x.0}
|
$rb | filter {|x| $x.1 == $ctx.1 } | each {|x| $x.0}
|
||||||
|
|
|
@ -1,25 +1,58 @@
|
||||||
export def "parse cmd" [] {
|
def get-sign [cmd] {
|
||||||
$in
|
let x = ($nu.scope.commands | where name == $cmd).signatures?.0?.any?
|
||||||
| split row ' '
|
mut s = []
|
||||||
| reduce -f { args: [], sw: '' } {|it, acc|
|
mut n = {}
|
||||||
if ($acc.sw|is-empty) {
|
for it in $x {
|
||||||
if ($it|str starts-with '-') {
|
if $it.parameter_type in ['switch' 'named'] {
|
||||||
$acc | upsert sw $it
|
let name = $it.parameter_name
|
||||||
} else {
|
if not ($it.short_flag | is-empty) {
|
||||||
let args = ($acc.args | append $it)
|
$n = ($n | upsert $it.short_flag $name)
|
||||||
$acc | upsert args $args
|
|
||||||
}
|
}
|
||||||
} else {
|
if $it.parameter_type == 'switch' {
|
||||||
if ($it|str starts-with '-') {
|
$s = ($s | append $name)
|
||||||
$acc
|
if not ($it.short_flag | is-empty) {
|
||||||
| upsert $acc.sw true
|
$s = ($s | append $it.short_flag)
|
||||||
| upsert sw $it
|
}
|
||||||
} else {
|
|
||||||
$acc | upsert $acc.sw $it | upsert sw ''
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
| reject sw
|
{ switch: $s, name: $n }
|
||||||
|
}
|
||||||
|
|
||||||
|
def "parse cmd" [] {
|
||||||
|
let cmd = ($in | split row ' ')
|
||||||
|
let sign = (get-sign $cmd.0)
|
||||||
|
mut sw = ''
|
||||||
|
mut pos = []
|
||||||
|
mut opt = {}
|
||||||
|
for c in $cmd {
|
||||||
|
if ($sw | is-empty) {
|
||||||
|
if ($c | str starts-with '-') {
|
||||||
|
let c = if ($c | str substring 1..2) != '-' {
|
||||||
|
let k = ($c | str substring 1..)
|
||||||
|
if $k in $sign.name {
|
||||||
|
$'($sign.name | get $k)'
|
||||||
|
} else {
|
||||||
|
$k
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$c | str substring 2..
|
||||||
|
}
|
||||||
|
if $c in $sign.switch {
|
||||||
|
$opt = ($opt | upsert $c true)
|
||||||
|
} else {
|
||||||
|
$sw = $c
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$pos ++= [$c]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$opt = ($opt | upsert $sw $c)
|
||||||
|
$sw = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$opt.args = $pos
|
||||||
|
$opt
|
||||||
}
|
}
|
||||||
|
|
||||||
export def ensure-cache-by-lines [cache path action] {
|
export def ensure-cache-by-lines [cache path action] {
|
||||||
|
@ -149,7 +182,7 @@ export def kgh [
|
||||||
|
|
||||||
def "nu-complete helm list" [context: string, offset: int] {
|
def "nu-complete helm list" [context: string, offset: int] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
kgh -n $ctx.-n? | each {|x| {value: $x.name description: $x.updated} }
|
kgh -n $ctx.namespace? | each {|x| {value: $x.name description: $x.updated} }
|
||||||
}
|
}
|
||||||
|
|
||||||
def "nu-complete helm charts" [context: string, offset: int] {
|
def "nu-complete helm charts" [context: string, offset: int] {
|
||||||
|
@ -368,14 +401,14 @@ def "nu-complete kube kind" [] {
|
||||||
def "nu-complete kube res" [context: string, offset: int] {
|
def "nu-complete kube res" [context: string, offset: int] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
let kind = ($ctx | get args.1)
|
let kind = ($ctx | get args.1)
|
||||||
let ns = if ($ctx.-n? | is-empty) { [] } else { [-n $ctx.-n] }
|
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] {
|
def "nu-complete kube res via name" [context: string, offset: int] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get args.0 | str substring (-1..)))
|
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get args.0 | str substring (-1..)))
|
||||||
let ns = if ($ctx.-n? | is-empty) { [] } else { [-n $ctx.-n] }
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -383,8 +416,8 @@ def "nu-complete kube jsonpath" [context: string] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
let kind = ($ctx | get args.1)
|
let kind = ($ctx | get args.1)
|
||||||
let res = ($ctx | get args.2)
|
let res = ($ctx | get args.2)
|
||||||
let path = $ctx.-p?
|
let path = $ctx.jsonpath?
|
||||||
let ns = if ($ctx.-n? | is-empty) { [] } else { [-n $ctx.-n] }
|
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||||
mut r = []
|
mut r = []
|
||||||
if ($path | is-empty) {
|
if ($path | is-empty) {
|
||||||
if ($context | str ends-with '-p ') {
|
if ($context | str ends-with '-p ') {
|
||||||
|
@ -494,10 +527,22 @@ export def ky [
|
||||||
# kubectl edit
|
# kubectl edit
|
||||||
export def ke [
|
export def ke [
|
||||||
k: string@"nu-complete kube kind"
|
k: string@"nu-complete kube kind"
|
||||||
r: string@"nu-complete kube res"
|
r?: string@"nu-complete kube res"
|
||||||
-n: string@"nu-complete kube ns"
|
-n: string@"nu-complete kube ns"
|
||||||
|
--selector(-l): string
|
||||||
] {
|
] {
|
||||||
kubectl edit (spr [-n $n]) $k $r
|
let n = (spr [-n $n])
|
||||||
|
let r = if ($selector | is-empty) { $r } else {
|
||||||
|
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 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
$res | input list $'select ($k) '
|
||||||
|
}
|
||||||
|
}
|
||||||
|
kubectl edit $n $k $r
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl delete
|
# kubectl delete
|
||||||
|
@ -559,8 +604,12 @@ export def kgpw [
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl edit pod
|
# kubectl edit pod
|
||||||
export def kep [-n: string@"nu-complete kube ns", pod: string@"nu-complete kube res via name"] {
|
export def kep [
|
||||||
ke -n $n pod $pod
|
-n: string@"nu-complete kube ns"
|
||||||
|
pod?: string@"nu-complete kube res via name"
|
||||||
|
--selector (-l): string
|
||||||
|
] {
|
||||||
|
ke -n $n pod -l $selector $pod
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl describe pod
|
# kubectl describe pod
|
||||||
|
@ -578,11 +627,19 @@ export def ka [
|
||||||
] {
|
] {
|
||||||
let n = (spr [-n $n])
|
let n = (spr [-n $n])
|
||||||
let pod = if ($selector | is-empty) { $pod } else {
|
let pod = if ($selector | is-empty) { $pod } else {
|
||||||
let pods = (kgp -n $n -l $selector | each {|x| $x.NAME})
|
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 {
|
if ($pods | length) == 1 {
|
||||||
$pods.0
|
($pods.0).name
|
||||||
|
} else if ($pods | length) == 0 {
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
$pods | input list 'select pod '
|
($pods | input list 'select pod ').name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let c = if ($container | is-empty) {
|
let c = if ($container | is-empty) {
|
||||||
|
@ -621,7 +678,7 @@ def "nu-complete port forward type" [] {
|
||||||
def "nu-complete kube port" [context: string, offset: int] {
|
def "nu-complete kube port" [context: string, offset: int] {
|
||||||
let ctx = ($context | parse cmd)
|
let ctx = ($context | parse cmd)
|
||||||
let kind = ($ctx | get args.1)
|
let kind = ($ctx | get args.1)
|
||||||
let ns = if ($ctx.-n? | is-empty) { [] } else { [-n $ctx.-n] }
|
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') {
|
if ($kind | str starts-with 's') {
|
||||||
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
|
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
|
||||||
|
@ -688,8 +745,12 @@ export def kgs [
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl edit service
|
# kubectl edit service
|
||||||
export def kes [svc: string@"nu-complete kube res via name", -n: string@"nu-complete kube ns"] {
|
export def kes [
|
||||||
ke -n $n service $svc
|
svc?: string@"nu-complete kube res via name"
|
||||||
|
-n: string@"nu-complete kube ns"
|
||||||
|
--selector (-l): string
|
||||||
|
] {
|
||||||
|
ke -n $n service -l $selector $svc
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl delete service
|
# kubectl delete service
|
||||||
|
@ -708,8 +769,12 @@ export def kgd [
|
||||||
}
|
}
|
||||||
|
|
||||||
# kubectl edit deployment
|
# kubectl edit deployment
|
||||||
export def ked [d: string@"nu-complete kube res via name", -n: string@"nu-complete kube ns"] {
|
export def ked [
|
||||||
ke -n $n deployments $d
|
d?: string@"nu-complete kube res via name"
|
||||||
|
-n: string@"nu-complete kube ns"
|
||||||
|
--selector (-l): string
|
||||||
|
] {
|
||||||
|
ke -n $n deployments -l $selector $d
|
||||||
}
|
}
|
||||||
|
|
||||||
def "nu-complete num9" [] { [0 1 2 3] }
|
def "nu-complete num9" [] { [0 1 2 3] }
|
||||||
|
|
|
@ -360,7 +360,7 @@ def up_prompt [segment] {
|
||||||
}
|
}
|
||||||
|
|
||||||
export def default_env [name value] {
|
export def default_env [name value] {
|
||||||
if ($name in ($env | columns)) {
|
if ($name in $env) {
|
||||||
$env | get $name
|
$env | get $name
|
||||||
} else {
|
} else {
|
||||||
$value
|
$value
|
||||||
|
@ -415,7 +415,7 @@ export def-env init [] {
|
||||||
|
|
||||||
let-env config = ( $env.config | update menus ($env.config.menus
|
let-env config = ( $env.config | update menus ($env.config.menus
|
||||||
| each {|x|
|
| each {|x|
|
||||||
if ($x.marker in ($env.NU_POWER_MENU_MARKER | columns)) {
|
if ($x.marker in $env.NU_POWER_MENU_MARKER) {
|
||||||
let c = ($env.NU_POWER_MENU_MARKER | get $x.marker)
|
let c = ($env.NU_POWER_MENU_MARKER | get $x.marker)
|
||||||
$x | upsert marker $'(ansi -e {fg: $c})(char nf_left_segment_thin) '
|
$x | upsert marker $'(ansi -e {fg: $c})(char nf_left_segment_thin) '
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue