summaryrefslogtreecommitdiffstats
path: root/helper
diff options
context:
space:
mode:
authorJonathan Bauer2013-06-06 18:46:46 +0200
committerJonathan Bauer2013-06-06 18:46:46 +0200
commitd59a9c219fcbd0c77dbdbde6760db72fc0e8249a (patch)
tree356e3854a0781550d269dc96f21df1b93b529bc0 /helper
parent[binutil.inc] added commentaries for the functions searching for the dynamic ... (diff)
parent[helper] Add key-value util (diff)
downloadtm-scripts-d59a9c219fcbd0c77dbdbde6760db72fc0e8249a.tar.gz
tm-scripts-d59a9c219fcbd0c77dbdbde6760db72fc0e8249a.tar.xz
tm-scripts-d59a9c219fcbd0c77dbdbde6760db72fc0e8249a.zip
Merge branch 'master' of git:openslx-ng/tm-scripts
Diffstat (limited to 'helper')
-rw-r--r--helper/keyvalueutil.inc32
-rw-r--r--helper/string.inc5
2 files changed, 37 insertions, 0 deletions
diff --git a/helper/keyvalueutil.inc b/helper/keyvalueutil.inc
new file mode 100644
index 00000000..a0a89db7
--- /dev/null
+++ b/helper/keyvalueutil.inc
@@ -0,0 +1,32 @@
+# Helper file for managing key-value containing files
+# There are some specialized conveinience functions here first
+# that mostly just pass a predefined filename to the genric function
+# at the end
+
+# Add the given environment variable to /etc/environment
+add_env () {
+ [ $# -ne 2 ] && perror "Usage: $0 'key' 'value'"
+ [ -z "$1" ] && perror "$0: Empty key!"
+ add_key_value "/etc/environment" "$1" "$2"
+}
+
+#
+# Adds the given key-value-pair to a given file
+# The file will be relative to the current target build dir,
+# even if it starts with a slash.
+# Will perror if the key already exists with a different value
+add_key_value () {
+ [ $# -ne 3 ] && perror "Usage: $0 'file' 'key' 'value'"
+ [ -z "$TARGET_BUILD_DIR" ] && perror "No TARGET_BUILD_DIR set. Aborting for safety."
+ local FILE="$TARGET_BUILD_DIR/$1"
+ local KEY="$2"
+ local VALUE="$(echo "$3" | sed "s/'/\\\\'/g")" # \\\\\\\\\\\\\\\\\\\\\\ßß
+ if [ -s "$FILE" ]; then
+ local CURRENT="$(grep -E "^\s*$KEY=.*$" "$FILE" | awk -F '=' '{$1=""; printf $0}' | itrim)"
+ [ -n "$CURRENT" -a "'$VALUE'" != "$CURRENT" ] && perror "Cannot set $KEY to '$VALUE' as it is already set to $CURRENT"
+ [ -n "$CURRENT" ] && return 0
+ fi
+ mkdir -p "$(dirname "$FILE")"
+ echo "$KEY='$VALUE'" >> "$FILE"
+}
+
diff --git a/helper/string.inc b/helper/string.inc
index 9f18e4e0..04809a93 100644
--- a/helper/string.inc
+++ b/helper/string.inc
@@ -7,6 +7,11 @@ trim() {
echo -n "$var"
}
+# Inline version of trim, use when piping
+itrim () {
+ sed -r 's/^\s+//g;s/\s+$//g'
+}
+
# usage: CANONICALIZED_STRING=$(canonalize <path>)
# usage with relative path requires you to be in the correct directory.
canonicalize() {