-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfiles.sh
executable file
·147 lines (121 loc) · 4.08 KB
/
dotfiles.sh
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# config
VAULT_SECRET_DIR="$HOME/.config/ansible-vault"
VAULT_SECRET="$VAULT_SECRET_DIR/vault.secret"
REPO_DIR="$HOME/git/dotfiles"
CONFIG_DIR="$REPO_DIR/.config"
LOG_FILE="$REPO_DIR/dotfiles.log"
IS_FIRST_RUN="$CONFIG_DIR/.has_run"
DOTFILE_REPO_URL='https://github.com/jonathanchancey/dotfiles'
# gum defaults
export GUM_INPUT_CURSOR_FOREGROUND="#b57edc"
export GUM_INPUT_PROMPT_FOREGROUND="#b57edc"
export GUM_INPUT_PROMPT="* "
export GUM_INPUT_WIDTH=80
# check if gum is installed
command -v gum >/dev/null 2>&1 || {
echo >&2 "Please install gum using your package manager"; echo "more details https://github.com/charmbracelet/gum?tab=readme-ov-file#installation";
exit 1;
}
color_text() {
text=$1
gum style --foreground "#b57edc" "$text"
}
print_env_check() {
clear
gum style \
--border normal \
--margin "1" \
--padding "1" \
--foreground "#b57edc" \
--border-foreground "" "dotfiles bootstrap"
echo "operating system: $(grep '^NAME=' /etc/os-release | cut -d\" -f2)"
echo "user: $USER"
echo "home: $HOME"
# print ansible version
echo "ansible version: $(ansible-playbook --version | head -n1 | sed 's/.*\[\(.*\)\].*/\1/')"
# check if vault secret file exists with correct perms
if [ -s "$VAULT_SECRET" ]; then
# if exists, continue
if [ "$(stat -c "%a" "$VAULT_SECRET")" -eq 600 ]; then
echo "vault secret: :white_check_mark:" | gum format -t emoji
else
echo "vault secret: :heavy_check_mark: (does not have 600 permissions)" | gum format -t emoji
export VAULT_SECRET_WRONG_PERMS=true
fi
else
echo "vault secret: :x: (missing)" | gum format -t emoji
export VAULT_SECRET_MISSING=true
fi
# check if repo exists
if [ -d "$REPO_DIR" ]; then
echo "dotfiles repo: :white_check_mark:" | gum format -t emoji
else
# if not, ask to clone it later
echo "dotfiles repo: :x:" | gum format -t emoji
export REPO_DIR_MISSING=true
fi
# check if non-interactive sudo succeeds
if sudo -n true 2>/dev/null; then
echo "sudo permissions: :white_check_mark:" | gum format -t emoji
else
echo "sudo permissions: :x: (cannot run without password)" | gum format -t emoji
export SUDO_PERMISSIONS_MISSING=true
fi
# ready to proceed
if [ -z "$VAULT_SECRET_MISSING" ] && [ -z "$REPO_DIR_MISSING" ] && [ -z "$VAULT_SECRET_WRONG_PERMS" ] && [ -z "$SUDO_PERMISSIONS_MISSING" ]; then
echo '{{ Bold "ready to go!" }}' | gum format -t template
YES="run ansible!"
NO="wait cancel!"
CHOICE=$(gum choose "$YES" "$NO")
# if yes, clone
if [ "$CHOICE" == "$YES" ]; then
cd $REPO_DIR
ansible-playbook main.yml
fi
fi
}
print_env_check
# clone if repo missing
if [ -n "$REPO_DIR_MISSING" ]; then
# ask first
echo "clone repo to $REPO_DIR?"
YES="Yes, please!"
NO="No, thank you!"
CHOICE=$(gum choose "$YES" "$NO")
# if yes, clone
if [ "$CHOICE" == "$YES" ]; then
mkdir -p $REPO_DIR
gum spin --title "cloning" -- git clone $DOTFILE_REPO_URL $REPO_DIR
REPO_DIR_MISSING=""
fi
print_env_check
fi
# ask for vault secret
if [ -n "$VAULT_SECRET_MISSING" ]; then
mkdir -p $VAULT_SECRET_DIR
gum input --placeholder "paste your secret here" --password > $VAULT_SECRET
chmod 600 $VAULT_SECRET
VAULT_SECRET_MISSING=""
print_env_check
fi
# fix vault secret perms
if [ -n "$VAULT_SECRET_WRONG_PERMS" ]; then
chmod 600 $VAULT_SECRET
VAULT_SECRET_WRONG_PERMS=""
print_env_check
fi
# fix sudo perms
if [ -n "$SUDO_PERMISSIONS_MISSING" ]; then
export DOTFILES_USER=$USER
echo "add NOPASSWD entry for $DOTFILES_USER to /etc/sudoers.d?"
YES="Yes, please!"
NO="No, thank you!"
CHOICE=$(gum choose "$YES" "$NO")
# if yes, add sudoers.d file
if [ "$CHOICE" == "$YES" ]; then
echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$DOTFILES_USER > /dev/null
SUDO_PERMISSIONS_MISSING=""
fi
print_env_check
fi