Skip to content

Commit de7ce6c

Browse files
authored
Merge pull request #180 from mbland/go-template-download
Download release file instead of cloning repo in go-template (continued from #179)
2 parents 779f544 + 96af8f4 commit de7ce6c

File tree

2 files changed

+410
-31
lines changed

2 files changed

+410
-31
lines changed

go-template

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# description. You may remove any other comments from this template as well.
77
#
88
# This template automatically checks for the presence of the go-script-bash
9-
# sources and makes a shallow clone of the the go-script-bash repository if
10-
# necessary before dispatching commands. (If you prefer, you can change the
11-
# logic to create a regular clone instead.) This allows users to set up the
9+
# sources and downloads the go-script-bash repository contents if necessary
10+
# before dispatching commands. (If you prefer, you can change the logic to
11+
# create a shallow or regular clone instead.) This allows users to set up the
1212
# framework without taking any extra steps when running the command for the
1313
# first time, without the need to commit the framework to your repository.
1414
#
@@ -43,15 +43,81 @@ declare GO_SCRIPT_BASH_CORE_DIR="${GO_SCRIPT_BASH_CORE_DIR:-${0%/*}/$GO_SCRIPTS_
4343
# The URL of the go-script-bash framework sources
4444
declare GO_SCRIPT_BASH_REPO_URL="${GO_SCRIPT_BASH_REPO_URL:-https://github.com/mbland/go-script-bash.git}"
4545

46-
if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
46+
# URL with the release files
47+
declare GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_DOWNLOAD_URL:-${GO_SCRIPT_BASH_REPO_URL%.git}/archive}/$GO_SCRIPT_BASH_VERSION.tar.gz"
48+
49+
# Downloads `GO_SCRIPT_BASH_VERSION` as a tar.gz file and unpacks it.
50+
download_go_script_bash_tarball() {
51+
# GitHub removes the leading 'v' from the archive's output directory.
52+
local unpacked_dir="go-script-bash-${GO_SCRIPT_BASH_VERSION#v}"
53+
local unpacked_core="$unpacked_dir/go-core.bash"
54+
local core_dir_parent="${GO_SCRIPT_BASH_CORE_DIR%/*}"
55+
local url="$GO_SCRIPT_BASH_DOWNLOAD_URL"
56+
local protocol="${url%%://*}"
57+
local download_cmd=()
58+
59+
if [[ "$protocol" == "$url" ]]; then
60+
printf 'GO_SCRIPT_BASH_DOWNLOAD_URL has no protocol: %s\n' "$url" >&2
61+
return 1
62+
elif [[ "$(git --version)" =~ windows && "$protocol" == 'file' ]]; then
63+
url="file://$(cygpath -m "${url#file://}")"
64+
fi
65+
66+
if command -v curl >/dev/null; then
67+
download_cmd=(curl -LfsS "$url")
68+
elif command -v fetch >/dev/null; then
69+
download_cmd=(fetch -o - "$url")
70+
elif [[ "$protocol" == 'file' ]] && command -v cat; then
71+
# `wget` can't handle 'file://' urls. Though input redirection would work
72+
# below, this method is consistent with the process substitution logic.
73+
download_cmd=(cat "${url#file://}")
74+
elif command -v wget >/dev/null; then
75+
download_cmd=(wget -O - "$url")
76+
else
77+
printf "Failed to find cURL, wget, or fetch\n" >&2
78+
return 1
79+
fi
80+
81+
if ! command -v tar >/dev/null; then
82+
printf "Failed to find tar\n" >&2
83+
return 1
84+
fi
85+
printf "Downloading framework from '%s'...\n" "$url"
86+
87+
if ! tar -xzf <("${download_cmd[@]}") || [[ ! -f "$unpacked_core" ]]; then
88+
printf "Failed to download from '%s'.\n" "$url" >&2
89+
return 1
90+
elif [[ ! -d "$core_dir_parent" ]] && ! mkdir -p "$core_dir_parent" ; then
91+
printf "Failed to create scripts dir '%s'\n" "$core_dir_parent" >&2
92+
rm -rf "$unpacked_dir"
93+
return 1
94+
elif ! mv "$unpacked_dir" "$GO_SCRIPT_BASH_CORE_DIR"; then
95+
printf "Failed to install downloaded directory in '%s'\n" \
96+
"$GO_SCRIPT_BASH_CORE_DIR" >&2
97+
rm -rf "$unpacked_dir"
98+
return 1
99+
fi
100+
printf "Download of '%s' successful.\n\n" "$url"
101+
}
102+
103+
git_clone_go_script_bash() {
47104
printf "Cloning framework from '%s'...\n" "$GO_SCRIPT_BASH_REPO_URL"
48105
if ! git clone --depth 1 -c advice.detachedHead=false \
49106
-b "$GO_SCRIPT_BASH_VERSION" "$GO_SCRIPT_BASH_REPO_URL" \
50107
"$GO_SCRIPT_BASH_CORE_DIR"; then
51108
printf "Failed to clone '%s'; aborting.\n" "$GO_SCRIPT_BASH_REPO_URL" >&2
52-
exit 1
109+
return 1
53110
fi
54111
printf "Clone of '%s' successful.\n\n" "$GO_SCRIPT_BASH_REPO_URL"
112+
}
113+
114+
if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
115+
if ! download_go_script_bash_tarball; then
116+
printf "Using git clone as fallback\n"
117+
if ! git_clone_go_script_bash; then
118+
exit 1
119+
fi
120+
fi
55121
fi
56122

57123
. "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" "$GO_SCRIPTS_DIR"

0 commit comments

Comments
 (0)