* Add Makefile for Hygiene

Signed-off-by: Sora Morimoto <sora@morimoto.io>

* make format

Signed-off-by: Sora Morimoto <sora@morimoto.io>

---------

Signed-off-by: Sora Morimoto <sora@morimoto.io>
Co-authored-by: Kenny Parnell <k.parnell@gmail.com>
This commit is contained in:
Sora Morimoto 2023-07-19 22:32:53 +09:00 committed by GitHub
parent 577a6c5fe0
commit d8dec15ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 162 additions and 149 deletions

14
Makefile Normal file
View File

@ -0,0 +1,14 @@
SRCFILES = $(shell git ls-files "bin/**" "lib/**" "spec/**" "test-fixtures/*.sh")
SHFMT_BASE_FLAGS = -s -i 2 -ci
format:
shfmt -w $(SHFMT_BASE_FLAGS) $(SRCFILES)
.PHONY: format
format-check:
shfmt -d $(SHFMT_BASE_FLAGS) $(SRCFILES)
.PHONY: format-check
lint:
shellcheck -x $(SRCFILES)
.PHONY: lint

View File

@ -8,53 +8,53 @@ PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
source "$PLUGIN_DIR/lib/helpers.sh"
check_shasum() {
local archive_file_name=$1
local authentic_checksum_file=$2
local authentic_checksum=""
local archive_file_name=$1
local authentic_checksum_file=$2
local authentic_checksum=""
authentic_checksum=$(<"$authentic_checksum_file")
authentic_checksum=$(<"$authentic_checksum_file")
if command -v sha256sum >/dev/null 2>&1; then
sha256sum \
-c <(echo "$authentic_checksum $archive_file_name")
elif command -v shasum >/dev/null 2>&1; then
shasum \
-a 256 \
-c <(echo "$authentic_checksum $archive_file_name")
else
fail "sha256sum or shasum is not available for use"
fi
if command -v sha256sum >/dev/null 2>&1; then
sha256sum \
-c <(echo "$authentic_checksum $archive_file_name")
elif command -v shasum >/dev/null 2>&1; then
shasum \
-a 256 \
-c <(echo "$authentic_checksum $archive_file_name")
else
fail "sha256sum or shasum is not available for use"
fi
}
download_golang () {
local version=$1
local download_path=$2
local platform=""
local arch=""
download_golang() {
local version=$1
local download_path=$2
local platform=""
local arch=""
platform=$(get_platform)
arch=$(get_arch)
download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz"
platform=$(get_platform)
arch=$(get_arch)
download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz"
http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url")
if [ "$http_code" -eq 404 ] || [ "$http_code" -eq 403 ]; then
fail "URL: ${download_url} returned status ${http_code}"
fi
http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url")
if [ "$http_code" -eq 404 ] || [ "$http_code" -eq 403 ]; then
fail "URL: ${download_url} returned status ${http_code}"
fi
curl "$download_url" -o "${download_path}/archive.tar.gz"
curl "$download_url" -o "${download_path}/archive.tar.gz"
if [ "unset" = "${ASDF_GOLANG_SKIP_CHECKSUM:-unset}" ]; then
curl "${download_url}.sha256" -o "${download_path}/archive.tar.gz.sha256"
if [ "unset" = "${ASDF_GOLANG_SKIP_CHECKSUM:-unset}" ]; then
curl "${download_url}.sha256" -o "${download_path}/archive.tar.gz.sha256"
echo 'verifying checksum'
if ! check_shasum "${download_path}/archive.tar.gz" "${download_path}/archive.tar.gz.sha256"; then
fail "Authenticity of package archive can not be assured. Exiting."
else
msg "checksum verified"
fi
echo 'verifying checksum'
if ! check_shasum "${download_path}/archive.tar.gz" "${download_path}/archive.tar.gz.sha256"; then
fail "Authenticity of package archive can not be assured. Exiting."
else
err "checksum skipped"
msg "checksum verified"
fi
else
err "checksum skipped"
fi
}
download_golang "$ASDF_INSTALL_VERSION" "$ASDF_DOWNLOAD_PATH"

View File

@ -1,11 +1,11 @@
#!/usr/bin/env bash
if [ "${ASDF_INSTALL_VERSION}" != 'system' ] ; then
if [[ "unset" == "${GOROOT:-unset}" ]] ; then
export GOROOT=$ASDF_INSTALL_PATH/go
fi
if [ "${ASDF_INSTALL_VERSION}" != 'system' ]; then
if [[ "unset" == "${GOROOT:-unset}" ]]; then
export GOROOT=$ASDF_INSTALL_PATH/go
fi
if [[ "unset" == "${GOPATH:-unset}" ]] ; then
export GOPATH=$ASDF_INSTALL_PATH/packages
fi
if [[ "unset" == "${GOPATH:-unset}" ]]; then
export GOPATH=$ASDF_INSTALL_PATH/packages
fi
fi

View File

@ -8,8 +8,8 @@ PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
source "$PLUGIN_DIR/lib/helpers.sh"
if [ "$(get_platform silent)" = "darwin" ]; then
echo "coreutils"
echo "coreutils"
else
echo "coreutils"
echo "curl"
echo "coreutils"
echo "curl"
fi

View File

@ -7,23 +7,23 @@ PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
# shellcheck source=/dev/null
source "$PLUGIN_DIR/lib/helpers.sh"
install_golang () {
local version="$1"
local download_path="$2"
local install_path="$3"
created_tmp="0"
install_golang() {
local version="$1"
local download_path="$2"
local install_path="$3"
created_tmp="0"
if [ -z "$download_path" ] ; then
download_path=$(mktemp -dt asdf-golang.XXXX)
created_tmp="1"
ASDF_INSTALL_VERSION="$version" ASDF_DOWNLOAD_PATH="$download_path" "$PLUGIN_DIR/bin/download"
fi
if [ -z "$download_path" ]; then
download_path=$(mktemp -dt asdf-golang.XXXX)
created_tmp="1"
ASDF_INSTALL_VERSION="$version" ASDF_DOWNLOAD_PATH="$download_path" "$PLUGIN_DIR/bin/download"
fi
tar -C "$install_path" -xzf "${download_path}/archive.tar.gz"
tar -C "$install_path" -xzf "${download_path}/archive.tar.gz"
if [ "1" = "$created_tmp" ]; then
rm -r "$download_path"
fi
if [ "1" = "$created_tmp" ]; then
rm -r "$download_path"
fi
}
install_default_go_pkgs() {
@ -33,32 +33,31 @@ install_default_go_pkgs() {
if [ ! -f "$default_go_pkgs" ]; then return; fi
while read -r line; do
name=$(echo "$line" | \
sed 's|\(.*\) //.*$|\1|' | \
name=$(echo "$line" |
sed 's|\(.*\) //.*$|\1|' |
sed -E 's|^[[:space:]]*//.*||') # the first sed is for comments after package names, the second for full line comments
# Skip empty lines
if [ -z "$name" ]; then continue ; fi
if [ -z "$name" ]; then continue; fi
echo -ne "\nInstalling \033[33m${name}\033[39m go pkg... " >&2
# if using go > 1.16 then use go install as the preferred donwload path
if [ "$go_major_version" -ge 2 ] || [ "${go_minor_version//[!0-9]*}" -ge 16 ]; then
if [[ "$name" != *"@"* ]]; then
name="${name}@latest"
fi
if [ "$go_major_version" -ge 2 ] || [ "${go_minor_version//[!0-9]*/}" -ge 16 ]; then
if [[ $name != *"@"* ]]; then
name="${name}@latest"
fi
GOROOT="$ASDF_INSTALL_PATH/go" \
GOPATH="$ASDF_INSTALL_PATH/packages" \
PATH="$go_path:$PATH" \
go install "$name" > /dev/null && rc=$? || rc=$?
GOROOT="$ASDF_INSTALL_PATH/go" \
GOPATH="$ASDF_INSTALL_PATH/packages" \
PATH="$go_path:$PATH" \
go install "$name" >/dev/null && rc=$? || rc=$?
else
GOROOT="$ASDF_INSTALL_PATH/go" \
GOPATH="$ASDF_INSTALL_PATH/packages" \
PATH="$go_path:$PATH" \
go get -u "$name" > /dev/null && rc=$? || rc=$?
GOROOT="$ASDF_INSTALL_PATH/go" \
GOPATH="$ASDF_INSTALL_PATH/packages" \
PATH="$go_path:$PATH" \
go get -u "$name" >/dev/null && rc=$? || rc=$?
fi
if [[ $rc -eq 0 ]]; then
@ -66,7 +65,7 @@ install_default_go_pkgs() {
else
err "FAIL"
fi
done < "$default_go_pkgs"
done <"$default_go_pkgs"
}
install_golang "$ASDF_INSTALL_VERSION" "${ASDF_DOWNLOAD_PATH:-}" "$ASDF_INSTALL_PATH"

View File

@ -7,19 +7,19 @@ PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
# shellcheck source=/dev/null
source "$PLUGIN_DIR/lib/helpers.sh"
command eval "VERSIONS=($("$PLUGIN_DIR"/bin/list-all | tr ' ' '\n' | grep -ivE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | grep "^$1" | sed 's/^[[:space:]]\+//'))"
command eval "VERSIONS=($("$PLUGIN_DIR"/bin/list-all | tr ' ' '\n' | grep -ivE '(^Available versions:|-src|-dev|-latest|-stm|[-\.]rc|-alpha|-beta|[-\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)' | grep "^$1" | sed 's/^[[:space:]]\+//'))"
platform=$(get_platform true)
arch=$(get_arch)
version=""
for (( i=${#VERSIONS[@]}-1; i>=0; i-- )); do
version="${VERSIONS[i]}"
download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz"
http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url")
if [ "$http_code" -ne 404 ] && [ "$http_code" -ne 403 ]; then
break
fi
for ((i = ${#VERSIONS[@]} - 1; i >= 0; i--)); do
version="${VERSIONS[i]}"
download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz"
http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url")
if [ "$http_code" -ne 404 ] && [ "$http_code" -ne 403 ]; then
break
fi
done
printf "%s" "${version}"

View File

@ -2,9 +2,9 @@
set -eu
[ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail
git ls-remote --tags https://github.com/golang/go "go*" \
| awk -F/go '{ print $2 }' \
| uniq \
| sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n \
| grep -v '^1\($\|\.0\|\.0\.[0-9]\|\.1\|\.1rc[0-9]\|\.1\.[0-9]\|.2\|\.2rc[0-9]\|\.2\.1\|.8.5rc5\)$' \
| tr '\n' ' '
git ls-remote --tags https://github.com/golang/go "go*" |
awk -F/go '{ print $2 }' |
uniq |
sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n |
grep -v '^1\($\|\.0\|\.0\.[0-9]\|\.1\|\.1rc[0-9]\|\.1\.[0-9]\|.2\|\.2rc[0-9]\|\.2\.1\|.8.5rc5\)$' |
tr '\n' ' '

View File

@ -1,18 +1,18 @@
#!/usr/bin/env bash
case "${ASDF_GOLANG_MOD_VERSION_ENABLED:-}" in
true)
printf ".go-version go.mod go.work\n"
;;
false)
printf ".go-version\n"
;;
*)
cat >&2 <<-EOF
true)
printf ".go-version go.mod go.work\n"
;;
false)
printf ".go-version\n"
;;
*)
cat >&2 <<-EOF
Notice: Behaving like ASDF_GOLANG_MOD_VERSION_ENABLED=true
In the future this will have to be set to continue
reading from the go.mod and go.work files
EOF
printf ".go-version go.mod go.work\n"
;;
printf ".go-version go.mod go.work\n"
;;
esac

View File

@ -18,7 +18,7 @@ installed_versions() {
if [ -d "$plugin_installs_path" ]; then
for install in "${plugin_installs_path}"/*/; do
[[ -e "$install" ]] || break
[[ -e $install ]] || break
basename "$install" | sed 's/^ref-/ref:/'
done
fi
@ -28,7 +28,7 @@ get_legacy_version() {
current_file="$1"
basename=$(basename -- "$current_file")
if [[ "$basename" =~ ^go.(mod|work)$ ]]; then
if [[ $basename =~ ^go.(mod|work)$ ]]; then
GOLANG_VERSION=$(
grep 'go\s*[0-9]' "$current_file" |
sed -E \

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
if [ -d "${ASDF_INSTALL_PATH}/packages" ]; then
chmod -R u+wx "${ASDF_INSTALL_PATH}/packages"
chmod -R u+wx "${ASDF_INSTALL_PATH}/packages"
fi
rm -rf "${ASDF_INSTALL_PATH}"

View File

@ -2,50 +2,50 @@
set -eu
[ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail
get_platform () {
local silent=${1:-}
local platform=""
get_platform() {
local silent=${1:-}
local platform=""
platform="$(uname | tr '[:upper:]' '[:lower:]')"
platform="$(uname | tr '[:upper:]' '[:lower:]')"
case "$platform" in
linux|darwin|freebsd)
[ -z "$silent" ] && msg "Platform '${platform}' supported!"
;;
*)
fail "Platform '${platform}' not supported!"
;;
esac
case "$platform" in
linux | darwin | freebsd)
[ -z "$silent" ] && msg "Platform '${platform}' supported!"
;;
*)
fail "Platform '${platform}' not supported!"
;;
esac
printf "%s" "$platform"
printf "%s" "$platform"
}
get_arch () {
local arch=""
local arch_check=${ASDF_GOLANG_OVERWRITE_ARCH:-"$(uname -m)"}
case "${arch_check}" in
x86_64|amd64) arch="amd64"; ;;
i686|i386|386) arch="386"; ;;
armv6l|armv7l) arch="armv6l"; ;;
aarch64|arm64) arch="arm64"; ;;
ppc64le) arch="ppc64le"; ;;
*)
fail "Arch '${arch_check}' not supported!"
;;
esac
get_arch() {
local arch=""
local arch_check=${ASDF_GOLANG_OVERWRITE_ARCH:-"$(uname -m)"}
case "${arch_check}" in
x86_64 | amd64) arch="amd64" ;;
i686 | i386 | 386) arch="386" ;;
armv6l | armv7l) arch="armv6l" ;;
aarch64 | arm64) arch="arm64" ;;
ppc64le) arch="ppc64le" ;;
*)
fail "Arch '${arch_check}' not supported!"
;;
esac
printf "%s" "$arch"
printf "%s" "$arch"
}
msg () {
echo -e "\033[32m$1\033[39m" >&2
msg() {
echo -e "\033[32m$1\033[39m" >&2
}
err () {
echo -e "\033[31m$1\033[39m" >&2
err() {
echo -e "\033[31m$1\033[39m" >&2
}
fail () {
err "$1"
exit 1
fail() {
err "$1"
exit 1
}

View File

@ -1,18 +1,18 @@
#!/usr/bin/env bash
declare -a fake_install_versions=(
1.16
1.17.1
1.17.7
1.17.13
1.18.1
1.18.2
1.18.6
1.19
1.19.3
1.16
1.17.1
1.17.7
1.17.13
1.18.1
1.18.2
1.18.6
1.19
1.19.3
)
mkdir -p ../../installs/asdf-golang
for version in "${fake_install_versions[@]}"; do
mkdir "../../installs/asdf-golang/${version}"
mkdir "../../installs/asdf-golang/${version}"
done