Skip to content

Commit 6c207f2

Browse files
Add git fallback to template
1 parent e1b9c99 commit 6c207f2

File tree

2 files changed

+113
-20
lines changed

2 files changed

+113
-20
lines changed

go-template

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,53 @@ declare GO_SCRIPT_BASH_REPO_URL="${GO_SCRIPT_BASH_REPO_URL:-https://github.com/m
4949
# URL with the release files
5050
declare GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_DOWNLOAD_URL:-${GO_SCRIPT_BASH_REPO_URL%.git}/archive}/$GO_SCRIPT_BASH_VERSION.tar.gz"
5151

52-
5352
if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
54-
printf "Downloading framework from '%s'...\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"
55-
curl -LfsS "$GO_SCRIPT_BASH_DOWNLOAD_URL" | tar -xz
56-
if [[ ${PIPESTATUS[0]} -ne 0 ]] || [[ ${PIPESTATUS[1]:1} -ne 0 ]] ; then
57-
printf "Failed to download from '%s'; aborting.\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL" >&2
58-
exit 1
59-
fi
60-
if ! mkdir -p $GO_SCRIPTS_DIR; then
61-
printf "Faild to create scripts dir '%s'" $GO_SCRIPTS_DIR >&2
62-
exit 2
63-
fi
64-
if ! mv go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER $GO_SCRIPT_BASH_CORE_DIR; then
65-
printf "Failed to install downloaded directory in '%s'\n" $GO_SCRIPT_BASH_CORE_DIR >&2
66-
exit 3
53+
declare PIPEFAIL_BACKUP
54+
PIPEFAIL_BACKUP=$(shopt -op | grep pipefail)
55+
set -o pipefail
56+
57+
# Using a function to allow for multiple return points
58+
curl_download(){
59+
if ! command curl -V >/dev/null; then
60+
printf "Failed to find cURL or tar\n"
61+
return 1
62+
fi
63+
if ! command tar -h >/dev/null; then
64+
printf "Failed to find cURL or tar\n"
65+
return 1
66+
fi
67+
printf "Downloading framework from '%s'...\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"
68+
if ! curl -LfsS "$GO_SCRIPT_BASH_DOWNLOAD_URL" | tar -xz ; then
69+
printf "Failed to download from '%s'.\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL" >&2
70+
return 1
71+
fi
72+
if ! mkdir -p $GO_SCRIPTS_DIR ; then
73+
printf "Failed to create scripts dir '%s'" $GO_SCRIPTS_DIR >&2
74+
rm -rf go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER
75+
return 1
76+
fi
77+
if ! mv go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER $GO_SCRIPT_BASH_CORE_DIR; then
78+
printf "Failed to install downloaded directory in '%s'\n" $GO_SCRIPT_BASH_CORE_DIR >&2
79+
rm -rf go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER
80+
return 1
81+
fi
82+
printf "Download of '%s' successful.\n\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"
83+
return 0
84+
}
85+
86+
if ! curl_download; then
87+
printf "Using git clone as fallback\n"
88+
printf "Cloning framework from '%s'...\n" "$GO_SCRIPT_BASH_REPO_URL"
89+
if ! git clone --depth 1 -c advice.detachedHead=false \
90+
-b "$GO_SCRIPT_BASH_VERSION" "$GO_SCRIPT_BASH_REPO_URL" \
91+
"$GO_SCRIPT_BASH_CORE_DIR"; then
92+
printf "Failed to clone '%s'; aborting.\n" "$GO_SCRIPT_BASH_REPO_URL" >&2
93+
$PIPEFAIL_BACKUP
94+
exit 1
95+
fi
96+
printf "Clone of '%s' successful.\n\n" "$GO_SCRIPT_BASH_REPO_URL"
6797
fi
68-
printf "Download of '%s' successful.\n\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"
98+
$PIPEFAIL_BACKUP
6999
fi
70100

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

tests/template.bats

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ setup() {
1111
test_filter
1212
export GO_SCRIPT_BASH_VERSION="$_GO_CORE_VERSION"
1313
export GO_SCRIPTS_DIR="$_GO_TEST_DIR/tmp/go-template-test-scripts"
14+
export GO_SCRIPT_BASH_REPO_URL="https://github.com/mbland/go-script-bash.git"
15+
export GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_REPO_URL%.git}/archive"
1416
}
1517

