blob: 4eaf14de396471cc822a82b270b795a0f81160f9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#!/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
local secret
# Handle opts
while getopts ":p:s" opt
do
case "$opt" in
p) export BASHTARD_PLAYBOOK="$OPTARG" ;;
s) secret=1 ;;
*) emerg "Unused opt '$opt'?" ;;
esac
done
shift $(( OPTIND - 1 ))
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
if [[ $secret ]]
then
kvfile="$BASHTARD_ETCDIR/secrets"
else
kvfile="$BASHTARD_ETCDIR/hosts.d/${BASHTARD_PLATFORM[fqdn]}"
fi
buffer="$(tmpfile)"
grep -v "^$key=" < "$kvfile" > "$buffer"
printf "%s=%s\n" "$key" "$value" >> "$buffer"
sort "$buffer" > "$kvfile"
}
|