1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 07:07:46 +00:00

upgrade to 0.78: git, ssh, docker, kubernetes (#438)

Co-authored-by: agent <agent@nuc>
This commit is contained in:
fj0r 2023-04-06 14:01:03 +08:00 committed by GitHub
parent b9ec76b493
commit d8db82c6c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 45 deletions

View file

@ -15,7 +15,7 @@ export def dp [] {
| lines | lines
| each {|x| | each {|x|
let r = ($x | from json) let r = ($x | from json)
let t = ($r.created | str substring ',25' | into datetime -f '%Y-%m-%d %H:%M:%S %z' ) - 8hr let t = ($r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z' )
$r | upsert created $t $r | upsert created $t
} }
} else if $cli == 'podman' { } else if $cli == 'podman' {
@ -23,7 +23,7 @@ export def dp [] {
| lines | lines
| each {|x| | each {|x|
let r = ($x | from json) let r = ($x | from json)
let t = ($r.created | str substring ',32' | into datetime ) - 8hr let t = ($r.created | str substring ..32 | into datetime )
$r | upsert created $t $r | upsert created $t
} }
} else { } else {
@ -94,7 +94,7 @@ export def da [
} }
def "nu-complete docker cp" [cmd: string, offset: int] { def "nu-complete docker cp" [cmd: string, offset: int] {
let argv = ($cmd | str substring [0 $offset] | split row ' ') let argv = ($cmd | str substring ..$offset | split row ' ')
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 } let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
let ctn = ( let ctn = (
^$env.docker-cli ps ^$env.docker-cli ps
@ -107,10 +107,10 @@ def "nu-complete docker cp" [cmd: string, offset: int] {
| lines | lines
| each {|x| $"($n | get 0):($x)"} | each {|x| $"($n | get 0):($x)"}
} else { } else {
let files = do -i { let files = (do -i {
ls -a $"($p)*" ls -a $"($p)*"
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
} })
$files | append $ctn $files | append $ctn
} }
} }
@ -207,7 +207,7 @@ def "nu-complete docker run sshkey" [ctx: string, pos: int] {
} }
def "nu-complete docker run proxy" [] { def "nu-complete docker run proxy" [] {
let hostaddr = do -i {hostname -I | split row ' ' | get 0} let hostaddr = (do -i { hostname -I | split row ' ' | get 0 })
[$"http://($hostaddr):7890" $"http://localhost:7890"] [$"http://($hostaddr):7890" $"http://localhost:7890"]
} }
@ -283,7 +283,7 @@ export def dx [
ng: 'ng:/srv' ng: 'ng:/srv'
pg: 'pg:/var/lib/postgresql/data' pg: 'pg:/var/lib/postgresql/data'
} }
let c = do -i {$__dx_cache | transpose k v | where {|x| $dx | str contains $x.k} | get v.0} let c = (do -i {$__dx_cache | transpose k v | where {|x| $dx | str contains $x.k} | get v.0})
let c = if ($c|is-empty) { let c = if ($c|is-empty) {
'' ''
} else if $mount_cache { } else if $mount_cache {
@ -308,9 +308,9 @@ export def dx [
def "nu-complete registry list" [cmd: string, offset: int] { def "nu-complete registry list" [cmd: string, offset: int] {
let cmd = ($cmd | split row ' ') let cmd = ($cmd | split row ' ')
let url = do -i { $cmd | get 2 } let url = (do -i { $cmd | get 2 })
let reg = do -i { $cmd | get 3 } let reg = (do -i { $cmd | get 3 })
let tag = do -i { $cmd | get 4 } let tag = (do -i { $cmd | get 4 })
if ($reg|is-empty) { if ($reg|is-empty) {
if ($env | has 'REGISTRY_TOKEN') { if ($env | has 'REGISTRY_TOKEN') {
http get -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/_catalog" http get -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/_catalog"

View file

@ -4,7 +4,7 @@ export def _git_stat [n] {
| lines | lines
| reduce -f { c: '', r: [] } {|it, acc| | reduce -f { c: '', r: [] } {|it, acc|
if ($it | str starts-with '»¦«') { if ($it | str starts-with '»¦«') {
$acc | upsert c ($it | str substring '6,') $acc | upsert c ($it | str substring 6.. )
} else if ($it | find -r '[0-9]+ file.+change' | is-empty) { } else if ($it | find -r '[0-9]+ file.+change' | is-empty) {
$acc $acc
} else { } else {
@ -20,7 +20,7 @@ export def _git_stat [n] {
let col = if ($i.col | str starts-with 'file') { let col = if ($i.col | str starts-with 'file') {
'file' 'file'
} else { } else {
$i.col | str substring ',3' $i.col | str substring ..3
} }
let num = ($i.num | into int) let num = ($i.num | into int)
$a | upsert $col $num $a | upsert $col $num
@ -37,14 +37,14 @@ export def _git_log [v num] {
let stat = if $v { let stat = if $v {
_git_stat $num _git_stat $num
} else { {} } } else { {} }
let r = do -i { let r = (do -i {
git log -n $num --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD git log -n $num --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD
| lines | lines
| split column "»¦«" sha message author email date | split column "»¦«" sha message author email date
| each {|x| ($x| upsert date ($x.date | into datetime))} | each {|x| ($x| upsert date ($x.date | into datetime))}
} })
if $v { if $v {
$r | merge { $stat } | reverse $r | merge $stat | reverse
} else { } else {
$r | reverse $r | reverse
} }
@ -54,6 +54,7 @@ def "nu-complete git log" [] {
git log -n 32 --pretty=%h»¦«%s git log -n 32 --pretty=%h»¦«%s
| lines | lines
| split column "»¦«" value description | split column "»¦«" value description
| each {|x| $x | update value $"`($x.value)`"}
} }
export def glg [ export def glg [
@ -113,7 +114,12 @@ export def gm [branch:string@"nu-complete git branches"] {
} }
def git_main_branch [] { def git_main_branch [] {
git remote show origin | lines | str trim | find 'HEAD branch: ' | first | split words | last git remote show origin
| lines
| str trim
| find --regex 'HEAD .*?[: ].+'
| first
| str replace 'HEAD .*?[: ](.+)' '$1'
} }
def git_current_branch [] { def git_current_branch [] {

View file

@ -23,9 +23,9 @@ export def "parse cmd" [] {
} }
export def ensure-index [index path action] { export def ensure-index [index path action] {
let ts = do -i { ls $path | sort-by modified | reverse | get 0.modified } let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
if ($ts | is-empty) { return false } if ($ts | is-empty) { return false }
let tc = do -i { ls $index | get 0.modified } let tc = (do -i { ls $index | get 0.modified })
if not (($index | path exists) and ($ts < $tc)) { if not (($index | path exists) and ($ts < $tc)) {
mkdir (dirname $index) mkdir (dirname $index)
do $action do $action
@ -75,7 +75,7 @@ export def "kube-config" [] {
def "nu-complete kube ctx" [] { def "nu-complete kube ctx" [] {
let k = (kube-config) let k = (kube-config)
let cache = $'($env.HOME)/.cache/nu-complete/k8s/(basename $k.path).json' let cache = $'($env.HOME)/.cache/nu-complete/k8s/(basename $k.path).json'
ensure-index $cache $k.path { ensure-index $cache $k.path { ||
let clusters = ($k.data | get clusters | select name cluster.server) let clusters = ($k.data | get clusters | select name cluster.server)
let data = ($k.data let data = ($k.data
| get contexts | get contexts
@ -172,7 +172,7 @@ def "nu-complete kube def" [] {
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 def = ($ctx | get args.1) let def = ($ctx | get args.1)
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
kubectl get $ns $def | from ssv -a | get NAME kubectl get $ns $def | from ssv -a | get NAME
} }
@ -251,16 +251,16 @@ export def kgno [] {
### pods ### pods
def "nu-complete kube pods" [context: string, offset: int] { def "nu-complete kube pods" [context: string, offset: int] {
let ctx = ($context | parse cmd) let ctx = ($context | parse cmd)
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
kubectl get $ns pods | from ssv -a | get NAME kubectl get $ns pods | from ssv -a | get NAME
} }
def "nu-complete kube ctns" [context: string, offset: int] { def "nu-complete kube ctns" [context: string, offset: int] {
let ctx = ($context | parse cmd) let ctx = ($context | parse cmd)
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
let ctn = do -i { $ctx | get '-c' } let ctn = (do -i { $ctx | get '-c' })
let ctn = if ($ctn|is-empty) { [] } else { [-c $ctn] } let ctn = if ($ctn|is-empty) { [] } else { [-c $ctn] }
let pod = ($ctx | get args.1) 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 ' '
@ -355,11 +355,11 @@ export def kpf [
} }
def "nu-complete kube cp" [cmd: string, offset: int] { def "nu-complete kube cp" [cmd: string, offset: int] {
let ctx = ($cmd | str substring [0 $offset] | parse cmd) let ctx = ($cmd | str substring ..$offset | parse cmd)
let p = ($ctx.args | get (($ctx.args | length) - 1)) let p = ($ctx.args | get (($ctx.args | length) - 1))
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
let c = do -i { $ctx | get '-c' } let c = (do -i { $ctx | get '-c' })
let c = if ($c|is-empty) { [] } else { [-c $c] } let c = if ($c|is-empty) { [] } else { [-c $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 ':') let n = ($p | split row ':')
@ -368,9 +368,9 @@ def "nu-complete kube cp" [cmd: string, offset: int] {
| lines | lines
| each {|x| $"($n | get 0):($x)"} | each {|x| $"($n | get 0):($x)"}
} else { } else {
let files = do -i { ls -a $"($p)*" let files = (do -i { ls -a $"($p)*"
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
} })
$files | append $ctn $files | append $ctn
} }
} }
@ -388,7 +388,7 @@ export def kcp [
### service ### service
def "nu-complete kube service" [context: string, offset: int] { def "nu-complete kube service" [context: string, offset: int] {
let ctx = ($context | parse cmd) let ctx = ($context | parse cmd)
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
kubectl get $ns services | from ssv -a | get NAME kubectl get $ns services | from ssv -a | get NAME
} }
@ -412,7 +412,7 @@ export def kdels [svc: string@"nu-complete kube service", -n: string@"nu-complet
### deployments ### deployments
def "nu-complete kube deployments" [context: string, offset: int] { def "nu-complete kube deployments" [context: string, offset: int] {
let ctx = ($context | parse cmd) let ctx = ($context | parse cmd)
let ns = do -i { $ctx | get '-n' } let ns = (do -i { $ctx | get '-n' })
let ns = if ($ns|is-empty) { [] } else { [-n $ns] } let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
kubectl get $ns deployments | from ssv -a | get NAME kubectl get $ns deployments | from ssv -a | get NAME
} }
@ -479,8 +479,8 @@ export def ktp [-n: string@"nu-complete kube ns"] {
kubectl top pod $n | from ssv -a | rename name cpu mem kubectl top pod $n | from ssv -a | rename name cpu mem
| each {|x| { | each {|x| {
name: $x.name name: $x.name
cpu: ($x.cpu| str substring ',-1' | into decimal) cpu: ($x.cpu| str substring ..-1 | into decimal)
mem: ($x.mem | str substring ',-2' | into decimal) mem: ($x.mem | str substring ..-2 | into decimal)
} } } }
} }
@ -489,8 +489,8 @@ export def ktpa [] {
| each {|x| { | each {|x| {
namespace: $x.namespace namespace: $x.namespace
name: $x.name name: $x.name
cpu: ($x.cpu| str substring ',-1' | into decimal) cpu: ($x.cpu| str substring ..-1 | into decimal)
mem: ($x.mem | str substring ',-2' | into decimal) mem: ($x.mem | str substring ..-2 | into decimal)
} } } }
} }
@ -499,10 +499,10 @@ export def ktn [] {
kubectl top node | from ssv -a | rename name cpu pcpu mem pmem kubectl top node | from ssv -a | rename name cpu pcpu mem pmem
| each {|x| { | each {|x| {
name: $x.name name: $x.name
cpu: ($x.cpu| str substring ',-1' | into decimal) cpu: ($x.cpu| str substring ..-1 | into decimal)
cpu%: (($x.pcpu| str substring ',-1' | into decimal) / 100) cpu%: (($x.pcpu| str substring ..-1 | into decimal) / 100)
mem: ($x.mem | str substring ',-2' | into decimal) mem: ($x.mem | str substring ..-2 | into decimal)
mem%: (($x.pmem | str substring ',-1' | into decimal) / 100) mem%: (($x.pmem | str substring ..-1 | into decimal) / 100)
} } } }
} }

View file

@ -1,7 +1,7 @@
export def ensure-index [index path action] { export def ensure-index [index path action] {
let ts = do -i { ls $path | sort-by modified | reverse | get 0.modified } let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
if ($ts | is-empty) { return false } if ($ts | is-empty) { return false }
let tc = do -i { ls $index | get 0.modified } let tc = (do -i { ls $index | get 0.modified })
if not (($index | path exists) and ($ts < $tc)) { if not (($index | path exists) and ($ts < $tc)) {
mkdir (dirname $index) mkdir (dirname $index)
do $action do $action
@ -49,7 +49,7 @@ def fmt-group [p] {
def "ssh-hosts" [] { def "ssh-hosts" [] {
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json' let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
ensure-index $cache ~/.ssh/**/* { ensure-index $cache ~/.ssh/**/* { ||
let data = (ssh-list | each {|x| let data = (ssh-list | each {|x|
let uri = $"($x.User)@($x.HostName):($x.Port)" let uri = $"($x.User)@($x.HostName):($x.Port)"
{ {
@ -101,7 +101,7 @@ export extern main [
def "nu-complete scp" [cmd: string, offset: int] { def "nu-complete scp" [cmd: string, offset: int] {
let argv = ($cmd | str substring [0 $offset] | split row ' ') let argv = ($cmd | str substring ..$offset | split row ' ')
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 } let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
let ssh = (ssh-hosts | get completion let ssh = (ssh-hosts | get completion
| each {|x| {value: $"($x.value):" description: $x.uri} } | each {|x| {value: $"($x.value):" description: $x.uri} }
@ -112,10 +112,10 @@ def "nu-complete scp" [cmd: string, offset: int] {
| lines | lines
| each {|x| $"($n | get 0):($x)"} | each {|x| $"($n | get 0):($x)"}
} else { } else {
let files = do -i { let files = (do -i {
ls -a $"($p)*" ls -a $"($p)*"
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
} })
$files | append $ssh $files | append $ssh
} }
} }