1618
teardown() {
@@ -24,7 +26,7 @@ teardown() {
2426
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
2527
}
2628

27-
@test "$SUITE: download the go-script-bash release from $GO_CORE_URL" {
29+
@test "$SUITE: download the go-script-bash release from $GO_SCRIPT_BASH_REPO_URL" {
2830
run "$_GO_CORE_DIR/go-template"
2931

3032
# Without a command argument, the script will print the top-level help and
@@ -42,16 +44,77 @@ teardown() {
4244

4345
@test "$SUITE: fail to download a nonexistent repo" {
4446
GO_SCRIPT_BASH_REPO_URL='bogus-repo-that-does-not-exist' \
47+
GO_SCRIPT_BASH_DOWNLOAD_URL='bogus-url-that-does-not-exist' \
4548
run "$_GO_CORE_DIR/go-template"
46-
assert_failure "Downloading framework from 'bogus-repo-that-does-not-exist/archive/$GO_SCRIPT_BASH_VERSION.tar.gz'..." \
47-
"curl: (6) Could not resolve host: bogus-repo-that-does-not-exist" \
48-
"Failed to download from 'bogus-repo-that-does-not-exist/archive/$GO_SCRIPT_BASH_VERSION.tar.gz'; aborting."
49+
assert_failure "Downloading framework from 'bogus-url-that-does-not-exist/$GO_SCRIPT_BASH_VERSION.tar.gz'..." \
50+
"curl: (6) Could not resolve host: bogus-url-that-does-not-exist" \
51+
"Failed to download from 'bogus-url-that-does-not-exist/$GO_SCRIPT_BASH_VERSION.tar.gz'." \
52+
"Using git clone as fallback" \
53+
"Cloning framework from 'bogus-repo-that-does-not-exist'..." \
54+
"fatal: repository 'bogus-repo-that-does-not-exist' does not exist" \
55+
"Failed to clone 'bogus-repo-that-does-not-exist'; aborting."
4956
}
5057

5158
@test "$SUITE: fail to download a nonexistent version" {
5259
GO_SCRIPT_BASH_VERSION='vnonexistent' \
5360
run "$_GO_CORE_DIR/go-template"
5461
assert_failure "Downloading framework from 'https://github.com/mbland/go-script-bash/archive/vnonexistent.tar.gz'..." \
5562
"curl: (22) The requested URL returned error: 404 Not Found" \
56-
"Failed to download from 'https://github.com/mbland/go-script-bash/archive/vnonexistent.tar.gz'; aborting."
63+
"Failed to download from 'https://github.com/mbland/go-script-bash/archive/vnonexistent.tar.gz'." \
64+
"Using git clone as fallback" \
65+
"Cloning framework from 'https://github.com/mbland/go-script-bash.git'..." \
66+
"Cloning into '/Users/paquete/octobot/repos/go-script-bash/tests/tmp/go-template-test-scripts/go-script-bash'..." \
67+
"warning: Could not find remote branch vnonexistent to clone." \
68+
"fatal: Remote branch vnonexistent not found in upstream origin" \
69+
"Failed to clone 'https://github.com/mbland/go-script-bash.git'; aborting."
5770
}
71+
72+
@test "$SUITE: fail to find curl uses git clone" {
73+
PATH="$BATS_TEST_BINDIR:$PATH"
74+
stub_program_in_path curl "exit 1"
75+
run "$_GO_CORE_DIR/go-template"
76+
restore_program_in_path curl
77+
78+
# Without a command argument, the script will print the top-level help and
79+
# return an error, but the core repo should exist as expected.
80+
assert_output_matches "Failed to find cURL or tar"
81+
assert_output_matches "Using git clone as fallback"
82+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
83+
84+
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
85+
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
86+
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
87+
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
88+
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
89+
[[ -f "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash/go-core.bash" ]]
90+
91+
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
92+
run git log --oneline -n 1
93+
assert_success
94+
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
95+
}
96+
97+
@test "$SUITE: fail to find tar uses git clone" {
98+
PATH="$BATS_TEST_BINDIR:$PATH"
99+
stub_program_in_path tar "exit 1"
100+
run "$_GO_CORE_DIR/go-template"
101+
restore_program_in_path tar
102+
103+
# Without a command argument, the script will print the top-level help and
104+
# return an error, but the core repo should exist as expected.
105+
assert_output_matches "Failed to find cURL or tar"
106+
assert_output_matches "Using git clone as fallback"
107+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
108+
109+
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
110+
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
111+
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
112+
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
113+
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
114+
[[ -f "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash/go-core.bash" ]]
115+
116+
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
117+
run git log --oneline -n 1
118+
assert_success
119+
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
120+
}

0 commit comments

Comments
 (0)