aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2022-05-04 12:45:57 +0200
committerPatrick Spek <p.spek@tyil.nl>2022-05-04 12:45:57 +0200
commit53a8fac1ce2454461e98d17b4543a493a1b286d1 (patch)
treeae97150eafabaabb041f75037d42b3ea70f50a0f
parentcf67933256a99fbf26337c1924d7ecc942bf91c0 (diff)
Add var subcommand
-rw-r--r--lib/main.bash2
-rw-r--r--lib/subcommands/var.bash40
2 files changed, 42 insertions, 0 deletions
diff --git a/lib/main.bash b/lib/main.bash
index b5d6cc3..42ce373 100644
--- a/lib/main.bash
+++ b/lib/main.bash
@@ -81,6 +81,7 @@ Usage:
$BASHTARD_NAME ssh <command>
$BASHTARD_NAME sync [playbook]
$BASHTARD_NAME sysinfo
+ $BASHTARD_NAME var <key> [value]
Perform maintenance on your infra.
@@ -92,6 +93,7 @@ Commands:
sync Pull latest changes through git, and synchronize all added
playbooks.
sysinfo Show gathered information about this system.
+ var Show or set the value of a given configuration key.
Playbooks:
EOF
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 <p.spek@tyil.nl>
+#
+# 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"
+}