forked from tegonal/github-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-shfmt.sh
More file actions
executable file
·88 lines (79 loc) · 3.1 KB
/
install-shfmt.sh
File metadata and controls
executable file
·88 lines (79 loc) · 3.1 KB
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
#!/usr/bin/env bash
#
# __ __
# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts
# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com>
# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Apache License 2.0
# /___/ Please report bugs and contribute back your improvements
#
# Version: v4.11.0
####### Description #############
#
# installs shfmt v3.12.0_linux_amd64 into $HOME/.local/bin
#
####### Usage ###################
#
# # run the install-shfmt.sh in your github/gitlab workflow
# # for instance, assuming you fetched this file via gt and remote name is tegonal-scripts
# # then in a github workflow you would have
#
# jobs:
# steps:
# - name: install shfmt
# run: ./lib/tegonal-scripts/src/ci/install-shfmt.sh
# # and most likely as well
# - name: run shfmt
# run: ./scripts/run-shfmt.sh
#
###################################
set -euo pipefail
shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
unset CDPATH
function logError() {
local -r msg=$1
shift 1 || traceAndDie "could not shift by 1"
# shellcheck disable=SC2059
printf >&2 "\033[0;31mERROR\033[0m: $msg\n" "$@"
}
function die() {
logError "$@"
exit 1
}
currentDir=$(pwd)
tmpDir=$(mktemp -d -t download-shfmt-XXXXXXXXXX) || die "could not create a temp directory"
cd "$tmpDir"
shfmtVersion="v3.12.0"
binFile="shfmt_${shfmtVersion}_linux_amd64"
expectedSha="d9fbb2a9c33d13f47e7618cf362a914d029d02a6df124064fff04fd688a745ea $binFile"
echo "$expectedSha" >"$binFile.sha256"
url="https://github.com/mvdan/sh/releases/download/$shfmtVersion/$binFile"
echo "going to download shfmt $shfmtVersion from: $url"
if command -v curl >/dev/null; then
curl --fail -L -O "$url" || die "could not download shfmt"
else
# if curl does not exist, then we try it with wget
wget --no-verbose "$url"
fi
sha256sum -c "$binFile.sha256" || {
actualSha="$(sha256sum "$binFile")"
die "checksum did not match, aborting\nexpected:\n%s\ngiven :\n%s" "$expectedSha" "$actualSha"
}
chmod +x "./$binFile" || die "could not make shfmt executable"
shfmtInTmp="$tmpDir/$binFile"
homeLocalBin="$HOME/.local/bin"
shfmtBin="$homeLocalBin/shfmt"
mkdir -p "$homeLocalBin" || die "was not able to create the bin directory %s" "$homeLocalBin"
if [[ -f "$shfmtBin" ]]; then
echo "going to remove the existing installation in $homeLocalBin"
rm "$shfmtBin" || die "was not able to remove a previous installation in %s" "$homeLocalBin"
fi
mv "$shfmtInTmp" "$shfmtBin"
cd "$currentDir"
shfmtPath=$(command -v shfmt)
if [[ $shfmtPath != "$shfmtBin" ]]; then
shfmtCurrentVersion=$(shfmt --version)
logError "was able to install shfmt in %s but \`command -v shfmt\` returns another path:\n%s\nFollowing the output of \`shfmt --version\`:\n" "$shfmtBin" "$shfmtPath" "$shfmtCurrentVersion"
else
shfmt --version
printf "\033[0;32mSUCCESS\033[0m: installed shfmt %s in %s\n" "$shfmtVersion" "$homeLocalBin"
fi