-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
executable file
·253 lines (208 loc) · 6.86 KB
/
setup.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env bash
set -Eeuo pipefail
if [ "${DOTFILES_DEBUG:-}" ]; then
set -x
fi
# shellcheck disable=SC2016
declare -r DOTFILES_LOGO='
/$$ /$$
| $$ | $$
/$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$$| $$$$$$$
/$$_____/ /$$__ $$|_ $$_/ | $$ | $$ /$$__ $$ /$$_____/| $$__ $$
| $$$$$$ | $$$$$$$$ | $$ | $$ | $$| $$ \ $$ | $$$$$$ | $$ \ $$
\____ $$| $$_____/ | $$ /$$| $$ | $$| $$ | $$ \____ $$| $$ | $$
/$$$$$$$/| $$$$$$$ | $$$$/| $$$$$$/| $$$$$$$//$$ /$$$$$$$/| $$ | $$
|_______/ \_______/ \___/ \______/ | $$____/|__/|_______/ |__/ |__/
| $$
| $$
|__/
*** This is setup script for my dotfiles setup ***
https://github.com/shunk031/dotfiles
'
declare -r DOTFILES_REPO_URL="https://github.com/shunk031/dotfiles"
declare -r BRANCH_NAME="${BRANCH_NAME:-master}"
declare -r DOTFILES_GITHUB_PAT="${DOTFILES_GITHUB_PAT:-}"
function is_ci() {
"${CI:-false}"
}
function is_tty() {
[ -t 0 ]
}
function is_not_tty() {
! is_tty
}
function is_ci_or_not_tty() {
is_ci || is_not_tty
}
function at_exit() {
AT_EXIT+="${AT_EXIT:+$'\n'}"
AT_EXIT+="${*?}"
# shellcheck disable=SC2064
trap "${AT_EXIT}" EXIT
}
function get_os_type() {
uname
}
function keepalive_sudo_linux() {
# Might as well ask for password up-front, right?
echo "Checking for \`sudo\` access which may request your password."
sudo -v
# Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done 2>/dev/null &
}
function keepalive_sudo_macos() {
# ref. https://github.com/reitermarkus/dotfiles/blob/master/.sh#L85-L116
(
builtin read -r -s -p "Password: " </dev/tty
builtin echo "add-generic-password -U -s 'dotfiles' -a '${USER}' -w '${REPLY}'"
) | /usr/bin/security -i
printf "\n"
at_exit "
echo -e '\033[0;31mRemoving password from Keychain …\033[0m'
/usr/bin/security delete-generic-password -s 'dotfiles' -a '${USER}'
"
SUDO_ASKPASS="$(/usr/bin/mktemp)"
at_exit "
echo -e '\033[0;31mDeleting SUDO_ASKPASS script …\033[0m'
/bin/rm -f '${SUDO_ASKPASS}'
"
{
echo "#!/bin/sh"
echo "/usr/bin/security find-generic-password -s 'dotfiles' -a '${USER}' -w"
} >"${SUDO_ASKPASS}"
/bin/chmod +x "${SUDO_ASKPASS}"
export SUDO_ASKPASS
if ! /usr/bin/sudo -A -kv 2>/dev/null; then
echo -e '\033[0;31mIncorrect password.\033[0m' 1>&2
exit 1
fi
}
function keepalive_sudo() {
local ostype
ostype="$(get_os_type)"
if [ "${ostype}" == "Darwin" ]; then
keepalive_sudo_macos
elif [ "${ostype}" == "Linux" ]; then
keepalive_sudo_linux
else
echo "Invalid OS type: ${ostype}" >&2
exit 1
fi
}
function initialize_os_macos() {
function is_homebrew_exists() {
command -v brew &>/dev/null
}
# Instal Homebrew if needed.
if ! is_homebrew_exists; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Setup Homebrew envvars.
if [[ $(arch) == "arm64" ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ $(arch) == "i386" ]]; then
eval "$(/usr/local/bin/brew shellenv)"
else
echo "Invalid CPU arch: $(arch)" >&2
exit 1
fi
}
function initialize_os_linux() {
:
}
function initialize_os_env() {
local ostype
ostype="$(get_os_type)"
if [ "${ostype}" == "Darwin" ]; then
initialize_os_macos
elif [ "${ostype}" == "Linux" ]; then
initialize_os_linux
else
echo "Invalid OS type: ${ostype}" >&2
exit 1
fi
}
function run_chezmoi() {
# download the chezmoi binary from the URL
sh -c "$(curl -fsLS get.chezmoi.io)"
local chezmoi_cmd
chezmoi_cmd="./bin/chezmoi"
if is_ci_or_not_tty; then
no_tty_option="--no-tty" # /dev/tty is not available (especially in the CI)
else
no_tty_option="" # /dev/tty is available OR not in the CI
fi
# run `chezmoi init` to setup the source directory,
# generate the config file, and optionally update the destination directory
# to match the target state.
"${chezmoi_cmd}" init "${DOTFILES_REPO_URL}" \
--force \
--branch "${BRANCH_NAME}" \
--use-builtin-git true \
${no_tty_option}
# the `age` command requires a tty, but there is no tty in the github actions.
# Therefore, it is currnetly difficult to decrypt the files encrypted with `age` in this workflow.
# I decided to temporarily remove the encrypted target files from chezmoi's control.
if is_ci_or_not_tty; then
find "$(${chezmoi_cmd} source-path)" -type f -name "encrypted_*" -exec rm -fv {} +
fi
# Add to PATH for installing the necessary binary files under `$HOME/.local/bin`.
export PATH="${PATH}:${HOME}/.local/bin"
if [[ -n "${DOTFILES_GITHUB_PAT}" ]]; then
export DOTFILES_GITHUB_PAT
fi
# run `chezmoi apply` to ensure that target... are in the target state,
# updating them if necessary.
"${chezmoi_cmd}" apply ${no_tty_option}
# purge the binary of the chezmoi cmd
rm -fv "${chezmoi_cmd}"
}
function initialize_dotfiles() {
if ! is_ci_or_not_tty; then
# - /dev/tty of the github workflow is not available.
# - We can use password-less sudo in the github workflow.
# Therefore, skip the sudo keep alive function.
keepalive_sudo
fi
run_chezmoi
}
function get_system_from_chezmoi() {
local system
system=$(chezmoi data | jq -r '.system')
echo "${system}"
}
function restart_shell_system() {
local system
system=$(get_system_from_chezmoi)
# exec shell as login shell (to reload the .zprofile or .profile)
if [ "${system}" == "client" ]; then
/bin/zsh --login
elif [ "${system}" == "server" ]; then
/bin/bash --login
else
echo "Invalid system: ${system}; expected \`client\` or \`server\`" >&2
exit 1
fi
}
function restart_shell() {
# Restart shell if specified "bash -c $(curl -L {URL})"
# not restart:
# curl -L {URL} | bash
if [ -p /dev/stdin ]; then
echo "Now continue with Rebooting your shell"
else
echo "Restarting your shell..."
restart_shell_system
fi
}
function main() {
echo "$DOTFILES_LOGO"
initialize_os_env
initialize_dotfiles
# restart_shell # Disabled because the at_exit function does not work properly.
}
main