Skip to content

Commit 21ae6e6

Browse files
committed
add file_fill_template function
1 parent afa3e0d commit 21ae6e6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

modules/bash-commons/src/file.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
set -e
55

66
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/os.sh"
7+
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/string.sh"
78

89
# Returns true (0) if the given file exists and is a file and false (1) otherwise
910
function file_exists {
@@ -63,3 +64,21 @@ function file_replace_or_append_text {
6364
file_append_text "$replacement_text" "$file"
6465
fi
6566
}
67+
68+
# Replace a specific template string in a file with a value. Provided as an array of TEMPLATE-STRING=VALUE
69+
function file_fill_template {
70+
local -r file="$1"
71+
shift 1
72+
local -ar auto_fill=("$@")
73+
74+
if [[ -z "${auto_fill[@]}" ]]; then
75+
log_info "No auto-fill params specified."
76+
return
77+
fi
78+
79+
for param in "${auto_fill[@]}"; do
80+
local -r name="$(string_strip_suffix "$param" "=*")"
81+
local -r value="$(string_strip_prefix "$param" "*=")"
82+
file_replace_text "$name" "$value" "$file"
83+
done
84+
}

0 commit comments

Comments
 (0)