-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·102 lines (88 loc) · 2.1 KB
/
install
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
#!/bin/bash
# Author : Gcaufy
# Date : 2020/03/01
set -e
VERSION="1.0.6"
SHELLS="bash zsh fish"
PREFIX="${HOME}/.sshman"
for s in $SHELLS; do
if ! command -v "$s" > /dev/null; then
SHELLS=${SHELLS/$s/}
fi
done
if [[ ${#SHELLS} -lt 3 ]]; then
echo "No shell configuration to be updated."
exit 0
fi
function git_install() {
if [[ -d "${PREFIX}" ]]; then
cd "${PREFIX}"
git pull
else
git clone https://github.com/Gcaufy/sshman.git "${PREFIX}"
cd "${PREFIX}"
fi
git checkout v${VERSION}
}
function tar_install() {
local _url
_url="https://github.com/Gcaufy/sshman/archive/v${VERSION}.tar.gz"
echo -n "Downloading package from: $_url"
mkdir -p "${PREFIX}"
curl -sSL $_url | tar xz - --strip-components=1 -C "${PREFIX}"
}
function append_line() {
set -e
local update line file pat lno
update="$1"
line="$2"
file="$3"
pat="${4:-}"
lno=""
echo "Update $file:"
echo " - $line"
if [ -f "$file" ]; then
if [ $# -lt 4 ]; then
lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')
else
lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
fi
fi
if [ -n "$lno" ]; then
echo " - Already exists: line #$lno"
else
if [ "$update" -eq 1 ]; then
[ -f "$file" ] && echo >> "$file"
echo "$line" >> "$file"
echo " + Added"
else
echo " ~ Skipped"
fi
fi
echo
set +e
}
function install() {
if ! command -v "git" > /dev/null; then
tar_install
else
git_install
fi
echo -n "Update alias"
update_alias
echo -e "Install successfully. current version is: v$(cat $PREFIX/version.txt) \n"
echo -e 'Finished. Restart your shell or reload config file.'
[[ "$SHELLS" =~ bash ]] && echo -e ' source ~/.bashrc # bash'
[[ "$SHELLS" =~ zsh ]] && echo -e " source ${ZDOTDIR:-~}/.zshrc # zsh"
echo
echo
}
function update_alias() {
for shell in $SHELLS; do
[[ "$shell" = fish ]] && continue
[ "$shell" = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line 1 "alias s=\"'${PREFIX}/sshman'\"" "${dest}"
source "${dest}"
done
}
install