-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.bash
356 lines (319 loc) · 8.05 KB
/
functions.bash
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
#!/usr/bin/env bash
set -e
DOT="${DOT:-"${HOME}/.dotfiles"}"
BIN="${BIN:-"${HOME}/.local/bin"}"
LIB="${LIB:-"${HOME}/.local/lib"}"
RUST_ARCH="x86_64-unknown-linux-musl"
has() {
wh "$@" &>/dev/null
}
wh() {
for name in "$@"; do
command which "${name}" 2>/dev/null && return
done
return 1
}
PM="sudo $(wh yum apt)"
export PM
is_debian() {
[[ "${PM}" != *yum* ]]
}
del() {
for file in "$@"; do
([[ -e "${file}" || -L "${file}" ]] &&
mv --backup=t "$1" /tmp &>/dev/null) ||
rm -rf "${file}" &>/dev/null
done
}
version_lte() {
[[ "$1" == "$(printf "%s\n%s" "$1" "$2" | sort -V | head -n 1)" ]]
}
check_bin() {
[[ -d "${BIN}" ]] || mkdir -p "${BIN}"
[[ -d "${LIB}" ]] || mkdir -p "${LIB}"
}
install_dot() {
if [[ ! -d "${DOT}" ]]; then
git clone [email protected]:mmqmzk/dotfiles.git "${DOT}" ||
git clone https://github.com/mmqmzk/dotfiles.git "${DOT}"
fi
pushd "${DOT}" &>/dev/null
git pull --no-rebase || true
git submodule update --init
check_bin
ln -sfn "${DOT}/zsh-custom/diff-so-fancy/diff-so-fancy" "${BIN}/diff-so-fancy"
ln -sfn "${DOT}/zfuncs/sshrc" "${BIN}/sshrc"
ln -sfn "${DOT}/zfuncs/v" "${BIN}/v"
[[ -e ~/.sshrc.d ]] || ln -s "${DOT}/sshrc.d" ~/.sshrc.d
if [[ "${HOME}" != /root ]]; then
sudo mkdir -p /root/.local
sudo ln -s "${BIN}" /root/.local/bin || true
fi
popd
}
add_v() {
: "$1"
[[ "$2" == v* ]] && [[ "$_" != v* ]] && : "v$_"
[[ "$2" == V* ]] && [[ "$_" != V* ]] && : "V$_"
echo "$_"
}
check_current_tag() {
: "$("$1" --version 2>&1 |
grep -iv "command not found" |
grep -v '未找到命令' |
grep -Eio 'v?[0-9]+[0-9.]*' |
sed '1q')"
: "$(add_v "$_" "$2")"
if [[ -n "$_" ]] && version_lte "$2" "$_"; then
echo "Tool $1 already up to date, version: $_."
else
return 1
fi
}
get_tag_url() {
if ! has jq; then
echo "Tool jq not Installed." && exit 1
fi
curl -fsSL "https://api.github.com/repos/$1/releases/${2:-latest}" \
-H "Authorization: token $GITHUB_TOKEN" |
jq "{url:.assets[].browser_download_url,tag:.tag_name}\
|select(.url|contains(\"${3:-"${RUST_ARCH}"}\"))|.url+\" \"+.tag" -r |
sed '1q'
}
_download() {
local url="$1"
local dir="$2"
local result="$3"
del "${dir}"
mkdir -p "${dir}" && pushd "${dir}" &>/dev/null
curl -fsSL "${url}" -o "${result}"
case "${result}" in
*.tar.* | *.tar)
tar -xf "${result}" && del "${result}"
;;
*.zip)
unzip -o "${result}" && del "${result}"
;;
esac
popd &>/dev/null
}
_link() {
check_bin
: "$(find "$1" -name "*$2*" -type f -perm -555 | sed '1q')"
ln -sfn "$_" "${BIN}/$2"
: "$(ls -l --color=always "$_")"
echo "Linked $_."
local -a files
files=$(find "$1" -name "*.1" -type f)
if (("${#files}")); then
sudo cp -f "${files[@]}" /usr/local/share/man/man1
sudo mandb || true
fi
}
install_rust() {
local module="$1"
cargo install "${module}"
# local bin="$2"
# local repo="$3"
# local tag="${4:-"latest"}"
# [[ "${tag}" == "-f" ]] && tag="latest" && local force="YES"
# local arch="${5:-"${RUST_ARCH}"}"
# local url
# local result="${module}-${tag}-${arch}.tar.gz"
# if [[ "${tag}" == "latest" ]]; then
# local data
# data="$(get_tag_url "${repo}" "${tag}" "${arch}")"
# url="${data%% *}"
# tag="${data##* }"
# if [[ -z "${force}" ]] && check_current_tag "${bin}" "${tag}"; then
# return
# fi
# result="${result//latest/${tag}}"
# else
# url="https://github.com/${repo}/releases/download/${tag}/${result}"
# fi
# echo "Installing ${module} ${tag}."
# local dir="${LIB}/${module}"
# _download "${url}" "${dir}" "${result}"
# _link "${dir}" "${bin}"
}
install_bat() {
install_rust bat bat "sharkdp/bat" "$1"
}
install_delta() {
install_rust git-delta delta "dandavison/delta" "$1"
}
install_fd() {
install_rust fd-find fd "sharkdp/fd" "$1"
}
install_hexyl() {
install_rust hexyl hexyl "sharkdp/hexyl" "$1"
}
install_lsd() {
install_rust lsd lsd "Peltoche/lsd" "$1" "x86_64-unknown-linux-gnu"
}
install_ripgrep() {
install_rust ripgrep rg "BurntSushi/ripgrep" "$1"
}
install_xsv() {
install_rust xsv "xsv" "BurntSushi/xsv" "$1"
}
install_fzf() {
if [[ "$1" == "init" ]]; then
install_dot
fi
local fzf_base="${FZF_BASE:-"${DOT}/fzf"}"
if [[ -d "${fzf_base}" ]]; then
echo "Installing fzf."
bash "${fzf_base}/install" --bin
ln -sfn "${fzf_base}/bin/fzf" "${BIN}"
fi
}
install_jq() {
if is_debian; then
${PM} install jq jo -y
else
local JQ_TAG=$1
[[ -z "${JQ_TAG}" ]] && return 1
echo "Installing jq ${JQ_TAG}"
local result="jq-linux64"
local dir="${LIB}/jq"
: "https://github.com/stedolan/jq/releases/download/jq-${JQ_TAG}/${result}"
_download "$_" "${dir}" "${result}"
chmod 755 "${dir}/${result}"
_link "${dir}" "jq"
fi
}
install_vim() {
if [[ $1 == "init" ]]; then
install_dot
fi
echo "Installing vim"
${PM} install vim -y
pushd ~ &>/dev/null
del ~/.vim*
local VIMDIR="${DOT}/vim"
ln -sfn "${VIMDIR}" ~/.vim
bash "${VIMDIR}/install.sh"
popd &>/dev/null
}
install_cht() {
echo "Installing cheat.sh"
check_bin
: "/tmp/cht"
del "$_"
curl -sSfL http://cht.sh/:cht.sh -o "$_"
chmod 755 "$_"
mv -f "$_" "${BIN}"
}
install_q() {
local tag=${1:-latest}
[[ "${tag}" == "-f" ]] && tag="latest" && local force="YES"
local result="q-x86_64-Linux"
local url="https://github.com/harelba/q/releases/download/${tag}/${result}"
if [[ "${tag}" == "latest" ]]; then
local data
data="$(get_tag_url "harelba/q" "${tag}" "${result}")"
url="${data%% *}"
tag="${data##* }"
if [[ -z "${force}" ]] && check_current_tag "q" "${tag}"; then
return
fi
result="${result//latest/${tag}}"
fi
echo "Installing q ${tag}."
local dir="${LIB}/q"
_download "${url}" "${dir}" "${result}"
chmod 755 "${dir}/${result}"
_link "${dir}" "q"
}
install_node() {
: "${NVM_DIR:-"${DOT}/nvm"}/nvm.sh"
if [[ -f "$_" ]]; then
source "$_"
else
echo "Tool nvm not installed."
return 1
fi
local NVMRC="${HOME}/.nvmrc"
local CURRENT_VERSION
CURRENT_VERSION="$(nvm current 2>/dev/null)"
local NODE_TAG
NODE_TAG="$(nvm ls-remote "$1" 2>/dev/null |
grep -Eo 'v[0-9.]+' |
sort -V |
tail -n 1)"
[[ -z "${NODE_TAG}" ]] && echo "Node version $1 not found." && return 1
if [[ "${NODE_TAG}" != "${CURRENT_VERSION}" ]]; then
if nvm install "${NODE_TAG}"; then
echo "${NODE_TAG}" >"${NVMRC}"
nvm alias default "${NODE_TAG}"
nvm use --delete-prefix default
install_npm
else
echo "Install node version ${NODE_TAG} failed."
return 1
fi
if nvm version "${CURRENT_VERSION}" &>/dev/null; then
nvm uninstall "${CURRENT_VERSION}"
fi
else
echo "Node version ${NODE_TAG} alreday installed."
fi
}
install_npm() {
if ! has npm; then
nvm use default
has npm || (echo "Npm command not found." && return 1)
fi
unset HTTP_PROXY
unset HTTPS_PROXY
unset ALL_PROXY
unset http_proxy
unset https_proxy
unset all_proxy
npm install -g npm
npm install -g fkill-cli
npm install -g ramda-cli
npm install -g typescript
npm install -g eslint
npm install -g pm2
}
install_exa() {
install_rust exa
# local tag=${1:-"latest"}
# [[ "${tag}" == "-f" ]] && tag="latest" && local force="YES"
# local result="exa-linux-x86_64-${tag##v}.zip"
# local url="https://github.com/ogham/exa/releases/download/${tag}/${result}"
# if [[ "${tag}" == "latest" ]]; then
# local data="$(get_tag_url "ogham/exa" "${tag}" "exa-linux-x86_64")"
# url="${data%% *}"
# tag="${data##* }"
# if [[ -z "${force}" ]] && check_current_tag "exa" "${tag}"; then
# return
# fi
# result="${result//latest/${tag}}"
# fi
# echo "Installing exa ${tag}."
# local dir="${LIB}/exa"
# _download "${url}" "${dir}" "${result}"
# _link "${dir}" "exa"
}
install_go() {
GO111MODULE=on go install "github.com/$1@latest"
}
install_baidu() {
install_go qjfoidnh/BaiduPCS-Go
}
install_glow() {
install_go charmbracelet/glow
}
install_rclone() {
install_go rclone/rclone
}
install_v2sub() {
install_go ThomasZN/v2sub
}
install_fx() {
install_go antonmedv/fx
}