Skip to content

Commit

Permalink
Refactor code for better readability and maintainability
Browse files Browse the repository at this point in the history
- In `lib/utils.bash`, improved indentation and formatting of the `sort_versions` function.
- In `lib/utils.bash`, improved indentation and formatting of the `list_github_tags` function.
- In `scripts/format.bash`, no significant changes were made.
- In `scripts/lint.bash`, no significant changes were made.
  • Loading branch information
log2 committed Nov 15, 2023
1 parent a1e5440 commit b3d416d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
6 changes: 3 additions & 3 deletions bin/latest-stable
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugin_dir=$(dirname "$(dirname "$current_script_path")")
curl_opts=(-sI)

if [ -n "${GITHUB_API_TOKEN:-}" ]; then
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN")
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN")
fi

# curl of REPO/releases/latest is expected to be a 302 to another URL
Expand All @@ -21,9 +21,9 @@ redirect_url=$(curl "${curl_opts[@]}" "$GH_REPO/releases/latest" | sed -n -e "s|
version=
#printf "redirect url: %s\n" "$redirect_url" >&2
if [[ "$redirect_url" == "$GH_REPO/releases" ]]; then
version="$(list_all_versions | sort_versions | tail -n1 | xargs echo)"
version="$(list_all_versions | sort_versions | tail -n1 | xargs echo)"
else
version="$(printf "%s\n" "$redirect_url" | sed 's|.*/tag/v\{0,1\}||')"
version="$(printf "%s\n" "$redirect_url" | sed 's|.*/tag/v\{0,1\}||')"
fi

printf "%s\n" "$version"
88 changes: 44 additions & 44 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,69 @@ TOOL_NAME="flamingo"
TOOL_TEST="flamingo --help"

fail() {
echo -e "asdf-$TOOL_NAME: $*"
exit 1
echo -e "asdf-$TOOL_NAME: $*"
exit 1
}

curl_opts=(-fsSL)

# NOTE: You might want to remove this if flamingo is not hosted on GitHub releases.
if [ -n "${GITHUB_API_TOKEN:-}" ]; then
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN")
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}'
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
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 flamingo has other means of determining installable versions.
list_github_tags
# TODO: Adapt this. By default we simply list the tag names from GitHub releases.
# Change this function if flamingo has other means of determining installable versions.
list_github_tags
}

download_release() {
local version filename url
version="$1"
filename="$2"
local version filename url
version="$1"
filename="$2"

local url_filename
url_filename="$(get_filename "$(get_platform)" "${ASDF_INSTALL_VERSION}")"
url="$GH_REPO/releases/download/v${ASDF_INSTALL_VERSION}/${url_filename}"
local url_filename
url_filename="$(get_filename "$(get_platform)" "${ASDF_INSTALL_VERSION}")"
url="$GH_REPO/releases/download/v${ASDF_INSTALL_VERSION}/${url_filename}"

echo "* Downloading $TOOL_NAME release $version..."
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
echo "* Downloading $TOOL_NAME release $version..."
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 "${ASDF_DOWNLOAD_PATH}/${TOOL_NAME}" "$install_path"

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."
)
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 "${ASDF_DOWNLOAD_PATH}/${TOOL_NAME}" "$install_path"

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."
)
}

get_filename() {
Expand All @@ -80,10 +80,10 @@ get_filename() {
get_platform() {
arch=$(uname -m)
case $arch in
armv*) arch="arm";;
arm64) arch="arm64";; # m1 macs
aarch64) arch="arm64";;
*) arch="amd64";;
armv*) arch="arm" ;;
arm64) arch="arm64" ;; # m1 macs
aarch64) arch="arm64" ;;
*) arch="amd64" ;;
esac
echo "$(uname | tr '[:upper:]' '[:lower:]')_${arch}"
}
2 changes: 1 addition & 1 deletion scripts/format.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

shfmt --language-dialect bash --write \
./**/*
./**/*
8 changes: 4 additions & 4 deletions scripts/lint.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash

shellcheck --shell=bash --external-sources \
bin/* --source-path=template/lib/ \
lib/* \
scripts/*
bin/* --source-path=template/lib/ \
lib/* \
scripts/*

shfmt --language-dialect bash --diff \
./**/*
./**/*

0 comments on commit b3d416d

Please sign in to comment.