increase portability by getting rid of declare

This commit is contained in:
Patrick Stadler
2015-03-15 21:04:06 +01:00
parent 7a43a2e21c
commit 6f8e87208c
4 changed files with 42 additions and 58 deletions

View File

@@ -1,34 +1,6 @@
#!/bin/sh
# this is bad, but `declare -f -F` is not portable
is_function () {
declare -f -F $1 > /dev/null; return $?
}
# http://stackoverflow.com/a/1369211/183097
copy_function () {
is_function $1 || return 1
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
}
# if `type declare | grep -q 'shell builtin$'`; then
# is_function () {
# declare -f -F $1 > /dev/null; return $?
# }
# # http://stackoverflow.com/a/1369211/183097
# copy_function () {
# is_function $1 || return 1
# eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
# }
# # `declare` is non-posix, provide alternative
# else
# is_function () {
# type $1 | grep -q 'shell function$'
# }
# copy_function () {
# is_function $1 || return 1
# echo "${2}() { ${1} }"
# }
# fi
type $1 2> /dev/null | grep -q 'function$'
}

26
lib/utils/loader.sh Normal file
View File

@@ -0,0 +1,26 @@
#!/bin/sh
load_metric_with_prefix () {
local prefix=$1
local file=$2
local content=$(sed \
-e "s/^\s*\(init[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(collect[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(terminate[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(docs[ ]*()[ ]*{\)/${prefix}\1/" $file)
eval "$content"
}
load_reporter_with_prefix () {
local prefix=$1
local file=$2
local content=$(sed \
-e "s/^\s*\(init[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(report[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(terminate[ ]*()[ ]*{\)/${prefix}\1/" \
-e "s/^\s*\(docs[ ]*()[ ]*{\)/${prefix}\1/" $file)
eval "$content"
}