-
Notifications
You must be signed in to change notification settings - Fork 1
/
joinTheDots
executable file
·62 lines (47 loc) · 1.12 KB
/
joinTheDots
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
58
59
60
61
62
#! /bin/bash
set -euo pipefail
rendertemplate() {
python3 -c '
import os
import sys
import yaml
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader("dot"))
template = env.from_string(sys.stdin.read())
hostname = os.uname()[1]
with open("machine/default.yaml", "r") as f:
machine = yaml.load(f)
try:
with open("machine/{}.yaml".format(hostname), "r") as f:
machine.update(yaml.load(f))
except FileNotFoundError:
pass
print(template.render(**{
"host": hostname,
"machine": machine,
"env": os.environ,
}))
'
}
cd "$(readlink -f .)"
find dot -type f -o -type l | while read -r f; do
if [[ "$f" =~ \.j2$ ]]; then
f="${f%.j2}"
rendered="rendered/${f#dot/}"
mkdir -p "$(dirname "$rendered")"
rendertemplate < "$f.j2" > "$rendered"
target="$(readlink -f "$rendered")"
else
target="$(readlink -f "$f")"
fi
location="$HOME/.${f#dot/}"
if [[ -h "$location" ]]; then
rm "$location"
elif [[ -e "$location" ]]; then
echo "$location exists and is not a symlink, skipping!"
continue
fi
mkdir -p "$(dirname "$location")"
echo "$location => $f"
ln -s "$target" "$location"
done