Skip to content

Commit

Permalink
upgrade asdf template to latest version (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
0ghny authored Jul 30, 2024
1 parent 4070cd5 commit 9ba0685
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 112 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: asdf_plugin_test
uses: asdf-vm/actions/plugin-test@v1
uses: asdf-vm/actions/plugin-test@v2
with:
command: gobackup --help
32 changes: 10 additions & 22 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,18 @@ on:
pull_request:

jobs:
shellcheck:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: asdf-vm/actions/install@v2
- run: scripts/lint.bash

- name: Install asdf dependencies
uses: asdf-vm/actions/install@v1

- name: Run ShellCheck
run: scripts/shellcheck.bash

shellfmt:
actionlint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install asdf dependencies
uses: asdf-vm/actions/install@v1

- name: List file to shfmt
run: shfmt -f .

- name: Run shfmt
run: scripts/shfmt.bash

- uses: actions/checkout@v3
- name: Check workflow files
uses: docker://rhysd/actionlint:1.6.23
with:
args: -color
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Release

on:
push:
branches:
- main

permissions:
contents: write
pull-requests: write

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: googleapis/release-please-action@v4
with:
release-type: simple
18 changes: 18 additions & 0 deletions .github/workflows/semantic-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Lint

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
semantic-pr:
runs-on: ubuntu-latest
steps:
- uses: amannn/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
validateSingleCommit: true
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
shellcheck 0.7.2
shfmt 3.3.0
shellcheck 0.9.0
shfmt 3.6.0
2 changes: 1 addition & 1 deletion bin/download
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
current_script_path=${BASH_SOURCE[0]}
plugin_dir=$(dirname "$(dirname "$current_script_path")")

# shellcheck source=../lib/utils.bash
# shellcheck source=/dev/null
source "${plugin_dir}/lib/utils.bash"

mkdir -p "$ASDF_DOWNLOAD_PATH"
Expand Down
7 changes: 7 additions & 0 deletions bin/help.deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

cat <<-EOM
git
curl
sed
EOM
6 changes: 6 additions & 0 deletions bin/help.links
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

cat <<-EOM
Git Repository: https://github.com/gobackup/gobackup
Documentation: https://gobackup.github.io/
EOM
5 changes: 5 additions & 0 deletions bin/help.overview
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

cat <<-EOM
Manage Backup Automation with GoBackup
EOM
2 changes: 1 addition & 1 deletion bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
current_script_path=${BASH_SOURCE[0]}
plugin_dir=$(dirname "$(dirname "$current_script_path")")

# shellcheck source=../lib/utils.bash
# shellcheck source=/dev/null
source "${plugin_dir}/lib/utils.bash"

install_version "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
2 changes: 1 addition & 1 deletion bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
current_script_path=${BASH_SOURCE[0]}
plugin_dir=$(dirname "$(dirname "$current_script_path")")

# shellcheck source=../lib/utils.bash
# shellcheck source=/dev/null
source "${plugin_dir}/lib/utils.bash"

list_all_versions | sort_versions | xargs echo
140 changes: 63 additions & 77 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,117 @@

set -euo pipefail

# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for gobackup.
GH_REPO="https://github.com/huacnlee/gobackup"
TOOL_NAME="gobackup"
TOOL_TEST="gobackup --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 gobackup 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}'
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//'
}

list_all_versions() {
# Change this function if gobackup has other means of determining installable versions.
list_github_tags
list_github_tags
}

download_release() {
local version filename url
local -r platform="$(get_platform)"
local -r arch="$(get_arch)"
version="$1"
filename="$2"
local version filename url
local -r platform="$(get_platform)"
local -r arch="$(get_arch)"
version="$1"
filename="$2"

# https://github.com/huacnlee/gobackup/releases/download/v1.2.0/gobackup-linux-amd64.tar.gz
url="$GH_REPO/releases/download/v${version}/${TOOL_NAME}-${platform}-${arch}.tar.gz"
# https://github.com/huacnlee/gobackup/releases/download/v1.2.0/gobackup-linux-amd64.tar.gz
url="$GH_REPO/releases/download/v${version}/${TOOL_NAME}-${platform}-${arch}.tar.gz"

echo "* Downloading $TOOL_NAME release $version ${platform}/${arch}..."
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
echo "* Downloading $TOOL_NAME release $version ${platform}/${arch}..."
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"
echo "Installing from $ASDF_DOWNLOAD_PATH to $install_path"
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"
test -x "$install_path/$TOOL_NAME" || fail "Expected $install_path/$TOOL_NAME binary not found."
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"
echo "Installing from $ASDF_DOWNLOAD_PATH to $install_path"
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"
test -x "$install_path/$TOOL_NAME" || fail "Expected $install_path/$TOOL_NAME binary not found."
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_platform: determine platform of running machine
# .............................................................................
get_platform() {
local platform="Linux"
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
darwin) platform="darwin" ;;
linux) platform="linux" ;;
*) platform_not_supported ;;
esac

echo -n $platform
local platform="Linux"
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
darwin) platform="darwin" ;;
linux) platform="linux" ;;
*) platform_not_supported ;;
esac

echo -n $platform
}
# .............................................................................
# get_arch: determine architecture of running machine
# .............................................................................
get_arch() {
local arch="x86_64"
case "$(uname -m)" in
arm64 | aarch64) arch="arm64" ;;
x86_64 | amd64) arch="amd64" ;;
*) architecture_not_supported ;;
esac
echo -n "${arch}"
local arch="x86_64"
case "$(uname -m)" in
arm64 | aarch64) arch="arm64" ;;
x86_64 | amd64) arch="amd64" ;;
*) architecture_not_supported ;;
esac
echo -n "${arch}"
}
# .............................................................................
# get_file_ext: determine file extension based on platform
# .............................................................................
get_file_ext() {
local ext="tar.gz"
case "$(uname | tr '[:upper:]' '[:lower:]')" in
darwin) ext="tar.gz" ;;
linux) ext="tar.gz" ;;
*) platform_not_supported ;;
esac
echo -n "${ext}"
}
# .............................................................................
# LOG::fail
# .............................................................................
fail() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')][FAIL] $*"
exit 1
local ext="tar.gz"
case "$(uname | tr '[:upper:]' '[:lower:]')" in
darwin) ext="tar.gz" ;;
linux) ext="tar.gz" ;;
*) platform_not_supported ;;
esac
echo -n "${ext}"
}
# .............................................................................
# platform_not_supported: Raise a platform not supported exception
# .............................................................................
platform_not_supported() {
fail "Platform '$(uname)' not supported!"
fail "Platform '$(uname)' not supported!"
}
# .............................................................................
# architecture_not_supported: Raise an architecture not supported exception
# .............................................................................
architecture_not_supported() {
fail "Platform '$(uname -m)' not supported!"
fail "Platform '$(uname -m)' not supported!"
}
4 changes: 4 additions & 0 deletions scripts/format.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

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

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

shfmt --language-dialect bash --diff \
./**/*
4 changes: 0 additions & 4 deletions scripts/shellcheck.bash

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/shfmt.bash

This file was deleted.

0 comments on commit 9ba0685

Please sign in to comment.