github.com/ryanhartje/helm@v3.0.0-beta.3+incompatible/scripts/util.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright The Helm Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -euo pipefail 18 19 # Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG 20 # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal 21 kube::util::trap_add() { 22 local trap_add_cmd 23 trap_add_cmd=$1 24 shift 25 26 for trap_add_name in "$@"; do 27 local existing_cmd 28 local new_cmd 29 30 # Grab the currently defined trap commands for this trap 31 existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'` 32 33 if [[ -z "${existing_cmd}" ]]; then 34 new_cmd="${trap_add_cmd}" 35 else 36 new_cmd="${existing_cmd};${trap_add_cmd}" 37 fi 38 39 # Assign the test 40 trap "${new_cmd}" "${trap_add_name}" 41 done 42 } 43 44 # Opposite of kube::util::ensure-temp-dir() 45 kube::util::cleanup-temp-dir() { 46 rm -rf "${KUBE_TEMP}" 47 } 48 49 # Create a temp dir that'll be deleted at the end of this bash session. 50 # 51 # Vars set: 52 # KUBE_TEMP 53 kube::util::ensure-temp-dir() { 54 if [[ -z ${KUBE_TEMP-} ]]; then 55 KUBE_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kubernetes.XXXXXX) 56 kube::util::trap_add kube::util::cleanup-temp-dir EXIT 57 fi 58 }