Skip to content

Commit

Permalink
Add the ability to specify the version. Switch to long options.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorj committed Jan 19, 2024
1 parent e31d2c3 commit 76fa105
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 148 deletions.
4 changes: 2 additions & 2 deletions .plano.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def test(shell="sh", verbose=False, debug=False):
ENV["DEBUG"] = "1"

try:
run(f"{shell} {'-o igncr' if WINDOWS else ''} install.sh {'-v' if verbose else ''}".strip())
run(f"{shell} {'-o igncr' if WINDOWS else ''} uninstall.sh {'-v' if verbose else ''}".strip())
run(f"{shell} {'-o igncr' if WINDOWS else ''} install.sh {'--verbose' if verbose else ''}".strip())
run(f"{shell} {'-o igncr' if WINDOWS else ''} uninstall.sh {'--verbose' if verbose else ''}".strip())
finally:
if debug:
del ENV["DEBUG"]
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ curl https://raw.githubusercontent.com/skupperproject/skupper-install-script/mai
~~~ shell
curl https://raw.githubusercontent.com/skupperproject/skupper-install-script/main/uninstall.sh | sh
~~~

## More

~~~ shell
curl https://raw.githubusercontent.com/skupperproject/skupper-install-script/main/install.sh | bash -s -- main
~~~

~~~ shell
install.sh [opts] [version]
install.sh latest
install.sh main
install.sh 1.4.4
~~~

~~~ shell
install.sh --verbose --scheme home --version main --interactive
install.sh latest
install.sh main
install.sh 1.4.4
~~~
171 changes: 116 additions & 55 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -329,41 +329,10 @@ extract_archive() {
gzip -dc "${archive_file}" | (cd "${output_dir}" && tar xf -)
}

usage() {
local error="${1:-}"

if [ -n "${error}" ]
then
printf "%b %s\n\n" "$(red "ERROR:")" "${*}"
fi

cat <<EOF
Usage: ${0} [-hvy] [-s <scheme>]
A script that installs the Skupper command-line tool
Options:
-h Print this help text and exit
-i Operate in interactive mode
-s <scheme> Select an installation scheme (default "home")
-v Print detailed logging to the console
Installation schemes:
home Install to ~/.local/bin
opt Install to /opt/skupper/bin
EOF

if [ -n "${error}" ]
then
exit 1
fi

exit 0
}

# func <output-dir> -> release_version=<version>, release_file=<file>
fetch_latest_skupper_release() {
local output_dir="$1"
# func <version> <output-dir> -> release_version=<version>, release_file=<file>
fetch_skupper_release() {
local version="$1"
local output_dir="$2"

assert test -d "${output_dir}"
assert program_is_available awk
Expand Down Expand Up @@ -394,36 +363,111 @@ fetch_latest_skupper_release() {

local release_version_file="${output_dir}/release-version.txt"

log "Looking up the latest release version"

run curl -sf "https://skupper.io/data/install.json" \
| awk 'match($0, /"version": "[0-9]+\.[0-9]+\.[0-9]+"/) { print substr($0, RSTART+12, RLENGTH-13) }' \
>| "${release_version_file}"
case "${version}" in
latest)
log "Looking up the latest release version"

run curl -sf "https://skupper.io/data/install.json" \
| awk 'match($0, /"version": "[0-9]+\.[0-9]+\.[0-9]+"/) { print substr($0, RSTART+12, RLENGTH-13) }' \
>| "${release_version_file}"
;;
main)
echo "main-release" >| "${release_version_file}"
;;
*)
echo "${version}" >| "${release_version_file}"
;;
esac

release_version="$(cat "${release_version_file}")"

log "Release version: ${release_version}"
log "Release version file: ${release_version_file}"

local release_file_name="skupper-cli-${release_version}-${operating_system}-${architecture}.tgz"
# release_file is "returned"
release_file="${output_dir}/${release_file_name}"

if [ ! -e "${release_file}" ]
assert test -n "${release_file}"
assert test -n "${release_file_name}"
assert test -n "${release_version}"

if [ ! -e "${release_file}" ] || [ "${release_version}" = "main-release" ]
then
log "Downloading the latest release"
log "Downloading the ${release_version} release"

run curl -sfL --show-error -o "${release_file}" \
"https://github.com/skupperproject/skupper/releases/download/${release_version}/${release_file_name}"
local release_url="https://github.com/skupperproject/skupper/releases/download/${release_version}/${release_file_name}"

if ! run curl -sfL --show-error -o "${release_file}" "${release_url}"
then
fail "No release found at ${release_url}"
fi
else
log "Using the cached release archive"
fi

log "Archive file: ${release_file}"

# release_version is "returned"
release_version="$(cat "${release_version_file}")"

assert test -n "${release_version}"
assert test -f "${release_file}"
}

usage() {
local error="${1:-}"

if [ -n "${error}" ]
then
printf "%b %s\n\n" "$(red "ERROR:")" "${*}"
fi

cat <<EOF
Usage: ${0} [OPTION...]
A script that installs the Skupper command-line tool
Options:
-h, --help Print this help text and exit
--version VERSION Select the version (default "latest")
--scheme SCHEME Select an installation scheme (default "home")
--interactive Operate in interactive mode
--verbose Print detailed logging to the console
Versions:
latest The latest release
main The latest CI/CD release from main
X.Y.Z A specific version
Schemes:
home Install to ~/.local/bin
opt Install to /opt/skupper/bin
EOF

if [ -n "${error}" ]
then
exit 1
fi

exit 0
}

require_option_arg() {
local opt="$1"
local optarg="$2"

if [ -z "${optarg}" ]
then
usage "Option ${opt} is missing a required argument"
fi

case "${optarg}" in
-*) usage "Option ${opt} is missing a required argument" ;;
*) : ;;
esac
}

main() {
enable_strict_mode

Expand All @@ -432,21 +476,38 @@ main() {
enable_debug_mode
fi

local scheme="home"
local verbose=
local version=latest
local scheme=home
local interactive=
local verbose=

while getopts :his:v option
while [ $# -gt 0 ]
do
case "${option}" in
h) usage ;;
i) interactive=1 ;;
s) scheme="${OPTARG}" ;;
v) verbose=1 ;;
*) usage "Unknown option: ${OPTARG}" ;;
case "$1" in
-h|--help) usage ;;
--version)
require_option_arg "$1" "${2:-}"
version="$2"
shift
;;
--scheme)
require_option_arg "$1" "${2:-}"
scheme="$2"
shift
;;
--interactive) interactive=1 ;;
--verbose) verbose=1 ;;
*) usage "Unknown option: ${1}" ;;
esac

shift
done

case "${version}" in
latest|main|[0123456789].[0123456789].[0123456789]) ;;
*) usage "Illegal version argument: ${version}" ;;
esac

case "${scheme}" in
home) local skupper_bin_dir="${HOME}/.local/bin" ;;
opt) local skupper_bin_dir="/opt/skupper/bin" ;;
Expand Down Expand Up @@ -491,9 +552,9 @@ main() {

print_result "OK"

print_section "Downloading the latest release"
print_section "Downloading the release"

fetch_latest_skupper_release "${work_dir}"
fetch_skupper_release "${version}" "${work_dir}"

print_result "OK"

Expand Down
Loading

0 comments on commit 76fa105

Please sign in to comment.