From d8dec15ccc0cffc2e2762f348ccffeffb79c6489 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Wed, 19 Jul 2023 22:32:53 +0900 Subject: [PATCH] Hygiene (#113) * Add Makefile for Hygiene Signed-off-by: Sora Morimoto * make format Signed-off-by: Sora Morimoto --------- Signed-off-by: Sora Morimoto Co-authored-by: Kenny Parnell --- Makefile | 14 +++++ bin/download | 72 +++++++++++++------------- bin/exec-env | 14 ++--- bin/help.deps | 6 +-- bin/install | 61 +++++++++++----------- bin/latest-stable | 16 +++--- bin/list-all | 12 ++--- bin/list-legacy-filenames | 20 +++---- bin/parse-legacy-file | 4 +- bin/uninstall | 2 +- lib/helpers.sh | 68 ++++++++++++------------ set-env.fish | 2 +- test-fixtures/create-dummy-installs.sh | 20 +++---- 13 files changed, 162 insertions(+), 149 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b06c32d --- /dev/null +++ b/Makefile @@ -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 diff --git a/bin/download b/bin/download index 3b6bffb..46e7059 100755 --- a/bin/download +++ b/bin/download @@ -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" diff --git a/bin/exec-env b/bin/exec-env index a363908..c794669 100755 --- a/bin/exec-env +++ b/bin/exec-env @@ -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 diff --git a/bin/help.deps b/bin/help.deps index 293e920..7ca6747 100755 --- a/bin/help.deps +++ b/bin/help.deps @@ -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 diff --git a/bin/install b/bin/install index 2f3ad54..b196283 100755 --- a/bin/install +++ b/bin/install @@ -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" diff --git a/bin/latest-stable b/bin/latest-stable index 0b11812..e7dfc08 100755 --- a/bin/latest-stable +++ b/bin/latest-stable @@ -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}" diff --git a/bin/list-all b/bin/list-all index 6efa1ee..27d629a 100755 --- a/bin/list-all +++ b/bin/list-all @@ -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' ' ' diff --git a/bin/list-legacy-filenames b/bin/list-legacy-filenames index 3338835..c93f5ef 100755 --- a/bin/list-legacy-filenames +++ b/bin/list-legacy-filenames @@ -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 diff --git a/bin/parse-legacy-file b/bin/parse-legacy-file index 71b5b46..c6f0e13 100755 --- a/bin/parse-legacy-file +++ b/bin/parse-legacy-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 \ diff --git a/bin/uninstall b/bin/uninstall index ebca660..fd54c0d 100755 --- a/bin/uninstall +++ b/bin/uninstall @@ -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}" diff --git a/lib/helpers.sh b/lib/helpers.sh index 9fdd109..7ba2238 100644 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -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 } diff --git a/set-env.fish b/set-env.fish index 2a9782b..a9b0db9 100644 --- a/set-env.fish +++ b/set-env.fish @@ -5,4 +5,4 @@ function asdf_update_golang_env --on-event fish_prompt set -gx GOROOT (dirname (dirname "$full_path")) end -end \ No newline at end of file +end diff --git a/test-fixtures/create-dummy-installs.sh b/test-fixtures/create-dummy-installs.sh index d8a1c9b..6e2dc8e 100755 --- a/test-fixtures/create-dummy-installs.sh +++ b/test-fixtures/create-dummy-installs.sh @@ -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