From 53a8fac1ce2454461e98d17b4543a493a1b286d1 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 May 2022 12:45:57 +0200 Subject: Add var subcommand --- lib/subcommands/var.bash | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/subcommands/var.bash (limited to 'lib/subcommands') diff --git a/lib/subcommands/var.bash b/lib/subcommands/var.bash new file mode 100644 index 0000000..60bfe81 --- /dev/null +++ b/lib/subcommands/var.bash @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# SPDX-FileCopyrightText: 2022 Patrick Spek +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +subcommand() +{ + local key + local value + + # TODO: Handle opts + + key="$1" ; shift + value="$1" ; shift + + if [[ -z "$key" ]] + then + emerg "bashtard/var" "You must supply a key" + return 3 + fi + + if [[ -z "$value" ]] + then + # Just show the value of the given key + printf "%s=%s\n" "$key" "$(config "$key")" + return + fi + + # Set the var for this specific host + local kvfile + local buffer + + kvfile="$BASHTARD_ETCDIR/hosts.d/${BASHTARD_PLATFORM[fqdn]}" + buffer="$(tmpfile)" + + grep -v "^$key=" < "$kvfile" > "$buffer" + printf "%s=%s\n" "$key" "$value" >> "$buffer" + sort "$buffer" > "$kvfile" +} -- cgit v1.1