-
Notifications
You must be signed in to change notification settings - Fork 16
/
go-template
executable file
·125 lines (111 loc) · 4.93 KB
/
go-template
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
#! /usr/bin/env bash
#
# Template for a Bash program based on the go-script-bash framework
#
# You should replace this comment with your own program's high-level
# description. You may remove any other comments from this template as well.
#
# This template automatically checks for the presence of the go-script-bash
# sources and downloads the go-script-bash repository contents if necessary
# before dispatching commands. (If you prefer, you can change the logic to
# create a shallow or regular clone instead.) This allows users to set up the
# framework without taking any extra steps when running the command for the
# first time, without the need to commit the framework to your repository.
#
# Alternatively, you can make the `GO_SCRIPT_BASH_REPO_URL` a Git submodule of
# your project or check in a versioned copy of the sources. See the "How to use
# this framework" section of `README.md` for details.
#
# Make sure the variables within this script are configured as necessary for
# your program. You can add any other initialization or configuration between:
#
# . "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" "$GO_SCRIPTS_DIR"`
# `@go "$@"`
# Set to 'true' if your script is a standalone program, i.e. not bound to
# execute only from the directory in which it resides. See the "Standalone mode"
# section in README.md.
export _GO_STANDALONE=
# The path where your command scripts reside
#
# For `_GO_STANDALONE` programs and plugins containing command scripts, you may
# wish to set GO_SCRIPTS_DIR to `bin` and have a separate `./go` script to
# manage project tasks that finds its command scripts in `scripts`.
declare GO_SCRIPTS_DIR="${GO_SCRIPTS_DIR:-scripts}"
# The `GO_SCRIPT_BASH_REPO_URL` tag or branch you wish to use
declare GO_SCRIPT_BASH_VERSION="${GO_SCRIPT_BASH_VERSION:-v1.7.0}"
# The go-script-bash installation directory within your project
declare GO_SCRIPT_BASH_CORE_DIR="${GO_SCRIPT_BASH_CORE_DIR:-${0%/*}/$GO_SCRIPTS_DIR/go-script-bash}"
# The URL of the go-script-bash framework sources
declare GO_SCRIPT_BASH_REPO_URL="${GO_SCRIPT_BASH_REPO_URL:-https://github.com/mbland/go-script-bash.git}"
# URL with the release files
declare GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_DOWNLOAD_URL:-${GO_SCRIPT_BASH_REPO_URL%.git}/archive}/$GO_SCRIPT_BASH_VERSION.tar.gz"
# Downloads `GO_SCRIPT_BASH_VERSION` as a tar.gz file and unpacks it.
download_go_script_bash_tarball() {
# GitHub removes the leading 'v' from the archive's output directory.
local unpacked_dir="go-script-bash-${GO_SCRIPT_BASH_VERSION#v}"
local core_dir_parent="${GO_SCRIPT_BASH_CORE_DIR%/*}"
local url="$GO_SCRIPT_BASH_DOWNLOAD_URL"
local protocol="${url%%://*}"
local download_cmd=()
if [[ "$protocol" == "$url" ]]; then
printf 'GO_SCRIPT_BASH_DOWNLOAD_URL has no protocol: %s\n' "$url" >&2
return 1
elif [[ "$(git --version)" =~ windows && "$protocol" == 'file' ]]; then
url="file://$(cygpath -m "${url#file://}")"
fi
if command -v curl >/dev/null; then
download_cmd=(curl -LfsS "$url")
elif command -v fetch >/dev/null; then
download_cmd=(fetch -o - "$url")
elif [[ "$protocol" == 'file' ]] && command -v cat; then
# `wget` can't handle 'file://' urls. Though input redirection would work
# below, this method is consistent with the process substitution logic.
download_cmd=(cat "${url#file://}")
elif command -v wget >/dev/null; then
download_cmd=(wget -O - "$url")
else
printf "Failed to find cURL, wget, or fetch\n" >&2
return 1
fi
if ! command -v tar >/dev/null; then
printf "Failed to find tar\n" >&2
return 1
fi
printf "Downloading framework from '%s'...\n" "$url"
if ! "${download_cmd[@]}" | tar -xzf - ||
[[ "${PIPESTATUS[0]}" -ne '0' ]]; then
printf "Failed to download from '%s'.\n" "$url" >&2
return 1
elif [[ ! -d "$core_dir_parent" ]] && ! mkdir -p "$core_dir_parent" ; then
printf "Failed to create scripts dir '%s'\n" "$core_dir_parent" >&2
rm -rf "$unpacked_dir"
return 1
elif ! mv "$unpacked_dir" "$GO_SCRIPT_BASH_CORE_DIR"; then
printf "Failed to install downloaded directory in '%s'\n" \
"$GO_SCRIPT_BASH_CORE_DIR" >&2
rm -rf "$unpacked_dir"
return 1
fi
printf "Download of '%s' successful.\n\n" "$url"
}
git_clone_go_script_bash() {
printf "Cloning framework from '%s'...\n" "$GO_SCRIPT_BASH_REPO_URL"
if ! git clone --depth 1 -c advice.detachedHead=false \
-b "$GO_SCRIPT_BASH_VERSION" "$GO_SCRIPT_BASH_REPO_URL" \
"$GO_SCRIPT_BASH_CORE_DIR"; then
printf "Failed to clone '%s'; aborting.\n" "$GO_SCRIPT_BASH_REPO_URL" >&2
return 1
fi
printf "Clone of '%s' successful.\n\n" "$GO_SCRIPT_BASH_REPO_URL"
}
if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
if ! download_go_script_bash_tarball; then
printf "Using git clone as fallback\n"
if ! git_clone_go_script_bash; then
exit 1
fi
fi
fi
. "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" "$GO_SCRIPTS_DIR"
# Add any other configuration or initialization steps here.
@go "$@"