-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvmgr
More file actions
executable file
·223 lines (211 loc) · 7.8 KB
/
envmgr
File metadata and controls
executable file
·223 lines (211 loc) · 7.8 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
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
#!/usr/bin/env bash
# Simple shell script for symlinking all files from .rc dir to home directory
# Also upgrades, cleanups homebrew and similar stuff in single command
# Actually absurdly ugly, but hey, it works! Somehow.
# colors
COLOR_RESET="$(tput sgr0)"
COLOR_GREEN="$(tput setaf 2)"
# source config
# shellcheck disable=SC1091
source "${HOME}/.rc/envmgr.conf"
# Function to symlink dotfiles
# Checking if OS is Linux (GNU coreutils), if not, we assume OS uses BSD utils
symlink() {
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
ln -sfvn "${1}" "${2}"
else
ln -sfvh "${1}" "${2}"
fi
}
# Symlink dotfiles and envmgr from ${HOME}/.rc to ~
install_env() {
echo -e "${COLOR_GREEN}Deploying environment${COLOR_RESET}"
cd "${HOME}/.rc" || exit
echo -e "${COLOR_GREEN}Deploying dotfiles from $(pwd || true) to ${HOME}${COLOR_RESET}"
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
dotdirs=( .config .local/share )
for dir in "${dotdirs[@]}"; do
if [[ ! -d ${HOME}/${dir} ]]; then
mkdir -p "${HOME}/${dir}"
fi
done
dotfiles=("${dotfiles[@]}" "${linuxenv[@]}")
fi
for file in "${dotfiles[@]}"; do
symlink "${HOME}/.rc/.${file}" "${HOME}/.${file}"
done
symlink "${HOME}/.rc/envmgr" "${HOME}/.rc/.bin/envmgr"
cd - >&/dev/null || exit 1
echo -e "${COLOR_GREEN}Finished deploying dotfiles${COLOR_RESET}"
echo -e "${COLOR_GREEN}Installing vim plugins${COLOR_RESET}"
vim +PlugInstall +qall
echo -e "${COLOR_GREEN}Finished installing vim plugins${COLOR_RESET}"
}
# Install mise and tools (python, node, ruby, golang)
# TODO: detect last version automatically
# TODO: verify downloaded binary against shasum
install_devenv() {
# install mise
echo -e "${COLOR_GREEN}Installing mise${COLOR_RESET}"
# detect if it's already installed, if yes, just do self-update
if [[ -f ${HOME}/.local/bin/mise ]]; then
mise self-update
else
# if not, download
# detect os
case ${OSTYPE} in
"linux-gnu"*) mise_system="linux" ;;
"darwin"*) mise_system="macos" ;;
*) echo "Unable to detect supported operating system" || die ;;
esac
# detect arch since naming is non-standard
case $(uname -m) in
"x86_64"*) mise_architecture="x64" ;;
"arm64"*) mise_architecture="arm64" ;;
*) echo "Unable to detect supported architecture" || die ;;
esac
# if we have jq available, parse latest version, otherwise use one from configs
if hash jq 2>/dev/null; then
mise_version=$(curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/jdx/mise/releases | jq -r '.[0].tag_name')
fi
mise_download_url=https://github.com/jdx/mise/releases/download/"${mise_version}"/mise-"${mise_version}"-"${mise_system}"-"${mise_architecture}"
echo -e "${COLOR_GREEN}Downloading mise from ${mise_download_url}${COLOR_RESET}"
curl -L "${mise_download_url}" > "${HOME}/.local/bin/mise"
chmod +x "${HOME}/.local/bin/mise"
# init mise, force bash because that's what we default
eval "$("${HOME}"/.local/bin/mise activate bash || true)"
fi
# setup default packages
for gem in "${gems[@]}"; do
grep -sq "${gem}" "${HOME}/.default-gems" || echo "${gem}" >> "${HOME}/.default-gems"
done
for egg in "${eggs[@]}"; do
grep -sq "${egg}" "${HOME}/.default-python-packages" || echo "${egg}" >> "${HOME}/.default-python-packages"
done
for npm_package in "${npms[@]}"; do
grep -sq "${npm_package}" "${HOME}/.default-npm-packages" || echo "${npm_package}" >> "${HOME}/.default-npm-packages"
done
for go_pkg in "${gopkgs[@]}"; do
grep -sq "${go_pkg}" "${HOME}/.default-golang-pkgs" || echo "${go_pkg}" >> "${HOME}/.default-golang-pkgs"
done
# install default tools
echo -e "${COLOR_GREEN}Installing default tools${COLOR_RESET}"
for mise_tool in "${mise_tools[@]}"; do
mise install "${mise_tool}"
mise use "${mise_tool}"
done
echo -e "${COLOR_GREEN}Finished installing mise${COLOR_RESET}"
}
# Installs homebrew on os x (global, script) and linux (manual, homedir)
# Also installs apps from ${HOME}/.rc/Brewfile
install_homebrew() {
cd "${HOME}/.rc" || exit
echo -e "${COLOR_GREEN}Installing homebrew and favourite packages${COLOR_RESET}"
if hash brew 2>/dev/null; then
echo "Brew is already installed!"
else
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
git clone https://github.com/Homebrew/brew "${HOME}/.linuxbrew/Homebrew"
mkdir "${HOME}/.linuxbrew/bin"
ln -s "${HOME}/.linuxbrew/Homebrew/bin/brew" "${HOME}/.linuxbrew/bin"
eval "$("${HOME}"/.linuxbrew/bin/brew shellenv || true)"
elif [[ "${OSTYPE}" == "darwin"* ]]; then
# TODO: Migrate to manual install
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh || true)"
eval "$(/opt/homebrew/bin/brew shellenv || true)"
fi
fi
brew bundle
}
install_kubectl_plugins() {
cd "${HOME}/.rc" || exit
echo -e "${COLOR_GREEN}Installing kubectl plugins${COLOR_RESET}"
if hash kubectl-krew 2>/dev/null; then
kubectl krew install "${kubectl_plugins}"
fi
}
install_helm_plugins() {
cd "${HOME}/.rc" || exit
echo -e "${COLOR_GREEN}Installing helm plugins${COLOR_RESET}"
if hash helm 2>/dev/null; then
helm plugin install "${helm_plugins}"
fi
}
# Nukes whole dotfiles setup
# TODO: Maybe use shred instead of rm?
env_nuke() {
echo "Cleaning up env"
echo "Removing dotfiles symlinks"
for file in "${dotfiles[@]}"; do
rm -vf "${HOME}/.${file}"
done
echo "Cleaning up devenv"
#for backwards compatibility
rm -rvf "${HOME}/.asdf"
rm -rvf "${HOME}/.local/bin/mise"
rm -rvf "${HOME}/.local/share/mise"
rm -rvf "${HOME}/.local/state/mise"
echo "Removing history"
rm -vf "${HOME}/.zsh-history"
rm -vf "${HOME}/.bash_history"
echo "Removing linuxbrew"
rm -rf "${HOME}/.linuxbrew"
echo "Removing dotfiles directory"
rm -rf "${HOME}/.rc"
}
# Checks for updates in env, homebrew, vim plugins and mac app store
env_housekeeper() {
echo -e "${COLOR_GREEN}Started housekeeping${COLOR_RESET}"
echo -e "${COLOR_GREEN}Updating dotfiles repo${COLOR_RESET}"
git -C "${HOME}/.rc" pull
echo -e "${COLOR_GREEN}Cleaning up outdated symlinks${COLOR_RESET}"
find -L "${HOME}" -maxdepth 1 -type l -exec rm -i {} +
if hash brew 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating and cleaning up homebrew${COLOR_RESET}"
brew update
brew upgrade
brew cleanup
fi
# Update mise
echo -e "${COLOR_GREEN}Updating mise and mise plugins${COLOR_RESET}"
if hash mise 2>/dev/null; then
mise self-update
mise upgrade --bump
fi
if hash kubectl-krew 2>/dev/null; then
kubectl krew upgrade
fi
# helm doesn't have any checks for versions, so will always run. Disable for now
# if hash helm 2>/dev/null; then
# helm_installed_plugins=$(helm plugin list | tail -n +2 | awk '{print $1}')
# for plugin in $helm_installed_plugins; do
# helm plugin update $plugin
# done
# fi
if hash mas 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating mac app store apps${COLOR_RESET}"
mas upgrade
fi
if hash vim 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating vim plugins${COLOR_RESET}"
vim +PlugUpdate +PlugUpgrade +PlugClean +qall
fi
echo -e "${COLOR_GREEN}Housekeeping finished${COLOR_RESET}"
}
# Arguments parsing
case "${1}" in
env) install_env ;;
devenv) install_devenv ;;
brew) install_homebrew ;;
fullenv) install_env && install_devenv && install_homebrew && install_kubectl_plugins && install_helm_plugins ;;
nuke) env_nuke ;;
maid) env_housekeeper ;;
*)
echo "Command list:"
echo "env - symlink dotfiles"
echo "devenv - install mise with plugins"
echo "brew - install homebrew and packages from bundle"
echo "fullenv - env, devenv and brew combined"
echo "nuke - remove (almost) everything installed by envmgr"
echo "maid - upgrade stuff installed by envmgr"
esac