-
Notifications
You must be signed in to change notification settings - Fork 6
/
install.sh
131 lines (106 loc) · 3.59 KB
/
install.sh
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
function printf()
{
local os=`uname`
if [ "${os}" = 'Darwin' ]; then
echo "${@}"
else
echo -e "${@}"
fi
}
function check_cmd_exists()
{
local cmd="${1}"
if ! [ -x "$(command -v ${cmd})" ]; then
echo "[:(] '"${cmd}"' not found, please install it first" >&2
return 1
fi
}
function download_bin()
{
local repo="${1}"
local bin_name="${2}"
local dir="${3}"
local token="Authorization: token ${4}"
local os_type=`uname | awk '{print tolower($0)}'`
echo "[:-] detected os type: ${os_type}" >&2
local long_name="pre-builtin bin '${bin_name}' on '${repo}' for '${os_type}'"
check_cmd_exists 'curl'
local json=`curl --proto '=https' --tlsv1.2 -sSf -H "${token}" "https://api.${repo}/releases/latest"`
local ver=`echo "${json}" | grep '"tag_name": ' | awk -F '"' '{print $(NF-1)}'`
if [ -z "${ver}" ]; then
echo "***" >&2
echo "${json}" >&2
echo "***" >&2
echo "[:(] ${long_name} version not found or can't be downloaded, exiting" >&2
return 1
fi
local info=`echo "${json}" | \
grep '"assets": ' -A 99999 | \
grep '"name": \|"browser_download_url": ' | \
{ grep "${bin_name}_${os_type}" || test $? = 1; } | \
awk -F '": "' '{print $2}' | \
awk -F '"' '{print $1}'`
local res_name=`echo "${info}" | { grep -v 'https://' || test $? = 0; }`
if [ -z "${res_name}" ]; then
echo "[:(] ${long_name} not found, exiting" >&2
return 1
fi
local cnt=`echo "${res_name}" | wc -l | awk '{print $1}'`
if [ "${cnt}" != '1' ]; then
echo "***" >&2
echo "${res_name}" | awk '{print " - "$0}' >&2
echo "***" >&2
echo "[:(] error: more than one (${cnt}) resource of ${long_name}, exiting" >&2
return 1
fi
local download_url=`echo "${info}" | tail -n 1`
local hash_val=`echo "${res_name}" | awk -F '_' '{print $NF}' | awk -F '.' '{print $1}'`
local hash_bin=`echo "${res_name}" | awk -F '_' '{print $(NF-1)}'`
local bin_path="${dir}/${bin_name}"
echo "[:)] located version ${ver}: ${long_name}"
echo " - ${hash_bin}: ${hash_val}"
echo " - url: ${download_url}"
echo " - download to: ${bin_path}"
curl --proto '=https' --tlsv1.2 -sSf -kL -H "${token}" "${download_url}" > "${bin_path}"
chmod +x "${bin_path}"
echo "[:)] downloaded"
}
function download_and_install_ticat()
{
local download_rate_limit_token="ghp_${1}"
if [ -z "${2+x}" ]; then
local mods_repo=''
else
local mods_repo="${2}"
fi
check_cmd_exists 'git'
check_cmd_exists 'awk'
local title='\033[1;94m'
local green='\033[0;32m'
local gray='\033[38;5;8m'
local gray='\033[0;35m'
local orange='\033[0;33m'
local nc='\033[0m'
local ticat_repo='github.com/repos/innerr/ticat'
printf "${title}==> download ticat${nc}"
download_bin "${ticat_repo}" 'ticat' '.' "${download_rate_limit_token}" 2>&1 | awk '{print " * "$0}'
echo
if [ ! -z "${mods_repo}" ]; then
printf "${title}==> fetch components from '${mods_repo}'${nc}"
./ticat display.color.on : display.utf.off : display.width 90 : hub.add "${mods_repo}" 2>&1 | awk '{print " * "$0}'
else
printf "${title}==> init ticat with basic component repos${nc}"
./ticat display.color.on : display.utf.off : display.width 90 : noop 2>&1 | awk '{print " * "$0}'
fi
echo
printf "${title}==> add ticat to \$PATH${nc}"
./ticat display.color.on : install.ticat 2>&1 | awk '{print " * "$0}'
echo
printf "${green}==> Command ${orange}./ticat${green} is ready now, ${orange}ticat${green} is available after relogin${nc}"
printf " ${gray}ticat: workflow automating in unix-pipe style${nc}"
printf " ${gray}try: $>./ticat${nc}"
}
set -eo pipefail
mods_repo="${1}"
set -u
download_and_install_ticat 'NYrOv0JuQ8iZ6cEnOTzdaTfh7ovx2Q2iwEQX' "${mods_repo}"