blob: 638a1d28ef432f2308b0bad798383947aed54db0 [file] [log] [blame]
garciadeblas8d8cd992024-05-21 16:04:14 +02001#######################################################################################
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
19RED='\033[0;31m'
20GREEN='\033[0;32m'
21BLUE='\033[0;34m'
22CYAN='\033[0;36m'
23RESET='\033[0m'
24
25# Colored messages (blue is the default)
26# Examples:
27# m "hello world"
28# m "hello world" "$GREEN"
29function m() {
30 local COLOR=${2:-$BLUE}
31 echo -e "$COLOR$1$RESET"
32}
33
34function copy_function() {
35 local ORIG_FUNC=$(declare -f $1)
36 local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
37 eval "$NEWNAME_FUNC"
38}
39
40function 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
52function 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
63function 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
73function 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
80function 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}