Skip to content

Commit

Permalink
Run shfmt so the workflow is green
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Jul 15, 2023
1 parent 4496730 commit afba9ef
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,111 @@ TOOL_NAME="amm"
TOOL_TEST="amm --version"

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

curl_opts=(-fsSL)

if [ -n "${GITHUB_API_TOKEN:-}" ]; then
curl_opts=("${curl_opts[@]}" -H "Authorization: Bearer $GITHUB_API_TOKEN")
curl_opts=("${curl_opts[@]}" -H "Authorization: Bearer $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}'
}

# Make a query to the GitHub API
gh_query() {
local url_rest="$1"
echo "$url_rest"
curl "${curl_opts[@]}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$OWNER/$REPO/$url_rest" || fail "Could not curl $url_rest"
local url_rest="$1"
echo "$url_rest"
curl "${curl_opts[@]}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$OWNER/$REPO/$url_rest" || fail "Could not curl $url_rest"
}

# Argument is the key whose value to get, JSON is gotten from stdin
get_str_value_from_json() {
grep -oE "\"$1\": \"[^\"]*\"" | cut -d '"' -f 4
grep -oE "\"$1\": \"[^\"]*\"" | cut -d '"' -f 4
}

get_num_value_from_json() {
grep -oE "\"$1\": [0-9][0-9]+" | cut -d ':' -f 2 | sed -E "s/(^ +)|\"//g" # Trim
grep -oE "\"$1\": [0-9][0-9]+" | cut -d ':' -f 2 | sed -E "s/(^ +)|\"//g" # Trim
}

# Get the tag names from a list of releases (or a single release) provided by
# the GitHub API. Reads from stdin.
tag_names() {
get_str_value_from_json "tag_name"
get_str_value_from_json "tag_name"
}

list_github_tags() {
gh_query "releases" | tag_names
gh_query "releases" | tag_names
}

get_release_id() {
local tag="$1"
gh_query "releases/tags/$tag" | get_num_value_from_json "id" | head -n 1
local tag="$1"
gh_query "releases/tags/$tag" | get_num_value_from_json "id" | head -n 1
}

# Given a tag, list its scala-ammonite versions
list_assets() {
local tag="$1"
local id=$(get_release_id "$tag")
gh_query "releases/$id/assets" | get_str_value_from_json "name" | cut -d '-' -f 1,2 | uniq
local tag="$1"
local id=$(get_release_id "$tag")
gh_query "releases/$id/assets" | get_str_value_from_json "name" | cut -d '-' -f 1,2 | uniq
}

list_all_versions() {
# While loop from https://superuser.com/a/284226
while IFS= read -r tag || [[ -n $tag ]]; do
list_assets "$tag"
done < <(list_github_tags)
# While loop from https://superuser.com/a/284226
while IFS= read -r tag || [[ -n $tag ]]; do
list_assets "$tag"
done < <(list_github_tags)
}

# The Ammonite version is <scala-version>-<ammonite tag>
tag_from_version() {
cut -d "-" -f 2 <<< "$1"
cut -d "-" -f 2 <<<"$1"
}

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

tag=$(tag_from_version "$version")
url="$GH_REPO/releases/download/$tag/$version"

echo "* Downloading $TOOL_NAME release $version..."
curl "${curl_opts[@]}" \
-H "Accept: application/octet-stream" \
-o "$filename" -C - "$url" ||
fail "Could not curl $url_rest"
local version filename tag url
version="$1"
filename="$2"

tag=$(tag_from_version "$version")
url="$GH_REPO/releases/download/$tag/$version"

echo "* Downloading $TOOL_NAME release $version..."
curl "${curl_opts[@]}" \
-H "Accept: application/octet-stream" \
-o "$filename" -C - "$url" ||
fail "Could not curl $url_rest"
}

install_version() {
local install_type="$1"
local version="$2"
local install_path="${3%/bin}/bin"
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
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"
(
mkdir -p "$install_path"
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"

[ -f /etc/resolv.conf ] || fail "$TOOL_NAME executable does not exist"
chmod +x "$install_path/$TOOL_NAME"
[ -f /etc/resolv.conf ] || fail "$TOOL_NAME executable does not exist"
chmod +x "$install_path/$TOOL_NAME"

local tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)"
test -x "$install_path/$tool_cmd" || fail "Expected $install_path/$tool_cmd to be executable."
local 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."
)
echo "$TOOL_NAME $version installation was successful!"
) || (
rm -rf "$install_path"
fail "An error occurred while installing $TOOL_NAME $version."
)
}

0 comments on commit afba9ef

Please sign in to comment.