Federico Ressi | 71bda86 | 2018-05-28 11:38:56 +0200 | [diff] [blame] | 1 | # Generic use functions |
| 2 | |
| 3 | # ensure we don't re-source this in the same environment |
| 4 | [[ -z "$_NEUTRON_TEMPEST_PLUGIN_FUNCTIONS" ]] || return 0 |
| 5 | declare -r -g _NEUTRON_TEMPEST_PLUGIN_FUNCTIONS=1 |
| 6 | |
| 7 | # Create a function copying the code from an existing one |
| 8 | function save_function { |
| 9 | local old_name=$1 |
| 10 | local new_name=$2 |
| 11 | |
| 12 | # Saving the same function again after redefining it could produce a |
| 13 | # recorsive function in case for example this plugin is sourced twice |
| 14 | if type -t "${new_name}"; then |
| 15 | # Prevent copying the same function twice |
| 16 | return 0 |
| 17 | fi |
| 18 | |
| 19 | # Save xtrace setting |
| 20 | _XTRACE_FUNCTIONS=$(set +o | grep xtrace) |
| 21 | set +o xtrace |
| 22 | |
| 23 | # Get code of the original function |
| 24 | local old_code=$(declare -f ${old_name}) |
| 25 | # Produce code for the new function |
| 26 | local new_code="${new_name}${old_code#${old_name}}" |
| 27 | # Define the new function |
| 28 | eval "${new_code}" |
| 29 | |
| 30 | # Restore xtrace |
| 31 | $_XTRACE_FUNCTIONS |
| 32 | } |