forked from pcdshub/shared-dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.sh
343 lines (303 loc) · 8.36 KB
/
helpers.sh
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
#
# This file contains some helper scripts for your dotfiles to reuse.
#
# Certain functions were borrowed, adapted, and rewritten from other sources,
# including:
# * https://github.com/pcdshub/epics-setup
if [ -z "$_PCDS_CONDA_FOR_UTILS" ]; then
_PCDS_CONDA_FOR_UTILS=/cds/group/pcds/pyps/conda/py39/envs/pcds-5.3.0/
fi
# shellcheck disable=SC2034
_ALL_IOCS_TEXT=/cds/data/iocData/.all_iocs/iocs.txt
_ALL_IOCS_JSON=/cds/data/iocData/.all_iocs/iocs.json
# _helper_cd_verbose
# Change directory to the provided path and let the user know about it.
# Also adds the path to the user's history.
# Usage: pythonpathmunge (dirname)
_helper_cd_verbose() {
local new_path
new_path="$1"
if [ -z "$new_path" ]; then
return
fi
cd "$new_path" || return
history -s "cd \"$new_path\""
echo "Old directory: $OLDPWD"
echo "Working directory: $PWD"
echo "Tip: use 'cd -' to go back."
}
# _helper_pick_directory:
# Use a fuzzy finder to pick a directory underneath the provided path.
#
# Usage: _helper_pick_directory [path] [query string]
_helper_pick_directory() {
local new_directory
local starting_dir
local query_string
starting_dir=$1
shift
query_string="$*"
if [ -z "$starting_dir" ]; then
echo "No starting directory was provided" > /dev/stderr
return
fi
if [ -n "$query_string" ]; then
query_args=("-q" "$query_string")
else
query_args=()
fi
local fzf
fzf=$(_helper_find_fzf)
if [ -z "$fzf" ]; then
echo "Sorry, fzf was not found." > /dev/stderr
return
fi
find "$starting_dir" -maxdepth 1 -type d 2> /dev/null | $fzf +m "${query_args[@]}"
}
# _helper_cd_under:
# CD to a directory underneath the provided one, filtered by fzf
#
# Usage: _helper_cd_under [path] [query string]
_helper_cd_under() {
local new_directory
new_directory=$(_helper_pick_directory "$@")
if [ -n "$new_directory" ]; then
echo "cd \"$new_directory\""
history -s "cd \"$new_directory\""
cd "$new_directory" || return
fi
}
# _helper_find_jq
# Find the JSON query tool, jq, if available.
_helper_find_jq() {
local jq
jq=$(command -v jq 2>/dev/null)
if [ -z "$jq" ]; then
jq="${_PCDS_CONDA_FOR_UTILS}/bin/jq"
if [ ! -x "$jq" ]; then
jq=""
fi
fi
echo "$jq"
}
# _helper_find_fzf
# Find the fuzzy finder, fzf, if available.
_helper_find_fzf() {
local fzf
fzf=$(command -v fzf 2>/dev/null)
if [ -z "$fzf" ]; then
fzf="${_PCDS_CONDA_FOR_UTILS}/bin/fzf"
if [ ! -x "$fzf" ]; then
fzf=""
fi
fi
echo "$fzf"
}
# _helper_get_ioc_info
# Get IOC information in JSON format.
# Usage: _helper_get_ioc_info_json (ioc_name)
# Example: _helper_get_ioc_info_json ioc-lfe-motion
# Example: _helper_get_ioc_info_json ioc-lfe-motion | jq ".config_file"
_helper_get_ioc_info_json() {
local ioc_name
ioc_name=$1
if [ -z "$ioc_name" ]; then
echo "No IOC specified" > /dev/stderr
return
fi
local jq
jq=$(_helper_find_jq)
if [ -x "$jq" ]; then
$jq ".[] | select(.name == \"$ioc_name\")" < $_ALL_IOCS_JSON
else
echo "Sorry, jq is unavailable" 2>/dev/stderr
fi
}
# _helper_get_hosts_json
# Get JSON information about one or all hosts in LDAP (same source as netconfig)
# Usage: _helper_get_host_info_json [hostname]
# Example: _helper_get_host_info_json
# Example: _helper_get_host_info_json zygo01
_helper_get_host_info_json() {
local jq
local hostname
local all_host_metadata
jq=$(_helper_find_jq)
if [ ! -x "$jq" ]; then
echo "Sorry, jq is unavailable" 2>/dev/stderr
return
fi
hostname=$1
# shellcheck disable=SC2016
all_host_metadata=$(
"${_PCDS_CONDA_FOR_UTILS}/bin/python" -m whatrecord.plugins.netconfig |
$jq '.metadata | [ to_entries[] | .key as $name | .value + { "name": $name } ]'
)
if [ -z "$all_host_metadata" ]; then
echo "Sorry, something went wrong with whatrecord's netconfig plugin" 2>/dev/stderr
return
fi
if [ -z "$hostname" ]; then
# All hosts
echo "$all_host_metadata"
else
echo "$all_host_metadata" | $jq 'map(select(.name=='"\"$hostname\""'))'
fi
}
# _helper_get_hosts_table
# Get table for all hosts in LDAP (same source as netconfig)
# Usage: _helper_get_host_info_table [columns]
# Example: _helper_get_host_info_table name description
_helper_get_host_info_table() {
local host_md
local -a columns
host_md=$(_helper_get_host_info_json "")
if [[ -z "$host_md" || -z "$dotfiles" ]]; then
return
fi
if [ $# -eq 0 ]; then
columns=( "name" "description" "ipHostNumber" )
else
columns=( "$@" )
fi
"${_PCDS_CONDA_FOR_UTILS}/bin/python" "$dotfiles/json_to_table.py" \
"${columns[@]}" < <(echo "$host_md")
}
# _helper_remove_from_list
# Remove an item from a list *and* deduplicate the list.
# Usage: _helper_remove_from_list (to_remove) (from) [delimiter ":"]
_helper_remove_from_list () {
local to_remove
local value
local delimiter
local seen
local -a valid_items
local IFS
[ $# -lt 2 ] && return;
to_remove="$1"
value="$2"
delimiter="${3-:}"
valid_items=()
while IFS= read -r item; do
seen=0
for other in "${valid_items[@]}"; do
if [[ "$other" == "$item" ]]; then
seen=1
fi
done
if [[ seen -eq 0 && "$item" != "$to_remove" && "$item" != "${to_remove}/" && "$item" != "" ]]; then
valid_items+=("$item")
fi
done < <(echo "${value}${delimiter}" | tr "${delimiter}" "\n" )
IFS=":"
echo "${valid_items[*]}"
}
# _helper_maybe_get_hutch
# If unspecified, get the current hutch.
# Usage: _helper_maybe_get_hutch [hutchname]
_helper_maybe_get_hutch() {
if [ -z "$1" ]; then
get_hutch_name;
else
echo "$1";
fi
}
# _helper_readlink
# Portable 'readlink -f' alternative for directories which works on macOS/Linux
# Usage: _helper_readlink pathname
_helper_readlink() {
(cd "$1" && pwd -P) 2>/dev/null || echo "$1"
}
# pathpurge
# Remove a path from ${PATH}
# Usage: pathpurge (dirname) [dirname ...]
pathpurge()
{
if [ $# -eq 0 ] ; then
echo "Usage: Usage: pathpurge dirname [dirname ...]"
return
fi
local to_remove
local canonical
while [ $# -gt 0 ] ;
do
to_remove="$1"
if [[ "$to_remove" == "." || "$to_remove" == ".." ]] ; then
to_remove=$( _helper_readlink "$to_remove" )
fi
# Remove the path as given
PATH=$(_helper_remove_from_list "$to_remove" "$PATH" ":")
canonical=$(_helper_readlink "$to_remove")
if [[ "$canonical" != "$to_remove" ]]; then
# Remove the canonical path as well
PATH=$(_helper_remove_from_list "$canonical" "$PATH" ":")
fi
shift
done
}
# pathmunge
# Add a path to the beginning of ${PATH}
# Usage: pathmunge (dirname)
pathmunge()
{
local to_add
to_add="$1"
if [ "$to_add" == "" ] ; then
echo "Usage: pathmunge dirname"
return
fi
if [ ! -d "$to_add" ] ; then
echo "pathmunge: $to_add is not a directory"
return
fi
to_add=$(_helper_readlink "$to_add")
if [ "$PATH" == "" ] ; then
export PATH=$to_add
return
fi
pathpurge "$to_add"
export PATH=$to_add:$PATH
}
# pythonpathpurge
# Remove a path from ${PYTHONPATH}
# Usage: pythonpathpurge (dirname)
pythonpathpurge()
{
if [ $# -eq 0 ] ; then
echo "Usage: Usage: pythonpathpurge dirname [dirname ...]"
return
fi
while [ $# -gt 0 ] ;
do
to_remove="$1"
if [[ "$to_remove" == "." || "$to_remove" == ".." ]] ; then
to_remove=$( _helper_readlink "$to_remove" )
fi
PYTHONPATH=$(_helper_remove_from_list "$to_remove" "$PYTHONPATH" ":")
shift
done
}
# pythonpathmunge
# Add a path to the beginning of ${PYTHONPATH}
# Usage: pythonpathmunge (dirname)
pythonpathmunge()
{
local to_add
to_add="$1"
if [ "$to_add" == "" ] ; then
echo "Usage: pythonpathmunge dirname"
return
fi
if [ ! -d "$to_add" ] ; then
echo "pythonpathmunge: $to_add is not a directory"
return
fi
to_add=$(_helper_readlink "$to_add")
if [ "$PYTHONPATH" == "" ] ; then
export PYTHONPATH=$to_add
return
fi
pathpurge "$to_add"
export PYTHONPATH=$to_add:$PYTHONPATH
}