-
Notifications
You must be signed in to change notification settings - Fork 2
/
introspection.sh
89 lines (82 loc) · 1.67 KB
/
introspection.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
#
# introspection
#
# enable execution of internal functions from CLI
#
# some_big_script int_call NAME args
#
# will call only NAME() and exit
#
# usage example:
#
# 1. script with two subfunctions f and b
# f() {
# }
# b() {
# #
# introspection_call "$*"
#
# 1. script with introspection for internals
#
# while [ -n "$1" ] ; then
# maybe_invoke_introspection "$@"
# shift
# done
# will accept following invocation:
# script --intcall FUNC_NAME args
# where FUNC_NAME is private script function
#
invoke_introspection()
{
local function="$1"
if ! declare -f "$function" > /dev/null ; then
(
if [ -n "$function" ] ; then
echo "$0: invoke_introspection: $function not found"
fi
echo "$0: available functions:"
declare -F | awk '{print " ",$3}'
) >&2
exit 1
fi
shift
"$function" "$@"
return $?
}
list_functions()
{
declare -F | awk '{print $3}'
}
maybe_invoke_introspection()
{
if [ "$1" == "--int-call" ] ; then
shift
invoke_introspection "$@"
exit $?
fi
}
variable_exists()
## variable_exists NAME
##
## checks if variable NAME is set and has non-empty
## value
## (useful for dynamically created variable names)
{
[ -n "$(variable_get "$1")" ]
}
variable_set()
## variable_set NAME VALUE
##
## sets variable NAME to value VALUE
## (useful for dynamically created variable names)
{
eval "$1='$2'"
}
variable_get()
## variable_get NAME
##
## outputs VALUE of variable NAME
## (useful for dynamically created variable names)
{
eval echo "\$""$1"
}