-
Notifications
You must be signed in to change notification settings - Fork 0
/
hugow
executable file
·354 lines (304 loc) · 11.9 KB
/
hugow
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
#!/usr/bin/env sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Hugo Wrapper is the universal way to download and exectue Hugo binary
# https://github.com/khos2ow/hugo-wrapper
# ----------------------------------------------------------------------------
set -e
# hugo-wrapper command available flags
get_version=""
get_latest=false
upgrade=false
show_help=false
# hugo related commands to pass through the actual binary
HUGO_ARGS=""
while [ -n "$1" ]; do
case "$1" in
--get-version) get_version=$2; shift 2 ;;
--get-latest) get_latest=true; shift 1 ;;
--upgrade) upgrade=true; shift 1 ;;
-h | --help) show_help=true; shift 1 ;;
*) HUGO_ARGS="$HUGO_ARGS $1"; shift 1 ;;
esac
done
set -- $HUGO_ARGS
if [ "$upgrade" = true ]; then
if [ "$get_latest" = true -o -n "$get_version" ]; then
echo "Error: Flag --upgrade cannot be used alongside of --get-version or --get-latest"
exit 1
fi
else
if [ "$get_latest" = true -a -n "$get_version" ]; then
echo "Error: Flags --get-version and --get-latest cannot be used alongside eachother"
exit 1
fi
fi
# check which download command (wget or curl) is available.
DOWNLOAD_COMMAND=""
DOWNLOAD_OUTPUT=""
DOWNLOAD_SILENT=""
if command -v wget > /dev/null; then
DOWNLOAD_COMMAND="wget"
DOWNLOAD_OUTPUT="-O"
DOWNLOAD_SILENT="-q"
elif command -v curl > /dev/null; then
DOWNLOAD_COMMAND="curl"
DOWNLOAD_OUTPUT="-o"
DOWNLOAD_SILENT="-s"
else
echo "Error: Unable to find 'wget' or 'curl' command."
exit 1
fi
# OS type
os_type=""
case "`uname -s`" in
Darwin) os_type="macOS" ;;
Linux) os_type="Linux" ;;
DragonFly) os_type="DragonFlyBSD" ;;
FreeBSD) os_type="FreeBSD" ;;
NetBSD) os_type="NetBSD" ;;
OpenBSD) os_type="OpenBSD" ;;
# CYGWIN* os_type="Windows" ;;
# MINGW*) os_type="Windows" ;;
# Windows_NT) os_type="Windows" ;;
esac
# OS architecture
os_arch=""
case "`uname -m`" in
x86) os_arch="32bit" ;;
x86_64) os_arch="64bit" ;;
amd64) os_arch="64bit" ;;
armv7l) os_arch="ARM" ;;
armv8) os_arch="ARM64" ;;
esac
if [ -z "$os_type" -o -z "$os_arch" ]; then
echo "Error: Unknown OS type or architecture"
exit 1
fi
# ----------------------------------------------------------------------------
# traverses directory structure from process work directory to filesystem root
# first directory with .hugo subdirectory is considered project base directory
# ----------------------------------------------------------------------------
find_basedir() {
if [ -z "$1" ]; then
echo "Error: Path not specified to find_basedir"
return 1
fi
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ]; do
if [ -d "$wdir"/.hugo ]; then
basedir=$wdir
break
fi
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
done
echo "${basedir}"
}
BASE_DIR=`find_basedir "$(pwd)"`
if [ -z "$BASE_DIR" ]; then
echo "Error: Unable to find base directory."
exit 1
fi
if [ ! -d "$BASE_DIR/.hugo" ]; then
mkdir -p "$BASE_DIR/.hugo"
else
if [ -r "$BASE_DIR/.hugo/hugo" -a ! -s "$BASE_DIR/.hugo/hugo" ]; then
rm "$BASE_DIR/.hugo/hugo"
fi
if [ -r "$BASE_DIR/.hugo/version" -a ! -s "$BASE_DIR/.hugo/version" ]; then
rm "$BASE_DIR/.hugo/version"
fi
fi
parse_json() {
local json="$1"
local field="$2"
if [ -z "$json" ]; then
echo ""
elif [ -z "$field" ]; then
echo ""
fi
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $field`
echo ${temp##*|} | sed "s/${field}: //g"
}
perform_checksum() {
if [ -n "$1" ]; then
if command -v sha256sum > /dev/null; then
echo "$1" | sha256sum --check > /dev/null
elif command -v shasum > /dev/null; then
echo "$1" | shasum --algorithm 256 --check > /dev/null
elif command -v cksum > /dev/null; then
echo "$1" | cksum -a SHA256 -c > /dev/null
else
echo "Error: cannot find any checksum command"
exit 1
fi
fi
}
remove_file() {
if [ -n "$1" -a "$1" != "/" -a -f "$1" -a -r "$1" ] ; then
rm "$1"
fi
}
download_version() {
versionToDownload="$1"
if [ -n "$versionToDownload" ]; then
if [ "$versionToDownload" = "LATEST" ]; then
latest_release=`$DOWNLOAD_COMMAND $DOWNLOAD_SILENT ${DOWNLOAD_OUTPUT}- https://api.github.com/repos/gohugoio/hugo/releases/latest`
versionToDownload=`parse_json "$latest_release" "tag_name"`
fi
# strip down 'v' from begining of the string if exists
versionToDownload=`echo $versionToDownload | sed -ne 's/[^0-9]*\(\([0-9]*\.\)\{0,4\}[0-9]*[^.]\).*/\1/p'`
printf "downloading hugo binary version v${versionToDownload} ... "
# download for specific OS and architecture
local binaryUrl="https://github.com/gohugoio/hugo/releases/download/v${versionToDownload}/hugo_${versionToDownload}_${os_type}-${os_arch}.tar.gz"
local checksumUrl="https://github.com/gohugoio/hugo/releases/download/v${versionToDownload}/hugo_${versionToDownload}_checksums.txt"
local tarballName="hugo_${versionToDownload}_${os_type}-${os_arch}.tar.gz"
local tarballPath="$BASE_DIR/.hugo/${tarballName}"
local checksumName="checksum.txt"
local checksumPath="$BASE_DIR/.hugo/${checksumName}"
$DOWNLOAD_COMMAND $DOWNLOAD_SILENT $DOWNLOAD_OUTPUT "$tarballPath" "$binaryUrl" &
$DOWNLOAD_COMMAND $DOWNLOAD_SILENT $DOWNLOAD_OUTPUT "$checksumPath" "$checksumUrl" &
wait
if [ -s "$tarballPath" -a -s "$checksumPath" ]; then
printf "[done]\n"
else
printf "[failed]\n"
remove_file "$checksumPath"
remove_file "$tarballPath"
exit 1
fi
printf "verifying hugo binary version v${versionToDownload} ..... "
cd $BASE_DIR/.hugo/
grep "${tarballName}" "$BASE_DIR/.hugo/$checksumName" | perform_checksum
cd - > /dev/null 2>&1
wait
printf "[done]\n"
printf "unzipping hugo binary version v${versionToDownload} ..... "
if [ -f "${tarballPath}" -a -r "${tarballPath}" ]; then
tar --directory="$BASE_DIR/.hugo/" -xzf "${tarballPath}" 2>&1
wait
printf "[done]\n"
else
printf "[failed]\n"
remove_file "$checksumPath"
remove_file "$tarballPath"
exit 1
fi
# save the downloaded binary version into $BASE_DIR/.hugo/version
echo "$versionToDownload" > $BASE_DIR/.hugo/version
# cleanup after extraction
remove_file "$checksumPath"
remove_file "$tarballPath"
remove_file "$BASE_DIR/.hugo/LICENSE"
remove_file "$BASE_DIR/.hugo/README.md"
fi
}
# ----------------------------------------------------------------------------
# upgrade hugo wrapper binary and save it as ${BASE_DIR}/hugow
# ----------------------------------------------------------------------------
if [ "$upgrade" = true ]; then
printf "downloading hugow binary ... "
$DOWNLOAD_COMMAND $DOWNLOAD_SILENT $DOWNLOAD_OUTPUT "hugow" "https://raw.githubusercontent.com/khos2ow/hugo-wrapper/master/hugow" &
wait
printf "[done]\n"
chmod +x hugow
exit
fi
# ----------------------------------------------------------------------------
# download hugo binary and save it as ${BASE_DIR}/.hugo/hugo
# ----------------------------------------------------------------------------
if [ -r "$BASE_DIR/.hugo/hugo" ]; then
current_binary_version="$($BASE_DIR/.hugo/hugo version | sed -ne 's/[^0-9]*\(\([0-9]*\.\)\{0,4\}[0-9]*[^.]\).*/\1/p' | sed 's/^ *//;s/ *$//')"
# download hugo binary and save it as ${BASE_DIR}/.hugo/hugo
if [ -n "$get_version" ]; then
if [ "$get_version" != "$current_binary_version" ]; then
# specified hugo version
download_version "$get_version"
else
echo "hugo binary version $get_version already exists"
fi
elif [ $get_latest = true ]; then
latest_release=`$DOWNLOAD_COMMAND $DOWNLOAD_SILENT ${DOWNLOAD_OUTPUT}- https://api.github.com/repos/gohugoio/hugo/releases/latest`
latest_version=`parse_json "$latest_release" "tag_name" | sed -ne 's/[^0-9]*\(\([0-9]*\.\)\{0,4\}[0-9]*[^.]\).*/\1/p'`
if [ "$latest_version" != "$current_binary_version" ]; then
# latest hugo version
download_version "$latest_version"
else
echo "latest hugo binary version $latest_version already exists"
fi
elif [ -r "$BASE_DIR/.hugo/version" ]; then
current_file_version="$(cat "$BASE_DIR/.hugo/version")"
if [ "$current_file_version" != "$current_binary_version" ]; then
# specified hugo version
download_version "$current_file_version"
fi
else
# save the current binary version into $BASE_DIR/.hugo/version
echo "$current_binary_version" > $BASE_DIR/.hugo/version
fi
else
if [ -n "$get_version" ]; then
# specified hugo version
download_version "$get_version"
elif [ $get_latest = true ]; then
# latest hugo version
download_version "LATEST"
elif [ -r "$BASE_DIR/.hugo/version" ]; then
# specified hugo version
download_version "$(cat "$BASE_DIR/.hugo/version")"
else
# latest hugo version
download_version "LATEST"
fi
fi
# ----------------------------------------------------------------------------
# only download binary and not execute hugo related command
# ----------------------------------------------------------------------------
if [ "$get_latest" = true -o -n "$get_version" ]; then
${BASE_DIR}/.hugo/hugo version
exit
fi
# ----------------------------------------------------------------------------
# Show help, both from hugow and ${BASE_DIR}/.hugo/hugo
# ----------------------------------------------------------------------------
if [ $show_help = true ]; then
help=$(${BASE_DIR}/.hugo/hugo --help)
cat << USAGE
hugow is the universal wrapper for hugo main command.
Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go.
Complete documentation is available at http://gohugo.io/.
Flags:
--get-latest get latest version of hugo binary.
--get-version string get specified version of hugo binary.
--upgrade upgrade hugo wrapper binary itself.
-h, --help help for hugo-wrapper
--------
$help
USAGE
exit 0
fi
# ----------------------------------------------------------------------------
# pass commands and flags to actual hugo binary
# ----------------------------------------------------------------------------
${BASE_DIR}/.hugo/hugo "$@"