forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.nu
89 lines (80 loc) · 2.18 KB
/
utils.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export def ensure-cache-by-lines [cache path action] {
let ls = do -i { open $path | lines | length }
if ($ls | is-empty) { return false }
let lc = do -i { open $cache | get lines}
if not (($cache | path exists) and ($lc | is-not-empty) and ($ls == $lc)) {
mkdir ($cache | path dirname)
{
lines: $ls
payload: (do $action)
} | save -f $cache
}
(open $cache).payload
}
export def normalize-column-names [ ] {
let i = $in
let cols = $i | columns
mut t = $i
for c in $cols {
$t = ($t | rename -c {$c: ($c | str downcase | str replace ' ' '_')})
}
$t
}
export def --wrapped with-flag [...flag] {
if ($in | is-empty) { [] } else { [...$flag $in] }
}
export def `kcache flush` [] {
rm -rf ~/.cache/nu-complete/k8s/
nu-complete kube ctx
rm -rf ~/.cache/nu-complete/k8s-api-resources/
}
export def kube-shortnames [] {
kubectl api-resources | from ssv -a
| where SHORTNAMES != ''
| reduce -f {} {|i,a|
$i.SHORTNAMES
| split row ','
| reduce -f {} {|j,b|
$b | insert $j $i.NAME
}
| merge $a
}
}
export def parse_cellpath [path] {
$path | split row '.' | each {|x|
if ($x | find --regex "^[0-9]+$" | is-empty) {
$x
} else {
$x | into int
}
}
}
export def get_cellpath [record path] {
$path | reduce -f $record {|it, acc| $acc | get $it }
}
export 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
}
}
export 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
}
}