forked from dotenv-linter/dotenv-linter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
190 lines (150 loc) · 4.01 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
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
#!/bin/sh
set -e
DOTENV_LINTER="dotenv-linter"
DOTENV_LINTER_REPO="${DOTENV_LINTER}/${DOTENV_LINTER}"
DOTENV_LINTER_GITHUB="https://github.com/${DOTENV_LINTER_REPO}"
DOTENV_LINTER_RELEASES="${DOTENV_LINTER_GITHUB}/releases"
usage() {
require_cmd cat
this=$1
cat 1>&2 <<EOF
$this: download binaries for dotenv-linter
USAGE:
$this [FLAGS] [OPTIONS] <tag>
FLAGS:
-h, --help Prints help information
OPTIONS:
-b, --bindir <DIR_PATH> Sets bindir or installation directory. Defaults to ./bin
ARGS:
<tag> is a tag from ${DOTENV_LINTER_RELEASES}. If tag is missing, then the latest will be used.
EOF
exit 2
}
parse_args() {
BINDIR=${BINDIR:-./bin}
while [ "$#" -gt 0 ]; do
case $1 in
-h|--help)
usage "$0"
shift # past argument
;;
-b|--bindir)
BINDIR="$2"
shift # past argument
shift # past value
;;
*) TAG=$1
shift # past argument
;;
esac
done
}
main() {
parse_args "$@"
require_cmd uname
require_cmd mktemp
require_cmd grep
require_cmd rm
get_architecture || return 1
_arch="$RETVAL"
case $_arch in
win-*) _ext=".zip" ;;
*) _ext=".tar.gz" ;;
esac
_archive_name="${DOTENV_LINTER}-${_arch}${_ext}"
if [ -z "${TAG}" ]; then
println "The latest version will be installed."
_url="${DOTENV_LINTER_RELEASES}/latest/download/${_archive_name}"
else
println "Version ${TAG} will be installed"
_url="${DOTENV_LINTER_RELEASES}/download/${TAG}/${_archive_name}"
fi
# Installation
_temp_dir=$(mktemp -d)
_archive_path="${_temp_dir}/${_archive_name}"
download "${_url}" "${_archive_path}" || return 1
case $_archive_path in
*.zip)
require_cmd unzip
unzip "${_archive_path}" -d "${_temp_dir}"
_file_name="${DOTENV_LINTER}.exe"
;;
*) require_cmd tar
tar -xzf "${_archive_path}" -C "${_temp_dir}"
_file_name="${DOTENV_LINTER}"
;;
esac
test ! -d "${BINDIR}" && install -d "${BINDIR}"
install "${_temp_dir}/${_file_name}" "${BINDIR}/"
_exe_path=${BINDIR}/${_file_name}
println "Successfully installed $($_exe_path -v) to ${_exe_path}"
rm -rf "${_temp_dir}"
return 0;
}
get_architecture() {
_ostype="$(uname -s)"
_cputype="$(uname -m)"
_clibtype="gnu"
if [ "$_cputype" != "x86_64" ]; then
err "Error: Unsupported architecture $_cputype. Only x86_64 binaries are available."
fi
if [ "$_ostype" = "Linux" ]; then
if ldd --version 2>&1 | grep -q 'musl'; then
_clibtype="musl"
fi
fi
case "$_ostype" in
Linux)
if [ "$_clibtype" != "musl" ]; then
_ostype=linux
else
# For 'musl' architecture we release alpine version
_ostype=alpine
fi
;;
Darwin)
_ostype=darwin
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) # windows systems
_ostype=win
_cputype="x64"
;;
*)
err "Unsupported OS type: $_ostype"
;;
esac
RETVAL="${_ostype}-${_cputype}"
}
# $1 - url for download. $2 - path to download
# Wrapper function for curl/wget
download() {
if [ ! $# -eq 2 ]; then
err "URL or target path not specified"
fi
if cmd_exists curl; then
curl -sSfL "$1" -o "$2"
elif cmd_exists wget; then
wget -q "$1" -O "$2"
else
err "Not found download command. 'curl' or 'wget' is required."
fi
if [ $# -eq 2 ] && [ ! -f "$2" ]; then
err "Failed to download file $1"
fi
}
require_cmd() {
if ! cmd_exists "$1"; then
err "'$1' is required (command not found)."
fi
}
cmd_exists() {
command -v "$1" > /dev/null 2>&1
}
err() {
println "$1" >&2
exit 1
}
println() {
printf '%s installer: %s\n' "${DOTENV_LINTER}" "$1"
}
main "$@" || exit 1