From 8b66366014c63c1c27dd6da42e6b1e9761a11d01 Mon Sep 17 00:00:00 2001 From: guochao Date: Tue, 13 Sep 2022 16:28:48 +0800 Subject: [PATCH] first commit --- bin/download | 48 ++++++++++++++++++++++++++++++++ bin/install | 11 ++++++++ bin/list-all | 12 ++++++++ lib/utils.bash | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100755 bin/download create mode 100755 bin/install create mode 100755 bin/list-all create mode 100644 lib/utils.bash diff --git a/bin/download b/bin/download new file mode 100755 index 0000000..7935db5 --- /dev/null +++ b/bin/download @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -euo pipefail + +current_script_path=${BASH_SOURCE[0]} +plugin_dir=$(dirname "$(dirname "$current_script_path")") + +# shellcheck source=../lib/utils.bash +source "${plugin_dir}/lib/utils.bash" + +mkdir -p "$ASDF_DOWNLOAD_PATH" + +case $(uname -m) in + x86_64) + ARCH=amd64 + ;; + arm64|aarch64) + ARCH=arm64 + ;; + *) + exit -1 + ;; +esac + +OS="$(uname -s | tr '[[:upper:]]' '[[:lower:]]')" +case $OS in + linux|darwin) + ;; + *) + exit -1 + ;; +esac + +FILES=( + "kubectl-vela-v$ASDF_INSTALL_VERSION-$OS-$ARCH.tar.gz" + "vela-v$ASDF_INSTALL_VERSION-$OS-$ARCH.tar.gz" +) + +for FILE in "${FILES[@]}"; do + # Download tar.gz file to the download directory + download_release "$ASDF_INSTALL_VERSION" "$FILE" + + # Extract contents of tar.gz file into the download directory + tar -xzf "$FILE" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $FILE" + + # Remove the tar.gz file since we don't need to keep it + rm "$FILE" +done diff --git a/bin/install b/bin/install new file mode 100755 index 0000000..9737a63 --- /dev/null +++ b/bin/install @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +current_script_path=${BASH_SOURCE[0]} +plugin_dir=$(dirname "$(dirname "$current_script_path")") + +# shellcheck source=../lib/utils.bash +source "${plugin_dir}/lib/utils.bash" + +install_version "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" diff --git a/bin/list-all b/bin/list-all new file mode 100755 index 0000000..ab733bc --- /dev/null +++ b/bin/list-all @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -euo pipefail + +current_script_path=${BASH_SOURCE[0]} +plugin_dir=$(dirname "$(dirname "$current_script_path")") + +# shellcheck source=../lib/utils.bash +source "${plugin_dir}/lib/utils.bash" + +list_all_versions | sort_versions | xargs echo + diff --git a/lib/utils.bash b/lib/utils.bash new file mode 100644 index 0000000..579776f --- /dev/null +++ b/lib/utils.bash @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for . +GH_REPO="https://github.com/kubevela/kubevela" +TOOL_NAME="kubevela" +TOOL_TEST="vela" + +fail() { + echo -e "asdf-$TOOL_NAME: $*" + exit 1 +} + +curl_opts=(-fsSL) + +# NOTE: You might want to remove this if is not hosted on GitHub releases. +if [ -n "${GITHUB_API_TOKEN:-}" ]; then + curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN") +fi + +sort_versions() { + sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | + LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' +} + +list_github_tags() { + git ls-remote --tags --refs "$GH_REPO" | + grep -o 'refs/tags/.*' | cut -d/ -f3- | + sed 's/^v//' # NOTE: You might want to adapt this sed to remove non-version strings from tags +} + +list_all_versions() { + # TODO: Adapt this. By default we simply list the tag names from GitHub releases. + # Change this function if has other means of determining installable versions. + list_github_tags +} + +download_release() { + local version filename url + version="$1" + filename="$2" + + # TODO: Adapt the release URL convention for + url="$GH_REPO/releases/download/v${version}/${filename}" + + echo "* Downloading $TOOL_NAME release $version: $filename..." + curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url" +} + +install_version() { + local install_type="$1" + local version="$2" + local install_path="${3%/bin}/bin" + + if [ "$install_type" != "version" ]; then + fail "asdf-$TOOL_NAME supports release installs only" + fi + + ( + mkdir -p "$install_path" + cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path" + + # TODO: Assert executable exists. + local tool_cmd + tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)" + test -x "$install_path/$tool_cmd" || fail "Expected $install_path/$tool_cmd to be executable." + + echo "$TOOL_NAME $version installation was successful!" + ) || ( + rm -rf "$install_path" + fail "An error occurred while installing $TOOL_NAME $version." + ) +}