-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml-elastic.sh
98 lines (89 loc) · 3 KB
/
yaml-elastic.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
# shellcheck disable=SC2148
# Tool for extracting information from an elasticsearch-style yaml file.
# Requires @kislyuk's yq (the one from python-pip, not the other one)
_KISLYUK_YQ=/usr/local/bin/yq
if [[ ! -x $_KISLYUK_YQ ]] || ! $_KISLYUK_YQ --help | grep -q kislyuk/yq; then
echo "yaml-elastic requires https://github.com/kislyuk/yq under /usr/local/bin" >&2
echo "This can be installed using python3-pip" >&2
exit 1
fi
yaml-expand-forms() { (
use strict
use utils
# recursively enumerate all the collapsed forms of a json entity
# we leave out the outermost double quotes at each stage, to save time
# adding and then re-stripping them at each recursion stage
# this means that the final output is missing its outermost double quotes,
# so we have to add them afterwards
local head=${1%%.*}
local tail=${1#*.}
if [[ "$tail" != "$1" ]]; then
for tail_form in $(yaml-expand-forms "$tail"); do
echo "$head\".\"$tail_form"
echo "$head.$tail_form"
done
else
echo "$head"
fi
) }
yaml-extract() { (
use strict
use utils
# perform a recursive tree search for all collapsed forms of the search term
local key="$1"
local file="$2"
local result=null
for searchterm in $(yaml-expand-forms "$key"); do
# remember to add the double quotes, see expand_forms above
result=$($_KISLYUK_YQ ".\"$searchterm\"" "$file")
if [[ "$result" != null ]]; then
# Strip quotes from the value
result="${result#\"}"
result="${result%\"}"
echo "$result"
return
fi
done
echo
) }
yaml-replace() { (
use strict
use utils
# perform a recursive tree search for all collapsed forms of the search term
local key=${1%%=*}
local value="${1#*=}"
local file=$2
local result=null
local modified=
for searchterm in $(yaml-expand-forms "$key"); do
# remember to add the double quotes, see expand_forms above
result=$($_KISLYUK_YQ ".\"$searchterm\"" "$file")
if [[ "$result" != null ]]; then
cp "$file" "${file}.bak"
$_KISLYUK_YQ -y ".\"$searchterm\" |= \"$value\"" "${file}.bak" > "$file"
modified=true
break
fi
done
if [[ ! $modified && "${3:-}" == "or-add" ]]; then
cp "$file" "${file}.bak"
$_KISLYUK_YQ -y ".\"$key\" |= \"$value\"" "${file}.bak" > "$file"
fi
) }
yaml-delete() { (
use strict
use utils
# perform a recursive tree search for all collapsed forms of the search term
local key=$1
local file=$2
local result=null
for searchterm in $(yaml-expand-forms "$key"); do
# remember to add the double quotes, see expand_forms above
result=$($_KISLYUK_YQ ".\"$searchterm\"" "$file")
if [[ "$result" != null ]]; then
cp "$file" "${file}.bak"
$_KISLYUK_YQ -y "del(.\"$searchterm\")" "${file}.bak" > "$file"
return
fi
done
) }