mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 07:07:46 +00:00
K8s (#347)
* ssh kubernetes docker git just nvim * avoid null when cache ssh * docker cp & kubectl cp & scp Co-authored-by: agent <agent@nuc>
This commit is contained in:
parent
02fac2cd18
commit
2b785af0a4
5 changed files with 174 additions and 65 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
export def has [name] {
|
||||||
|
$name in ($in | columns) and (not ($in | get $name | is-empty))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
alias docker = podman
|
alias docker = podman
|
||||||
|
|
||||||
export def dp [] {
|
export def dp [] {
|
||||||
|
@ -42,8 +47,9 @@ def "nu-complete docker images" [] {
|
||||||
| each {|x| $"($x.REPOSITORY):($x.TAG)"}
|
| each {|x| $"($x.REPOSITORY):($x.TAG)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
export def dl [ctn: string@"nu-complete docker container"] {
|
export def dl [ctn: string@"nu-complete docker container" -n: int = 100] {
|
||||||
docker logs -f $ctn
|
let n = if $n == 0 { [] } else { [--tail $n] }
|
||||||
|
docker logs -f $n $ctn
|
||||||
}
|
}
|
||||||
|
|
||||||
export def da [
|
export def da [
|
||||||
|
@ -57,9 +63,31 @@ export def da [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "nu-complete docker cp" [cmd: string, offset: int] {
|
||||||
|
let argv = ($cmd | str substring [0 $offset] | split row ' ')
|
||||||
|
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
|
||||||
|
let ctn = (
|
||||||
|
docker ps
|
||||||
|
| from ssv -a
|
||||||
|
| each {|x| {description: $x.'CONTAINER ID' value: $"($x.NAMES):" }}
|
||||||
|
)
|
||||||
|
let n = ($p | split row ':')
|
||||||
|
if $"($n | get 0):" in ($ctn | get value) {
|
||||||
|
docker exec ($n | get 0) sh -c $"ls -dp ($n | get 1)*"
|
||||||
|
| lines
|
||||||
|
| each {|x| $"($n | get 0):($x)"}
|
||||||
|
} else {
|
||||||
|
let files = do -i {
|
||||||
|
ls -a $"($p)*"
|
||||||
|
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
|
||||||
|
}
|
||||||
|
$files | append $ctn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export def dcp [
|
export def dcp [
|
||||||
lhs: string@"nu-complete docker container",
|
lhs: string@"nu-complete docker cp",
|
||||||
rhs: string@"nu-complete docker container"
|
rhs: string@"nu-complete docker cp"
|
||||||
] {
|
] {
|
||||||
docker cp $lhs $rhs
|
docker cp $lhs $rhs
|
||||||
}
|
}
|
||||||
|
@ -252,14 +280,14 @@ def "nu-complete registry list" [cmd: string, offset: int] {
|
||||||
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 (do -i { $env.REGISTRY_TOKEN } | is-empty) {
|
if ($env | has 'REGISTRY_TOKEN') {
|
||||||
fetch $"($url)/v2/_catalog"
|
|
||||||
} else {
|
|
||||||
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/_catalog"
|
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/_catalog"
|
||||||
|
} else {
|
||||||
|
fetch $"($url)/v2/_catalog"
|
||||||
}
|
}
|
||||||
| get repositories
|
| get repositories
|
||||||
} else if ($tag|is-empty) {
|
} else if ($tag|is-empty) {
|
||||||
if (do -i { $env.REGISTRY_TOKEN } | is-empty) {
|
if ($env | has 'REGISTRY_TOKEN') {
|
||||||
fetch $"($url)/v2/($reg)/tags/list"
|
fetch $"($url)/v2/($reg)/tags/list"
|
||||||
} else {
|
} else {
|
||||||
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/($reg)/tags/list"
|
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/($reg)/tags/list"
|
||||||
|
@ -273,10 +301,10 @@ export def "registry list" [
|
||||||
url: string
|
url: string
|
||||||
reg: string@"nu-complete registry list"
|
reg: string@"nu-complete registry list"
|
||||||
] {
|
] {
|
||||||
if (do -i { $env.REGISTRY_TOKEN } | is-empty) {
|
if ('REGISTRY_TOKEN' in (env).name) {
|
||||||
fetch $"($url)/v2/($reg)/tags/list"
|
|
||||||
} else {
|
|
||||||
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/($reg)/tags/list"
|
fetch -H [authorization $"Basic ($env.REGISTRY_TOKEN)"] $"($url)/v2/($reg)/tags/list"
|
||||||
|
} else {
|
||||||
|
fetch $"($url)/v2/($reg)/tags/list"
|
||||||
}
|
}
|
||||||
| get tags
|
| get tags
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,7 +154,7 @@ export alias gcb = ^git checkout -b
|
||||||
export alias gcf = git config --list
|
export alias gcf = git config --list
|
||||||
export alias gcl = git clone --recurse-submodules
|
export alias gcl = git clone --recurse-submodules
|
||||||
export alias gclean = git clean -id
|
export alias gclean = git clean -id
|
||||||
# export alias gpristine = git reset --hard && git clean -dffx
|
# export alias gpristine = git reset --hard and git clean -dffx
|
||||||
export alias gcm = git checkout (git_main_branch)
|
export alias gcm = git checkout (git_main_branch)
|
||||||
export alias gcd = git checkout develop
|
export alias gcd = git checkout develop
|
||||||
export alias gcmsg = git commit -m
|
export alias gcmsg = git commit -m
|
||||||
|
@ -195,7 +195,7 @@ export alias grs = git restore
|
||||||
export alias grset = git remote set-url
|
export alias grset = git remote set-url
|
||||||
export alias grss = git restore --source
|
export alias grss = git restore --source
|
||||||
export alias grst = git restore --staged
|
export alias grst = git restore --staged
|
||||||
export alias grt = cd "$(git rev-parse --show-toplevel || echo .)"
|
export alias grt = cd "$(git rev-parse --show-toplevel or echo .)"
|
||||||
# export alias gru = git reset --
|
# export alias gru = git reset --
|
||||||
export alias grup = git remote update
|
export alias grup = git remote update
|
||||||
export alias grv = git remote -v
|
export alias grv = git remote -v
|
||||||
|
|
|
@ -1,35 +1,25 @@
|
||||||
export def 'filter index' [...idx] {
|
|
||||||
reduce -f [] -n {|it, acc|
|
|
||||||
if $it.index not-in ($idx|flatten) {
|
|
||||||
$acc.item | append $it.item
|
|
||||||
} else {
|
|
||||||
$acc.item
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export def "parse cmd" [] {
|
export def "parse cmd" [] {
|
||||||
$in
|
let argv = ($in | split row ' ')
|
||||||
| split row ' '
|
mut pos = []
|
||||||
| reduce -f { args: [], sw: '' } {|it, acc|
|
mut opt = {}
|
||||||
if ($acc.sw|is-empty) {
|
mut sw = ''
|
||||||
if ($it|str starts-with '-') {
|
for i in $argv {
|
||||||
$acc | upsert sw $it
|
if ($i | str starts-with '-') {
|
||||||
} else {
|
if not ($sw | is-empty) {
|
||||||
let args = ($acc.args | append $it)
|
$opt = ($opt | upsert $sw true)
|
||||||
$acc | upsert args $args
|
|
||||||
}
|
}
|
||||||
|
$sw = $i
|
||||||
} else {
|
} else {
|
||||||
if ($it|str starts-with '-') {
|
if ($sw | is-empty) {
|
||||||
$acc
|
$pos ++= [$i]
|
||||||
| upsert $acc.sw true
|
|
||||||
| upsert sw $it
|
|
||||||
} else {
|
} else {
|
||||||
$acc | upsert $acc.sw $it | upsert sw ''
|
$opt = ($opt | upsert $sw $i)
|
||||||
|
$sw = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
| reject sw
|
$opt.args = $pos
|
||||||
|
return $opt
|
||||||
}
|
}
|
||||||
|
|
||||||
export def index-need-update [index path] {
|
export def index-need-update [index path] {
|
||||||
|
@ -43,15 +33,6 @@ export def index-need-update [index path] {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
export def 'str max-length' [] {
|
|
||||||
$in | reduce -f 0 {|x, a|
|
|
||||||
if ($x|is-empty) { return $a }
|
|
||||||
let l = ($x | str length)
|
|
||||||
if $l > $a { $l } else { $a }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#####
|
|
||||||
export-env {
|
export-env {
|
||||||
let-env KUBERNETES_SCHEMA_URL = $"file:///($env.HOME)/.config/kubernetes-json-schema/all.json"
|
let-env KUBERNETES_SCHEMA_URL = $"file:///($env.HOME)/.config/kubernetes-json-schema/all.json"
|
||||||
}
|
}
|
||||||
|
@ -109,7 +90,7 @@ def "nu-complete kube ctx" [] {
|
||||||
| upsert mx_cl (if $max_cl > $a.mx_cl { $max_cl } else $a.mx_cl)
|
| upsert mx_cl (if $max_cl > $a.mx_cl { $max_cl } else $a.mx_cl)
|
||||||
| upsert completion ($a.completion | append {value: $x.name, ns: $ns, cluster: $cluster})
|
| upsert completion ($a.completion | append {value: $x.name, ns: $ns, cluster: $cluster})
|
||||||
})
|
})
|
||||||
{completion: $data.completion, max: {ns: $data.mx_ns, cluster: $data.mx_cl}} | save $cache
|
{completion: $data.completion, max: {ns: $data.mx_ns, cluster: $data.mx_cl}} | save -f $cache
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = (cat $cache | from json)
|
let data = (cat $cache | from json)
|
||||||
|
@ -174,7 +155,7 @@ export def 'kconf export' [name: string@"nu-complete kube ctx"] {
|
||||||
export def-env kcconf [name: string@"nu-complete kube ctx"] {
|
export def-env kcconf [name: string@"nu-complete kube ctx"] {
|
||||||
let dist = $"($env.HOME)/.kube/config.d"
|
let dist = $"($env.HOME)/.kube/config.d"
|
||||||
mkdir $dist
|
mkdir $dist
|
||||||
kconf export $name | save -r $"($dist)/($name)"
|
kconf export $name | save -fr $"($dist)/($name)"
|
||||||
let-env KUBECONFIG = $"($dist)/($name)"
|
let-env KUBECONFIG = $"($dist)/($name)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,13 +355,35 @@ export def kpf [
|
||||||
kubectl port-forward $n $"($res)/($target)" $port
|
kubectl port-forward $n $"($res)/($target)" $port
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "nu-complete kube cp" [cmd: string, offset: int] {
|
||||||
|
let ctx = ($cmd | str substring [0 $offset] | parse cmd)
|
||||||
|
let p = ($ctx.args | get (($ctx.args | length) - 1))
|
||||||
|
let ns = do -i { $ctx | get '-n' }
|
||||||
|
let ns = if ($ns|is-empty) { [] } else { [-n $ns] }
|
||||||
|
let c = do -i { $ctx | get '-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 n = ($p | split row ':')
|
||||||
|
if $"($n | get 0):" in ($ctn | get value) {
|
||||||
|
kubectl exec $ns ($n | get 0) $c -- sh -c $"ls -dp ($n | get 1)*"
|
||||||
|
| lines
|
||||||
|
| each {|x| $"($n | get 0):($x)"}
|
||||||
|
} else {
|
||||||
|
let files = do -i { ls -a $"($p)*"
|
||||||
|
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
|
||||||
|
}
|
||||||
|
$files | append $ctn
|
||||||
|
}
|
||||||
|
}
|
||||||
export def kcp [
|
export def kcp [
|
||||||
lhs: string@"nu-complete kube pods"
|
lhs: string@"nu-complete kube cp"
|
||||||
rhs: string@"nu-complete kube pods"
|
rhs: string@"nu-complete kube cp"
|
||||||
|
-c: string@"nu-complete kube ctns"
|
||||||
-n: string@"nu-complete kube ns"
|
-n: string@"nu-complete kube ns"
|
||||||
] {
|
] {
|
||||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||||
kubectl cp $n $lhs $rhs
|
let c = if ($c|is-empty) { [] } else { [-c $c] }
|
||||||
|
kubectl cp $n $lhs $c $rhs
|
||||||
}
|
}
|
||||||
|
|
||||||
### service
|
### service
|
||||||
|
@ -441,6 +444,22 @@ export def ksd [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export def ksdr [
|
||||||
|
d: string@"nu-complete kube deployments"
|
||||||
|
num: int@"nu-complete num9"
|
||||||
|
-n: string@"nu-complete kube ns"
|
||||||
|
] {
|
||||||
|
if $num > 9 {
|
||||||
|
"too large"
|
||||||
|
} else if $num <= 0 {
|
||||||
|
"too small"
|
||||||
|
} else {
|
||||||
|
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||||
|
kubectl scale $n deployments $d --replicas 0
|
||||||
|
kubectl scale $n deployments $d --replicas $num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export alias krsd = kubectl rollout status deployment
|
export alias krsd = kubectl rollout status deployment
|
||||||
export alias kgrs = kubectl get rs
|
export alias kgrs = kubectl get rs
|
||||||
export def krh [-n: string@"nu-complete kube ns", --revision (-v): int, dpl: string@"nu-complete kube deployments"] {
|
export def krh [-n: string@"nu-complete kube ns", --revision (-v): int, dpl: string@"nu-complete kube deployments"] {
|
||||||
|
@ -456,7 +475,7 @@ export def kru [-n: string@"nu-complete kube ns", --revision (-v): int, dpl: str
|
||||||
export alias ksss = kubectl scale statefulset
|
export alias ksss = kubectl scale statefulset
|
||||||
export alias krsss = kubectl rollout status statefulset
|
export alias krsss = kubectl rollout status statefulset
|
||||||
|
|
||||||
### kubecto top pod
|
### kubectl top pod
|
||||||
export def ktp [-n: string@"nu-complete kube ns"] {
|
export def ktp [-n: string@"nu-complete kube ns"] {
|
||||||
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
let n = if ($n|is-empty) { [] } else { [-n $n] }
|
||||||
kubectl top pod $n | from ssv -a | rename name cpu mem
|
kubectl top pod $n | from ssv -a | rename name cpu mem
|
||||||
|
|
33
nvim/nvim.nu
33
nvim/nvim.nu
|
@ -1,3 +1,34 @@
|
||||||
|
## neovim configurations
|
||||||
|
# local vcs_root = require'taberm.vcs'.root
|
||||||
|
# function HookPwdChanged(after, before)
|
||||||
|
# vim.b.pwd = after
|
||||||
|
# local git_dir = vcs_root(after, nil)
|
||||||
|
# vim.api.nvim_command('silent tcd! '..(git_dir or after))
|
||||||
|
# end
|
||||||
|
|
||||||
|
# function OppositePwd()
|
||||||
|
# local tab = vim.api.nvim_get_current_tabpage()
|
||||||
|
# local wins = vim.api.nvim_tabpage_list_wins(tab)
|
||||||
|
# local cwin = vim.api.nvim_tabpage_get_win(tab)
|
||||||
|
# for _, w in ipairs(wins) do
|
||||||
|
# if cwin ~= w then
|
||||||
|
# local b = vim.api.nvim_win_get_buf(w)
|
||||||
|
# local pwd = vim.b[b].pwd
|
||||||
|
# if pwd then return pwd end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
|
# function ReadTempDrop(path, action)
|
||||||
|
# vim.api.nvim_command(action or 'botright vnew')
|
||||||
|
# local win = vim.api.nvim_get_current_win()
|
||||||
|
# local buf = vim.api.nvim_create_buf(true, true)
|
||||||
|
# vim.api.nvim_win_set_buf(win, buf)
|
||||||
|
# vim.api.nvim_command('read '..path)
|
||||||
|
# vim.fn.delete(path)
|
||||||
|
# end
|
||||||
|
|
||||||
|
|
||||||
def nvim_tcd [] {
|
def nvim_tcd [] {
|
||||||
[
|
[
|
||||||
{|before, after|
|
{|before, after|
|
||||||
|
@ -81,7 +112,7 @@ export def drop [] {
|
||||||
if 'NVIM' in (env).name {
|
if 'NVIM' in (env).name {
|
||||||
let c = $in
|
let c = $in
|
||||||
let temp = (mktemp -t nuvim.XXXXXXXX|str trim)
|
let temp = (mktemp -t nuvim.XXXXXXXX|str trim)
|
||||||
$c | save $temp
|
$c | save -f $temp
|
||||||
nvim --headless --noplugin --server $env.NVIM --remote-send $"<cmd>lua ReadTempDrop\('($temp)')<cr>"
|
nvim --headless --noplugin --server $env.NVIM --remote-send $"<cmd>lua ReadTempDrop\('($temp)')<cr>"
|
||||||
} else {
|
} else {
|
||||||
echo $in
|
echo $in
|
||||||
|
|
53
ssh/ssh.nu
53
ssh/ssh.nu
|
@ -1,3 +1,11 @@
|
||||||
|
export def 'str max-length' [] {
|
||||||
|
$in | reduce -f 0 {|x, a|
|
||||||
|
if ($x|is-empty) { return $a }
|
||||||
|
let l = ($x | str length)
|
||||||
|
if $l > $a { $l } else { $a }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export def index-need-update [index path] {
|
export def index-need-update [index path] {
|
||||||
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 }
|
||||||
|
@ -9,14 +17,6 @@ export def index-need-update [index path] {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
export def 'str max-length' [] {
|
|
||||||
$in | reduce -f 0 {|x, a|
|
|
||||||
if ($x|is-empty) { return $a }
|
|
||||||
let l = ($x | str length)
|
|
||||||
if $l > $a { $l } else { $a }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def "nu-complete ssh host" [] {
|
def "nu-complete ssh host" [] {
|
||||||
rg -LNI '^Host [a-z0-9_\-\.]+' ~/.ssh | lines | each {|x| $x | split row ' '| get 1}
|
rg -LNI '^Host [a-z0-9_\-\.]+' ~/.ssh | lines | each {|x| $x | split row ' '| get 1}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ def fmt-group [p] {
|
||||||
$p | str replace $"($env.HOME)/.ssh/" ''
|
$p | str replace $"($env.HOME)/.ssh/" ''
|
||||||
}
|
}
|
||||||
|
|
||||||
def "nu-complete ssh" [] {
|
def "ssh-hosts" [] {
|
||||||
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
|
let cache = $'($env.HOME)/.cache/nu-complete/ssh.json'
|
||||||
if index-need-update $cache ~/.ssh/**/* {
|
if index-need-update $cache ~/.ssh/**/* {
|
||||||
let data = (ssh-list | each {|x|
|
let data = (ssh-list | each {|x|
|
||||||
|
@ -68,11 +68,14 @@ def "nu-complete ssh" [] {
|
||||||
identfile: ($data.identfile | str max-length),
|
identfile: ($data.identfile | str max-length),
|
||||||
}
|
}
|
||||||
|
|
||||||
{max: $max, completion: $data} | save $cache
|
{max: $max, completion: $data} | save -f $cache
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = (cat $cache | from json)
|
cat $cache | from json
|
||||||
|
}
|
||||||
|
|
||||||
|
def "nu-complete ssh" [] {
|
||||||
|
let data = (ssh-hosts)
|
||||||
$data.completion
|
$data.completion
|
||||||
| each { |x|
|
| each { |x|
|
||||||
let uri = ($x.uri | str lpad -l $data.max.uri -c ' ')
|
let uri = ($x.uri | str lpad -l $data.max.uri -c ' ')
|
||||||
|
@ -96,3 +99,31 @@ export extern ssh [
|
||||||
-J: string # j
|
-J: string # j
|
||||||
-W: string # w
|
-W: string # w
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def "nu-complete scp" [cmd: string, offset: int] {
|
||||||
|
let argv = ($cmd | str substring [0 $offset] | split row ' ')
|
||||||
|
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
|
||||||
|
let ssh = (ssh-hosts | get completion
|
||||||
|
| each {|x| {value: $"($x.value):" description: $x.uri} }
|
||||||
|
)
|
||||||
|
let n = ($p | split row ':')
|
||||||
|
if $"($n | get 0):" in ($ssh | get value) {
|
||||||
|
^ssh ($n | get 0) $"sh -c 'ls -dp ($n | get 1)*'"
|
||||||
|
| lines
|
||||||
|
| each {|x| $"($n | get 0):($x)"}
|
||||||
|
} else {
|
||||||
|
let files = do -i {
|
||||||
|
ls -a $"($p)*"
|
||||||
|
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
|
||||||
|
}
|
||||||
|
$files | append $ssh
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export def scp [
|
||||||
|
lhs: string@"nu-complete scp",
|
||||||
|
rhs: string@"nu-complete scp"
|
||||||
|
] {
|
||||||
|
^scp -r $lhs $rhs
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue