-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.zsh
executable file
·378 lines (320 loc) · 12.8 KB
/
setup.zsh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/zsh
set -eu
#
# Setup env
#
ARCH="amd64"
if [[ "$(uname)" == "Darwin" ]]; then
if [[ "$(uname -m)" == "amd64" || "$(uname -m)" == "x86_64" ]]; then
HOMEBREW_PATH=/usr/local
ARCH="amd64"
elif [[ "$(uname -m)" == "arm64" ]]; then
HOMEBREW_PATH=/opt/homebrew
ARCH="arm64"
fi
fi
#
# Setup PATH
#
cd $HOME
PREFIX=$HOME/.local
mkdir -p $PREFIX/bin
mkdir -p $PREFIX/lib
export PATH=$PREFIX/bin:${HOMEBREW_PATH:-}/bin:$PATH
export LD_LIBRARY_PATH=$PREFIX/lib:${LD_LIBRARY_PATH:-}
export TERM=xterm-256color
autoload -Uz colors && colors
autoload -Uz compinit && compinit
autoload -U +X bashcompinit && bashcompinit
#
# Functions
#
command_exists () {
if ! [[ -x $(command -v "$1") ]]; then
print -P "%F{160}▓▒░ %F{220}$1%F{160} not found in PATH.%f%b"
return 1
fi
return 0
}
get_latest_version_tag() {
local repo_url=$1
local prefix="${2:-""}"
local version_pattern="^${prefix}\K[0-9]+(\.[0-9]+)+$"
local latest_tag=$(git ls-remote --tags "$repo_url" | awk -F/ '{print $NF}' | \
grep -oP "$version_pattern" | \
sort -V | tail -n1)
echo "$latest_tag"
}
get_latest_tag() {
local repo_url=$1
local latest_tag=$(git ls-remote --tags --sort='v:refname' "$repo_url" | \
tail --line 1 | cut --delimiter='/' --fields=3)
echo "$latest_tag"
}
#
# Check command exists
#
if [[ "$(uname)" == "Darwin" ]]; then
command_exists "brew" || exit 1;
brew update
FORMULAS+=(
coreutils
diffutils
ed
findutils
gawk
git
gnu-sed
gnu-tar
grep
gzip
pass
stow
wget
zsh
)
for formula in ${FORMULAS[@]}; do
if brew ls --versions ${formula} ; then
brew upgrade ${formula}
else
brew install ${formula}
fi
done
fi
command_exists "curl" || exit 1;
command_exists "git" || exit 1;
command_exists "gpg" || exit 1;
command_exists "stow" || exit 1;
#
# Anyenv
#
export ANYENV_ROOT="$HOME/.anyenv"
if [[ ! -f $ANYENV_ROOT/bin/anyenv ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}anyenv%F{220} All in one for **env (%F{33}anyenv/anyenv%F{220})…%f"
command git clone https://github.com/anyenv/anyenv "$ANYENV_ROOT" && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
command mkdir -p $ANYENV_ROOT/plugins
command git clone https://github.com/znz/anyenv-update.git $ANYENV_ROOT/plugins/anyenv-update && \
print -P "%F{33}▓▒░ %F{34}Installation plugin anyenv-update successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
command git clone https://github.com/znz/anyenv-git.git $ANYENV_ROOT/plugins/anyenv-git && \
print -P "%F{33}▓▒░ %F{34}Installation plugin anyenv-git successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
export PATH="$ANYENV_ROOT/bin:$PATH"
command yes | anyenv install --init
fi
# Initial setting of anyenv.
export PATH="$ANYENV_ROOT/bin:$PATH"
eval "$(env PATH="$ANYENV_ROOT/libexec:$PATH" $ANYENV_ROOT/libexec/anyenv-init - --no-rehash)"
#
# Golang
#
# Add GOPATH
export GOENV_DISABLE_GOPATH=1
export GOPATH=$HOME/Project
export PATH=$GOPATH/bin:$PATH
# Goenv
if [[ ! -f $ANYENV_ROOT/envs/goenv/bin/goenv ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}goenv%F{220} Go Version Management (%F{33}syndbg/goenv%F{220})…%f"
command anyenv install goenv && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
# ghq
if ! [[ -x $(command -v ghq) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}ghq%F{220} Manage remote repository clones (%F{33}x-motemen/ghq%F{220})…%f"
if [[ "$(uname)" == "Linux" ]]; then
command curl -L -o /tmp/ghq.zip https://github.com/x-motemen/ghq/releases/latest/download/ghq_linux_amd64.zip
elif [[ "$(uname)" == "Darwin" ]]; then
command curl -L -o /tmp/ghq.zip https://github.com/x-motemen/ghq/releases/latest/download/ghq_darwin_${ARCH}.zip
fi
command unzip -qq /tmp/ghq.zip -d /tmp && \
mv /tmp/ghq_*/ghq $PREFIX/bin/ && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
command rm -rf /tmp/ghq*
fi
# Peco
if ! [[ -x $(command -v peco) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}peco%F{220} Simplistic interactive filtering tool (%F{33}peco/peco%F{220})…%f"
if [[ "$(uname)" == "Linux" ]]; then
command curl -L -o /tmp/peco.tar.gz https://github.com/peco/peco/releases/latest/download/peco_linux_amd64.tar.gz && \
tar xvf /tmp/peco.tar.gz -C /tmp
elif [[ "$(uname)" == "Darwin" ]]; then
command curl -L -o /tmp/peco.zip https://github.com/peco/peco/releases/latest/download/peco_darwin_${ARCH}.zip && \
unzip -qq /tmp/peco.zip -d /tmp
fi
command mv /tmp/peco_*/peco $PREFIX/bin/ && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
command rm -rf /tmp/peco*
fi
# tenv
if ! [[ -x $(command -v tenv) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}tenv%F{220} OpenTofu, Terraform, Terragrunt, and Atmos version manager, written in Go. (%F{33}tofuutils/tenv%F{220})…%f"
TENV_VERSION=$(get_latest_version_tag https://github.com/tofuutils/tenv.git v)
if [[ "$(uname)" == "Linux" ]]; then
command curl -L -o /tmp/tenv.tar.gz https://github.com/tofuutils/tenv/releases/download/v${TENV_VERSION}/tenv_v${TENV_VERSION}_Linux_x86_64.tar.gz && \
tar xvf /tmp/tenv.tar.gz -C /tmp
elif [[ "$(uname)" == "Darwin" ]]; then
TENV_ARCH="x86_64"
if [[ "${ARCH}" == "arm64" ]]; then
TENV_ARCH="arm64"
fi
command curl -L -o /tmp/tenv.tar.gz https://github.com/tofuutils/tenv/releases/download/v${TENV_VERSION}/tenv_v${TENV_VERSION}_Darwin_${TENV_ARCH}.tar.gz && \
tar xvf /tmp/tenv.tar.gz -C /tmp
fi
command mv /tmp/{tofu,tf,terragrunt,terraform,tenv,atmos} $PREFIX/bin/ && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
fi
# Nodenv
if [[ ! -f $ANYENV_ROOT/envs/nodenv/bin/nodenv ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}nodenv%F{220} Groom your app's Node environment with nodenv. (%F{33}nodenv/nodenv%F{220})…%f"
command anyenv install nodenv && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
command mkdir -p "$ANYENV_ROOT/envs/nodenv/plugins" && \
git clone https://github.com/pine/nodenv-yarn-install.git "$ANYENV_ROOT/envs/nodenv/plugins/nodenv-yarn-install" && \
print -P "%F{33}▓▒░ %F{34}Installation yarn plugin successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
# Pyenv
export PYENV_ROOT=$ANYENV_ROOT/envs/pyenv
if [[ ! -f $ANYENV_ROOT/envs/pyenv/bin/pyenv ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}pyenv%F{220} Simple Python Version Management: pyenv (%F{33}pyenv/pyenv%F{220})…%f"
command anyenv install pyenv && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
# AtomicParsley
if ! [[ -x $(command -v AtomicParsley) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}AtomicParsley%F{220} AtomicParsley is a lightweight command line program for reading, parsing and setting metadata into MPEG-4 files, in particular, iTunes-style metadata. (%F{33}wez/atomicparsley%F{220})…%f"
ATOMICPARSLEY_VERSION=$(get_latest_tag https://github.com/wez/atomicparsley.git)
if [[ "$(uname)" == "Linux" ]]; then
command curl -L -o /tmp/AtomicParsleyLinux.zip https://github.com/wez/atomicparsley/releases/download/${ATOMICPARSLEY_VERSION}/AtomicParsleyLinux.zip && \
unzip -qq /tmp/AtomicParsleyLinux.zip -d /tmp
elif [[ "$(uname)" == "Darwin" ]]; then
command curl -L -o /tmp/AtomicParsleyMacOS.zip https://github.com/wez/atomicparsley/releases/download/${ATOMICPARSLEY_VERSION}/AtomicParsleyMacOS.zip && \
unzip -qq /tmp/AtomicParsleyMacOS.zip -d /tmp
fi
command mv /tmp/AtomicParsley $PREFIX/bin/ && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
fi
# ffmpeg
if [[ "$(uname)" == "Linux" ]]; then
if ! [[ -x $(command -v ffmpeg) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}ffmpeg%F{220} A complete, cross-platform solution to record, convert and stream audio and video. (%F{33}AkashiSN/ffmpeg-docker%F{220})…%f"
FFMPEG_VERSION=$(get_latest_version_tag https://github.com/AkashiSN/ffmpeg-docker v)
command curl -L -o /tmp/ffmpeg.tar.xz https://github.com/AkashiSN/ffmpeg-docker/releases/download/v${FFMPEG_VERSION}/ffmpeg-7.0.2-linux-amd64.tar.xz && \
tar xvf /tmp/ffmpeg.tar.xz -C /tmp/
command mv /tmp/ffmpeg-7.0.2-linux-amd64/bin/* $PREFIX/bin/ && \
mv /tmp/ffmpeg-7.0.2-linux-amd64/lib/* $PREFIX/lib/ && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
fi
fi
# yt-dlp
if ! [[ -x $(command -v yt-dlp) ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}yt-dlp%F{220} yt-dlp is a feature-rich command-line audio/video downloader with support for thousands of sites. (%F{33}yt-dlp/yt-dlp%F{220})…%f"
if [[ "$(uname)" == "Linux" ]]; then
command curl -L -o /tmp/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux
elif [[ "$(uname)" == "Darwin" ]]; then
if [[ "${ARCH}" == "arm64" ]]; then
command curl -L -o /tmp/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos
else
command curl -L -o /tmp/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos_legacy
fi
fi
command mv /tmp/yt-dlp $PREFIX/bin/ && \
chmod +x $PREFIX/bin/yt-dlp && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The installation has failed.%f%b"
fi
#
# Clone dotfiles
#
if [[ ! -d $GOPATH/src/github.com/AkashiSN/dotfiles ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}dotfiles%F{220} dotfiles (%F{33}AkashiSN/dotfiles%F{220})…%f"
command mkdir -p $GOPATH/src/github.com/AkashiSN/dotfiles && \
git clone https://github.com/AkashiSN/dotfiles.git $GOPATH/src/github.com/AkashiSN/dotfiles && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
else
print -P "%F{33}▓▒░ %F{220}Updating %F{33}dotfiles%F{220}%f"
(cd $GOPATH/src/github.com/AkashiSN/dotfiles && git pull) && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
print -P "%F{33}▓▒░ %F{220}Linking %F{33}dotfiles%F{220}%f"
if [[ ! -L $HOME/.gitconfig ]]; then
rm -f $HOME/.gitconfig
fi
command stow -v -d $GOPATH/src/github.com/AkashiSN/dotfiles -t $HOME others
command stow -v -d $GOPATH/src/github.com/AkashiSN/dotfiles -t $PREFIX/bin scripts
if [[ "$(uname)" == "Darwin" ]]; then
command chmod 755 ${HOMEBREW_PATH}/share/zsh && \
chmod 755 ${HOMEBREW_PATH}/share/zsh/site-functions && \
defaults write com.apple.desktopservices DSDontWriteNetworkStores True && \
killall Finder
fi
if ! [ "${SSH_CONNECTION:-}" ]; then
print -P "%F{33}▓▒░ %F{220}Linking %F{33}.ssh%F{220}%f"
command mkdir -p $HOME/.ssh && \
stow -v -d $GOPATH/src/github.com/AkashiSN/dotfiles -t $HOME/.ssh ssh
fi
#
# Import gpgkey
#
if [[ ! $(gpg --list-keys nishi) ]]; then
gpg --import $GOPATH/src/github.com/AkashiSN/dotfiles/gpg/nishi.gpg
echo -e "5\ny\n" | gpg --command-fd 0 --edit-key "nishi" trust
fi
if [[ ! $(gpg --list-keys isec) ]]; then
gpg --import $GOPATH/src/github.com/AkashiSN/dotfiles/gpg/isec.gpg
echo -e "5\ny\n" | gpg --command-fd 0 --edit-key "isec" trust
fi
if [[ ! -f $(gpgconf --list-dir homedir)/gpg-agent.conf ]]; then
if [[ "$(uname -r)" == *microsoft* ]]; then
cat <<EOF > $(gpgconf --list-dir homedir)/gpg-agent.conf
pinentry-program /usr/bin/pinentry-curses
EOF
fi
cat <<EOF >> $(gpgconf --list-dir homedir)/gpg-agent.conf
enable-ssh-support
default-cache-ttl-ssh 7200
max-cache-ttl-ssh 28800
EOF
fi
#
# vault setup
#
export PASSWORD_STORE_DIR=$HOME/.password-store
if [[ ! -d $PASSWORD_STORE_DIR ]]; then
git clone git@github.com:AkashiSN/vault.git $PASSWORD_STORE_DIR
fi
#
# Change default shell
#
zsh=false
if [[ "$(uname)" == "Linux" ]]; then
if [[ $(cat /etc/passwd | grep $HOME) =~ "zsh" ]]; then
zsh=true
fi
elif [[ "$(uname)" == "Darwin" ]]; then
if [[ $(dscl . -read ~/ UserShell) =~ "zsh" ]]; then
zsh=true
fi
fi
if ! $zsh ; then
/bin/echo -n "Do you want to change default shell to zsh? [y/N]: ";
if read -q; then;
print -P "%F{33}▓▒░ %F{34}Change login shell to zsh%f%b"
export user=$(whoami) && \
sudo usermod -s $(which zsh) $user && \
print -P "%F{33}▓▒░ %F{34}All complete, Restart your shell (exec \$SHELL -l) .%f%b" || \
print -P "%F{160}▓▒░ Changeing login shell has failed.%f%b"
fi
fi