1
|
# This file is part of Haketilo
|
2
|
#
|
3
|
# Copyright (C) 2021, Wojtek Kosior
|
4
|
#
|
5
|
# This program is free software: you can redistribute it and/or modify
|
6
|
# it under the terms of the CC0 1.0 Universal License as published by
|
7
|
# the Creative Commons Corporation.
|
8
|
#
|
9
|
# This program is distributed in the hope that it will be useful,
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
# CC0 1.0 Universal License for more details.
|
13
|
|
14
|
ENDL="
|
15
|
"
|
16
|
|
17
|
# A "raw" echo, interprets neither backclash escapes nor command-line options.
|
18
|
# Does not emit trailing newline.
|
19
|
ech() {
|
20
|
printf %s "$*"
|
21
|
}
|
22
|
|
23
|
errcho() {
|
24
|
echo "$@" >&2
|
25
|
}
|
26
|
|
27
|
map_set_instr() {
|
28
|
echo "$1__$2='$3'"
|
29
|
}
|
30
|
|
31
|
map_set() {
|
32
|
eval "$(map_set_instr "$@")"
|
33
|
}
|
34
|
|
35
|
map_set_export() {
|
36
|
eval "export $(map_set_instr "$@")"
|
37
|
}
|
38
|
|
39
|
map_get() {
|
40
|
eval "echo \"\$$1__$2\""
|
41
|
}
|
42
|
|
43
|
map_del_instr() {
|
44
|
echo "unset $1__$2"
|
45
|
}
|
46
|
|
47
|
map_del() {
|
48
|
eval "$(map_del_instr "$@")"
|
49
|
}
|
50
|
|
51
|
sanitize() {
|
52
|
echo "$1" | tr /.- _
|
53
|
}
|
54
|
|
55
|
escape_regex_special() {
|
56
|
ech "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
|
57
|
}
|
58
|
|
59
|
# Note: We don't actually parse JSON. We extract needed keys with sed regexes
|
60
|
# which does not work in the general case but is sufficient for now.
|
61
|
get_json_key() {
|
62
|
local KEY_REG="$(escape_regex_special "$1")"
|
63
|
ech "$2" |
|
64
|
sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' |
|
65
|
grep . | head -1
|
66
|
}
|