mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-11-10 05:59:41 -05:00
increase portability by getting rid of declare
This commit is contained in:
@@ -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
26
lib/utils/loader.sh
Normal 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"
|
||||
}
|
||||
Reference in New Issue
Block a user