| garciadeblas | 8d8cd99 | 2024-05-21 16:04:14 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | |
| 19 | RED='\033[0;31m' |
| 20 | GREEN='\033[0;32m' |
| 21 | BLUE='\033[0;34m' |
| 22 | CYAN='\033[0;36m' |
| 23 | RESET='\033[0m' |
| 24 | |
| 25 | # Colored messages (blue is the default) |
| 26 | # Examples: |
| 27 | # m "hello world" |
| 28 | # m "hello world" "$GREEN" |
| 29 | function m() { |
| 30 | local COLOR=${2:-$BLUE} |
| 31 | echo -e "$COLOR$1$RESET" |
| 32 | } |
| 33 | |
| 34 | function copy_function() { |
| 35 | local ORIG_FUNC=$(declare -f $1) |
| 36 | local NEWNAME_FUNC="$2${ORIG_FUNC#$1}" |
| 37 | eval "$NEWNAME_FUNC" |
| 38 | } |
| 39 | |
| 40 | function replace_text() { |
| 41 | local FILE=$1 |
| 42 | local START=$2 |
| 43 | local END=$3 |
| 44 | local NEW=$4 |
| 45 | local T=$(mktemp) |
| 46 | head -n $((START-1)) "$FILE" > "$T" |
| 47 | echo "$NEW" >> "$T" |
| 48 | tail -n +$((END+1)) "$FILE" >> "$T" |
| 49 | mv "$T" "$FILE" |
| 50 | } |
| 51 | |
| 52 | function insert_text() { |
| 53 | local FILE=$1 |
| 54 | local START=$2 |
| 55 | local NEW=$3 |
| 56 | local T=$(mktemp) |
| 57 | head -n $((START-1)) "$FILE" > "$T" |
| 58 | echo "$NEW" >> "$T" |
| 59 | tail -n +$START "$FILE" >> "$T" |
| 60 | mv "$T" "$FILE" |
| 61 | } |
| 62 | |
| 63 | function remove_text() { |
| 64 | local FILE=$1 |
| 65 | local START=$2 |
| 66 | local END=$3 |
| 67 | local T=$(mktemp) |
| 68 | head -n $((START-1)) "$FILE" > "$T" |
| 69 | tail -n +$((END+1)) "$FILE" >> "$T" |
| 70 | mv "$T" "$FILE" |
| 71 | } |
| 72 | |
| 73 | function envsubst_cp() { |
| 74 | local FROM_FILE=$1 |
| 75 | local TO_FILE=$2 |
| 76 | mkdir --parents "$(dirname "$TO_FILE")" |
| 77 | cat "$FROM_FILE" | envsubst > "$TO_FILE" |
| 78 | } |
| 79 | |
| 80 | function envsubst_dir() { |
| 81 | local FROM_DIR=$1 |
| 82 | local TO_DIR=$2 |
| 83 | rm --recursive --force "$TO_DIR" |
| 84 | mkdir --parents "$TO_DIR" |
| 85 | pushd "$FROM_DIR" > /dev/null |
| 86 | local F |
| 87 | find . -type f | while read F; do |
| 88 | envsubst_cp "$F" "$TO_DIR/$F" |
| 89 | done |
| 90 | popd > /dev/null |
| 91 | } |