Repository navigation
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·130 lines (109 loc) · 4.25 KB
/
Copy pathbootstrap.sh
File metadata and controls
executable file
·130 lines (109 loc) · 4.25 KB
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
#!/usr/bin/env bash
# Bootstrap the small set of prerequisites needed before mise can manage this
# repository. The full machine setup belongs in mise.toml and is applied by
# `mise bootstrap` below.
set -Eeuo pipefail
readonly DEFAULT_REPOSITORY="https://github.com/timfel/dotfiles.git"
readonly MISE_INSTALL_URL="https://mise.run"
repository="${DOTFILES_REPO:-$DEFAULT_REPOSITORY}"
dotfiles_dir="${DOTFILES_DIR:-${HOME}/dotfiles}"
log() {
printf 'bootstrap: %s\n' "$*"
}
fail() {
printf 'bootstrap: error: %s\n' "$*" >&2
exit 1
}
as_root() {
if [[ "${EUID}" -eq 0 ]]; then
"$@"
elif command -v sudo >/dev/null 2>&1; then
sudo "$@"
else
fail "installing prerequisites requires root or sudo: $*"
fi
}
install_git() {
if command -v git >/dev/null 2>&1; then
return
fi
log "git was not found; installing it with the system package manager"
if command -v apt-get >/dev/null 2>&1; then
as_root apt-get update
as_root apt-get install -y ca-certificates curl git
elif command -v dnf >/dev/null 2>&1; then
as_root dnf install -y ca-certificates curl git
elif command -v yum >/dev/null 2>&1; then
as_root yum install -y ca-certificates curl git
elif command -v apk >/dev/null 2>&1; then
as_root apk add ca-certificates curl git
elif command -v pacman >/dev/null 2>&1; then
as_root pacman -Sy --needed --noconfirm ca-certificates curl git
elif command -v zypper >/dev/null 2>&1; then
as_root zypper --non-interactive install ca-certificates curl git
elif command -v brew >/dev/null 2>&1; then
brew install git
else
fail "could not find a supported package manager; install git and rerun this script"
fi
}
install_mise() {
if command -v mise >/dev/null 2>&1; then
return
fi
command -v curl >/dev/null 2>&1 || fail "curl is required to install mise"
log "mise was not found; installing it from ${MISE_INSTALL_URL}"
local installer
installer="$(mktemp)"
curl --fail --silent --show-error --location "${MISE_INSTALL_URL}" --output "${installer}"
sh "${installer}"
rm -f "${installer}"
# The official installer normally uses ~/.local/bin. Add the common
# locations explicitly because this process cannot inherit a modified PATH.
export PATH="${HOME}/.local/bin:${HOME}/bin:${PATH}"
command -v mise >/dev/null 2>&1 || fail "mise was installed but is not on PATH"
}
ensure_global_mise_config() {
local config_dir config link_target actual_target
config_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/mise"
config="${config_dir}/config.toml"
link_target="${dotfiles_dir}/mise.toml"
mkdir -p "${config_dir}"
if [[ -e "${config}" || -L "${config}" ]]; then
[[ -L "${config}" ]] || fail "refusing to replace existing mise config: ${config}"
actual_target="$(readlink -f "${config}" 2>/dev/null || true)"
[[ "${actual_target}" == "$(readlink -f "${link_target}")" ]] \
|| fail "mise config is a conflicting symlink: ${config}"
return
fi
log "linking global mise config ${config} -> ${link_target}"
ln -s "${link_target}" "${config}"
}
clone_or_use_repository() {
if [[ -e "${dotfiles_dir}" ]]; then
[[ -d "${dotfiles_dir}" ]] || fail "DOTFILES_DIR exists but is not a directory: ${dotfiles_dir}"
git -C "${dotfiles_dir}" rev-parse --show-toplevel >/dev/null 2>&1 \
|| fail "DOTFILES_DIR exists but is not a git checkout: ${dotfiles_dir}"
log "using existing checkout ${dotfiles_dir}"
log "not pulling automatically; update it manually with git -C ${dotfiles_dir} pull"
return
fi
mkdir -p "$(dirname "${dotfiles_dir}")"
log "cloning ${repository} into ${dotfiles_dir}"
git clone "${repository}" "${dotfiles_dir}"
}
main() {
[[ -n "${HOME:-}" ]] || fail 'HOME is not set'
install_git
install_mise
clone_or_use_repository
local config="${dotfiles_dir}/mise.toml"
[[ -f "${config}" ]] || fail "mise.toml was not found in ${dotfiles_dir}"
ensure_global_mise_config
log "trusting ${config}"
mise trust --yes "${config}"
log "running mise bootstrap"
(cd "${dotfiles_dir}" && mise bootstrap --yes)
log "done"
}
main "$@"