mirror of
https://github.com/nushell/nu_scripts.git
synced 2024-08-17 09:20:28 +03:00
cwdhist: can place the path anywhere in the buffer (#669)
- git, kubernetes: upgrade cmd parse --------- Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
parent
99fe279311
commit
6a40ceb3db
127
modules/cmd_parse/cmd_parse.nu
Normal file
127
modules/cmd_parse/cmd_parse.nu
Normal file
@ -0,0 +1,127 @@
|
||||
export def get-sign [cmd] {
|
||||
let x = (scope commands | where name == $cmd).signatures?.0?.any?
|
||||
mut s = []
|
||||
mut n = {}
|
||||
mut p = []
|
||||
mut pr = []
|
||||
mut r = []
|
||||
for it in $x {
|
||||
if $it.parameter_type == 'switch' {
|
||||
if not ($it.short_flag | is-empty) {
|
||||
$s ++= $it.short_flag
|
||||
}
|
||||
if not ($it.parameter_name | is-empty) {
|
||||
$s ++= $it.parameter_name
|
||||
}
|
||||
} else if $it.parameter_type == 'named' {
|
||||
if ($it.parameter_name | is-empty) {
|
||||
$n = ($n | upsert $it.short_flag $it.short_flag)
|
||||
} else if ($it.short_flag | is-empty) {
|
||||
$n = ($n | upsert $it.parameter_name $it.parameter_name)
|
||||
} else {
|
||||
$n = ($n | upsert $it.short_flag $it.parameter_name)
|
||||
}
|
||||
} else if $it.parameter_type == 'positional' {
|
||||
if $it.is_optional == false {
|
||||
$p ++= $it.parameter_name
|
||||
} else {
|
||||
$pr ++= $it.parameter_name
|
||||
}
|
||||
} else if $it.parameter_type == 'rest' {
|
||||
$r ++= $it.parameter_name
|
||||
}
|
||||
}
|
||||
{ switch: $s, name: $n, positional: ($p ++ $pr), rest: $r }
|
||||
}
|
||||
|
||||
# "test -h [123 (3213 3)] 123 `a sdf` --cd --ef sadf -g" | cmd token
|
||||
export def "cmd token" [] {
|
||||
let s = ($in | split row '' | range 1..-2)
|
||||
let s = if ($s | last) == ' ' { $s } else { $s | append ' ' }
|
||||
mut par = []
|
||||
mut res = []
|
||||
mut cur = ''
|
||||
mut esc = false
|
||||
for c in $s {
|
||||
if $c == '\' {
|
||||
$esc = true
|
||||
} else {
|
||||
if $esc {
|
||||
$cur ++= $c
|
||||
$esc = false
|
||||
} else {
|
||||
if $c == ' ' and ($par | length) == 0 {
|
||||
$res ++= [$cur]
|
||||
$cur = ''
|
||||
} else {
|
||||
if $c in ['{' '[' '('] {
|
||||
$par ++= $c
|
||||
}
|
||||
if $c in ['}' ']' ')'] {
|
||||
$par = ($par | range ..-2)
|
||||
}
|
||||
if $c in ['"' "'" '`'] {
|
||||
if ($par | length) > 0 and ($par | last) == $c {
|
||||
$par = ($par | range ..-2)
|
||||
} else {
|
||||
$par ++= $c
|
||||
}
|
||||
}
|
||||
$cur ++= $c
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res
|
||||
}
|
||||
|
||||
export def "cmd parse" [] {
|
||||
let token = ($in | cmd token)
|
||||
let sign = (get-sign $token.0)
|
||||
mut sw = ''
|
||||
mut pos = []
|
||||
mut opt = {}
|
||||
for c in $token {
|
||||
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
|
||||
let p = $pos | range 1..($sign.positional | length)
|
||||
let rest = $pos | range (($sign.positional | length) + 1)..-1
|
||||
$opt._pos = ( $p | enumerate
|
||||
| reduce -f {} {|it, acc|
|
||||
$acc | upsert ($sign.positional | get $it.index) $it.item
|
||||
} )
|
||||
if ($sign.rest | length) > 0 {
|
||||
$opt._pos = ($opt._pos | upsert $sign.rest.0 $rest)
|
||||
}
|
||||
$opt
|
||||
}
|
||||
|
||||
# def test [a b x? ...y --cd(-c) --ef(-e):string -g -h:int --ij --lm:bool] {}
|
||||
# (scope commands | where name == test).signatures?.0?.any?
|
||||
# get-sign test | to yaml
|
||||
# "test -h 111 123 'asdf' --cd --ef sadf -g -h 222" | cmd parse | to yaml
|
@ -1,7 +1,7 @@
|
||||
def __cwdhist_menu [] {
|
||||
{
|
||||
name: cwdhist_menu
|
||||
only_buffer_difference: false
|
||||
only_buffer_difference: true
|
||||
marker: "| "
|
||||
type: {
|
||||
layout: list
|
||||
@ -14,11 +14,12 @@ def __cwdhist_menu [] {
|
||||
}
|
||||
source: { |buffer, position|
|
||||
#$"[($position)]($buffer);(char newline)" | save -a ~/.cache/cwdhist.log
|
||||
let t = ($buffer | split row ' ' | last)
|
||||
if $env.cwd_history_full {
|
||||
open $nu.history-path | query db $"
|
||||
select cwd as value, count\(*) as cnt
|
||||
from history
|
||||
where cwd like '%($buffer)%'
|
||||
where cwd like '%($t)%'
|
||||
group by cwd
|
||||
order by cnt desc
|
||||
limit 50
|
||||
@ -27,7 +28,7 @@ def __cwdhist_menu [] {
|
||||
open $env.cwd_history_file | query db $"
|
||||
select cwd as value, count
|
||||
from cwd_history
|
||||
where cwd like '%($buffer)%'
|
||||
where cwd like '%($t)%'
|
||||
order by count desc
|
||||
limit 50
|
||||
;"
|
||||
@ -71,8 +72,8 @@ def __cwdhist_switching [] {
|
||||
}
|
||||
|
||||
export def empty-sqlite [] {
|
||||
# pre-created empty sqlite database
|
||||
'H4sIAAAAAAAAA+3LQQpAYBiE4fn+bMURvhPYuIAD2MgJLChKSv9BrZ1GSrHD/n1qmtlM29Rj7H1Y1rmLXiqXmSp3SeERuzrRzfQuqJi29Fy5lO0fHgAAAAAAAAAAAAAA4LcDzTELuwAgAAA='
|
||||
# sqlite3 empty.db "VACUUM;"; cat empty.db | gzip | encode base64
|
||||
'H4sIAAAAAAAAAwsO9MksSVVIyy/KTSxRMGYQYGBkZHBQUGBgYGCEYhhAZhMLGBn0ihbwglgCZOgeBaNgFIyCUTAKRsEoGAWjYBSMglEwCkYBVQAANHgbMAAQAAA='
|
||||
| decode base64 --binary | gzip -d
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
export-env {
|
||||
for c in [podman nerdctl docker] {
|
||||
for c in [nerdctl podman docker] {
|
||||
if not (which $c | is-empty) {
|
||||
$env.docker-cli = $c
|
||||
break
|
||||
@ -29,6 +29,16 @@ def spr [args] {
|
||||
}
|
||||
}
|
||||
|
||||
def local_image [name] {
|
||||
let s = ($name | split row '/')
|
||||
if ($s | length) > 1 {
|
||||
$name
|
||||
} else {
|
||||
['localhost', $name] | str join '/'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def "nu-complete docker ns" [] {
|
||||
if $env.docker-cli == 'nerdctl' {
|
||||
^$env.docker-cli namespace list
|
||||
@ -338,6 +348,7 @@ export def container-create [
|
||||
if $dry_run {
|
||||
echo $"docker ($ns | str join ' ') run --name ($name) ($args|str join ' ') ($img) ($cmd | flatten)"
|
||||
} else {
|
||||
let $img = if $env.docker-cli == 'nerdctl' { local_image $img } else { $img }
|
||||
^$env.docker-cli $ns run --name $name $args $img ($cmd | flatten)
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use cmd_parse.nu *
|
||||
|
||||
def agree [
|
||||
prompt
|
||||
--default-not (-n)
|
||||
@ -32,66 +34,6 @@ def spr [args] {
|
||||
}
|
||||
}
|
||||
|
||||
def get-sign [cmd] {
|
||||
let x = (scope commands | where name == $cmd).signatures?.0?.any?
|
||||
mut s = []
|
||||
mut n = {}
|
||||
mut p = []
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else if $it.parameter_type == 'positional' {
|
||||
$p = ($p | append $it.parameter_name)
|
||||
}
|
||||
}
|
||||
{ switch: $s, name: $n, positional: $p }
|
||||
}
|
||||
|
||||
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._pos = ( $pos | range 1.. | enumerate | reduce -f {} {|it, acc| $acc | upsert ($sign.positional | get $it.index) $it.item } )
|
||||
$opt
|
||||
}
|
||||
|
||||
# git status
|
||||
export def gs [] {
|
||||
@ -690,7 +632,7 @@ export def remote_braches [] {
|
||||
}
|
||||
|
||||
def "nu-complete git remote branches" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let rb = (remote_braches)
|
||||
if ($ctx._args | length) < 3 {
|
||||
$rb | each {|x| {value: $x.1, description: $x.0} }
|
||||
|
@ -1,63 +1,4 @@
|
||||
def get-sign [cmd] {
|
||||
let x = (scope commands | where name == $cmd).signatures?.0?.any?
|
||||
mut s = []
|
||||
mut n = {}
|
||||
mut p = []
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else if $it.parameter_type == 'positional' {
|
||||
$p = ($p | append $it.parameter_name)
|
||||
}
|
||||
}
|
||||
{ switch: $s, name: $n, positional: $p }
|
||||
}
|
||||
|
||||
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._pos = ( $pos | range 1.. | enumerate | reduce -f {} {|it, acc| $acc | upsert ($sign.positional | get $it.index) $it.item } )
|
||||
$opt
|
||||
}
|
||||
use cmd_parse.nu *
|
||||
|
||||
export def ensure-cache-by-lines [cache path action] {
|
||||
let ls = (do -i { open $path | lines | length })
|
||||
@ -185,12 +126,12 @@ export def kgh [
|
||||
}
|
||||
|
||||
def "nu-complete helm list" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
kgh -n $ctx.namespace? | each {|x| {value: $x.name description: $x.updated} }
|
||||
}
|
||||
|
||||
def "nu-complete helm charts" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let path = ($ctx | get _pos.chart)
|
||||
let path = if ($path | is-empty) { '.' } else { $path }
|
||||
let paths = (do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} })
|
||||
@ -228,6 +169,7 @@ export def kdh [
|
||||
valuefile: path
|
||||
--values (-v): any
|
||||
--namespace (-n): string@"nu-complete kube ns"
|
||||
--ignore-image (-i)
|
||||
--has-plugin (-h)
|
||||
] {
|
||||
if $has_plugin {
|
||||
@ -245,6 +187,9 @@ export def kdh [
|
||||
let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] }
|
||||
let target = $'/tmp/($chart | path basename).($name).out.yaml'
|
||||
helm template --debug $name $chart -f $valuefile $values (spr [-n $namespace]) | save -f $target
|
||||
if $ignore_image {
|
||||
do -i { yq -i ea 'del(.spec.template.spec.containers.[].image)' $target }
|
||||
}
|
||||
kubectl diff -f $target
|
||||
}
|
||||
}
|
||||
@ -433,21 +378,21 @@ def "nu-complete kube kind" [] {
|
||||
}
|
||||
|
||||
def "nu-complete kube res" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let kind = ($ctx | get _args.1)
|
||||
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||
kubectl get $ns $kind | from ssv -a | get NAME
|
||||
}
|
||||
|
||||
def "nu-complete kube res via name" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..)))
|
||||
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||
kubectl get $ns $kind | from ssv -a | get NAME
|
||||
}
|
||||
|
||||
def "nu-complete kube jsonpath" [context: string] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let kind = ($ctx | get _args.1)
|
||||
let res = ($ctx | get _args.2)
|
||||
let path = $ctx.jsonpath?
|
||||
@ -597,14 +542,14 @@ export def kgno [] {
|
||||
}
|
||||
|
||||
def "nu-complete kube pods" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let ns = (do -i { $ctx | get namespace })
|
||||
let ns = (spr [-n $ns])
|
||||
kubectl get $ns pods | from ssv -a | get NAME
|
||||
}
|
||||
|
||||
def "nu-complete kube ctns" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let ns = (do -i { $ctx | get namespace })
|
||||
let ns = (spr [-n $ns])
|
||||
let ctn = (do -i { $ctx | get container })
|
||||
@ -715,7 +660,7 @@ def "nu-complete port forward type" [] {
|
||||
}
|
||||
|
||||
def "nu-complete kube port" [context: string, offset: int] {
|
||||
let ctx = ($context | parse cmd)
|
||||
let ctx = ($context | cmd parse)
|
||||
let kind = ($ctx | get _args.1)
|
||||
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
|
||||
let res = ($ctx | get _args.2)
|
||||
@ -744,7 +689,7 @@ export def kpf [
|
||||
}
|
||||
|
||||
def "nu-complete kube cp" [cmd: string, offset: int] {
|
||||
let ctx = ($cmd | str substring ..$offset | parse cmd)
|
||||
let ctx = ($cmd | str substring ..$offset | cmd parse)
|
||||
let p = ($ctx._args | get (($ctx._args | length) - 1))
|
||||
let ns = (do -i { $ctx | get namespace })
|
||||
let ns = (spr [-n $ns])
|
||||
|
Loading…
Reference in New Issue
Block a user