From 635e6c3f7f6173780cea3183f8df4c01a7eb6d03 Mon Sep 17 00:00:00 2001 From: Christophe Furmaniak Date: Sun, 9 Jul 2023 15:53:52 +0200 Subject: [PATCH] chore: init the repository (#1) * feat: deal with dl url * chore: fix format and shfmt * chore: add a mdlinter --- .github/ISSUE_TEMPLATE/bug_report.md | 10 +- .github/ISSUE_TEMPLATE/feature_request.md | 8 +- .github/PULL_REQUEST/pull_request_template.md | 3 +- .github/workflows/build.yml | 4 + .github/workflows/lint.yml | 10 ++ .github/workflows/release.yml | 4 + .github/workflows/semantic-pr.yml | 6 +- .markdownlint-cli2.yaml | 2 + .markdownlint.yaml | 19 +++ LICENSE | 2 - README.md | 31 ++-- bin/download | 10 +- bin/install | 2 +- bin/latest-stable | 8 +- bin/list-all | 2 +- contributing.md | 1 - lib/utils.bash | 145 +++++++++++++----- scripts/format.bash | 4 +- scripts/lint.bash | 10 +- 19 files changed, 193 insertions(+), 88 deletions(-) create mode 100644 .markdownlint-cli2.yaml create mode 100644 .markdownlint.yaml diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 83f93ac..613e1d6 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -6,22 +6,22 @@ labels: bug assignees: "" --- -**Describe the bug** +## Describe the bug -**Steps to reproduce** +## Steps to reproduce -**Expected behavior** +## Expected behavior -**Screenshots** +## Screenshots -**Additional context** +## Additional context diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index f52b3b9..fc6e725 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -6,18 +6,18 @@ labels: enhancement assignees: "" --- -**Is your feature request related to a problem? Please describe.** +## Is your feature request related to a problem? Please describe -**Describe the solution you'd like** +## Describe the solution you'd like -**Describe alternatives you've considered** +## Describe alternatives you've considered -**Additional context** +## Additional context diff --git a/.github/PULL_REQUEST/pull_request_template.md b/.github/PULL_REQUEST/pull_request_template.md index a32de42..1f23dca 100644 --- a/.github/PULL_REQUEST/pull_request_template.md +++ b/.github/PULL_REQUEST/pull_request_template.md @@ -1,5 +1,4 @@ - - + ## Description diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f215940..2d865ab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,10 @@ on: - main pull_request: +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + jobs: plugin_test: name: asdf plugin test diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5c3ffed..7418c15 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -6,6 +6,10 @@ on: - main pull_request: +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + jobs: lint: runs-on: ubuntu-latest @@ -22,3 +26,9 @@ jobs: uses: docker://rhysd/actionlint:1.6.23 with: args: -color + + mdlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: DavidAnson/markdownlint-cli2-action@v11 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 454ce7a..93b01e3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,6 +5,10 @@ on: branches: - main +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + permissions: contents: write pull-requests: write diff --git a/.github/workflows/semantic-pr.yml b/.github/workflows/semantic-pr.yml index 8b26fa4..0fa4b2f 100644 --- a/.github/workflows/semantic-pr.yml +++ b/.github/workflows/semantic-pr.yml @@ -1,4 +1,4 @@ -name: Lint +name: Semantic PR checks on: pull_request_target: @@ -7,6 +7,10 @@ on: - edited - synchronize +concurrency: + group: ${{ github.ref }}-${{ github.workflow }} + cancel-in-progress: true + jobs: semantic-pr: runs-on: ubuntu-latest diff --git a/.markdownlint-cli2.yaml b/.markdownlint-cli2.yaml new file mode 100644 index 0000000..54e311f --- /dev/null +++ b/.markdownlint-cli2.yaml @@ -0,0 +1,2 @@ +globs: + - "**/*.md" diff --git a/.markdownlint.yaml b/.markdownlint.yaml new file mode 100644 index 0000000..bdb8533 --- /dev/null +++ b/.markdownlint.yaml @@ -0,0 +1,19 @@ +# For a list of all rules, see here: https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md + +# Default state for all rules +default: true + +# MD013/line-length - Line length +MD013: false + +# MD024/Multiple headings with the same content +MD024: false + +# MD026/no-trailing-punctuation - Trailing punctuation in heading +MD026: false + +# MD033/no-inline-html - Inline HTML +MD033: false + +# MD037/Spaces inside emphasis markers +MD037: false diff --git a/LICENSE b/LICENSE index de91979..261eeb9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,3 @@ -TODO: INSERT YOUR NAME COPYRIGHT YEAR (if applicable to your license) - Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ diff --git a/README.md b/README.md index 94839a1..347e268 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,27 @@ -
+# asdf-gofumpt -# asdf-gofumpt [![Build](https://github.com/looztra/asdf-gofumpt/actions/workflows/build.yml/badge.svg)](https://github.com/looztra/asdf-gofumpt/actions/workflows/build.yml) [![Lint](https://github.com/looztra/asdf-gofumpt/actions/workflows/lint.yml/badge.svg)](https://github.com/looztra/asdf-gofumpt/actions/workflows/lint.yml) +[![Build](https://github.com/looztra/asdf-gofumpt/actions/workflows/build.yml/badge.svg)](https://github.com/looztra/asdf-gofumpt/actions/workflows/build.yml) +[![Lint](https://github.com/looztra/asdf-gofumpt/actions/workflows/lint.yml/badge.svg)](https://github.com/looztra/asdf-gofumpt/actions/workflows/lint.yml) [gofumpt](https://github.com/mvdan/gofumpt) plugin for the [asdf version manager](https://asdf-vm.com).
-# Contents +## Contents -- [Dependencies](#dependencies) -- [Install](#install) -- [Contributing](#contributing) -- [License](#license) +- [asdf-gofumpt](#asdf-gofumpt) + - [Contents](#contents) + - [Dependencies](#dependencies) + - [Install](#install) + - [Contributing](#contributing) + - [License](#license) -# Dependencies - -**TODO: adapt this section** +## Dependencies - `bash`, `curl`, `tar`: generic POSIX utilities. -- `SOME_ENV_VAR`: set this environment variable in your shell config to load the correct version of tool x. +- `GITUB_TOKEN` set this environment variable (must be a valid [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)) in your shell config to load the correct version of tool x. -# Install +## Install Plugin: @@ -34,7 +35,7 @@ gofumpt: ```shell # Show all installable versions -asdf list-all gofumpt +asdf list all gofumpt # Install specific version asdf install gofumpt latest @@ -49,12 +50,12 @@ gofumpt --version Check [asdf](https://github.com/asdf-vm/asdf) readme for more instructions on how to install & manage versions. -# Contributing +## Contributing Contributions of any kind welcome! See the [contributing guide](contributing.md). [Thanks goes to these contributors](https://github.com/looztra/asdf-gofumpt/graphs/contributors)! -# License +## License See [LICENSE](LICENSE) © [Christophe Furmaniak](https://github.com/looztra/) diff --git a/bin/download b/bin/download index b104db2..0d64cd7 100755 --- a/bin/download +++ b/bin/download @@ -5,19 +5,21 @@ 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" -# TODO: Adapt this to proper extension and adapt extracting strategy. -release_file="$ASDF_DOWNLOAD_PATH/$TOOL_NAME-$ASDF_INSTALL_VERSION.tar.gz" +# xTODO: Adapt this to proper extension and adapt extracting strategy. +release_file="$ASDF_DOWNLOAD_PATH/$TOOL_NAME-$ASDF_INSTALL_VERSION$(get_ext)" # Download tar.gz file to the download directory download_release "$ASDF_INSTALL_VERSION" "$release_file" # Extract contents of tar.gz file into the download directory -tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file" +#tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file" +cp "$release_file" "$ASDF_DOWNLOAD_PATH/gofumpt" +chmod +x "$ASDF_DOWNLOAD_PATH/gofumpt" # Remove the tar.gz file since we don't need to keep it rm "$release_file" diff --git a/bin/install b/bin/install index 9737a63..c0dc0b4 100755 --- a/bin/install +++ b/bin/install @@ -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" diff --git a/bin/latest-stable b/bin/latest-stable index b9a01c3..b9095a8 100755 --- a/bin/latest-stable +++ b/bin/latest-stable @@ -5,13 +5,13 @@ 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 . "${plugin_dir}/lib/utils.bash" 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 @@ -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" diff --git a/bin/list-all b/bin/list-all index 943371e..ba7a403 100755 --- a/bin/list-all +++ b/bin/list-all @@ -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 diff --git a/contributing.md b/contributing.md index ec1b8b0..1ed44a8 100644 --- a/contributing.md +++ b/contributing.md @@ -5,7 +5,6 @@ Testing Locally: ```shell asdf plugin test [--asdf-tool-version ] [--asdf-plugin-gitref ] [test-command*] -# TODO: adapt this asdf plugin test gofumpt https://github.com/looztra/asdf-gofumpt.git "gofumpt --version" ``` diff --git a/lib/utils.bash b/lib/utils.bash index 4e92791..6f38b62 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -8,67 +8,130 @@ TOOL_NAME="gofumpt" TOOL_TEST="gofumpt --version" 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 gofumpt 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 gofumpt has other means of determining installable versions. - list_github_tags + # xTODO/DONE: Adapt this. By default we simply list the tag names from GitHub releases. + # Change this function if gofumpt 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 arch + arch="$(get_arch)" + local platform + platform="$(get_platform)" + local ext + ext="$(get_ext)" + + #url="$GH_REPO/archive/v${version}.tar.gz" + url="$GH_REPO/releases/download/v${version}/gofumpt_v${version}_${platform}_${arch}${ext}" + + 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 -r "$ASDF_DOWNLOAD_PATH"/* "$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!" + echo "* Install locally or globally with:" + echo "asdf local $TOOL_NAME $version" + echo "asdf global $TOOL_NAME $version" + ) || ( + rm -rf "$install_path" + fail "An error occurred while installing $TOOL_NAME $version." + ) +} - # TODO: Adapt the release URL convention for gofumpt - url="$GH_REPO/archive/v${version}.tar.gz" +get_arch() { + arch=$(uname -m | tr '[:upper:]' '[:lower:]') + case ${arch} in + arm64) + arch='arm64' + ;; + arm6) + arch='arm6' + ;; + x86_64) + arch='amd64' + ;; + aarch64) + arch='arm64' + ;; + i386) + arch='i386' + ;; + esac + + echo "${arch}" +} - echo "* Downloading $TOOL_NAME release $version..." - curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url" +get_platform() { + plat=$(uname | tr '[:upper:]' '[:lower:]') + case ${plat} in + darwin) + plat='darwin' + ;; + linux) + plat='linux' + ;; + windows) + plat='windows' + ;; + esac + + echo "${plat}" } -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 -r "$ASDF_DOWNLOAD_PATH"/* "$install_path" - - # TODO: Assert gofumpt executable exists. - 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_ext() { + plat=$(uname | tr '[:upper:]' '[:lower:]') + case ${plat} in + windows) + ext='.exe' + ;; + *) + ext='' + ;; + esac + + echo "${ext}" } diff --git a/scripts/format.bash b/scripts/format.bash index 1a216ea..58e7723 100755 --- a/scripts/format.bash +++ b/scripts/format.bash @@ -1,4 +1,4 @@ #!/usr/bin/env bash -shfmt --language-dialect bash --write \ - ./**/* +shfmt --language-dialect bash --write --indent 2 \ + ./**/* diff --git a/scripts/lint.bash b/scripts/lint.bash index 3451a05..d8b4b1a 100755 --- a/scripts/lint.bash +++ b/scripts/lint.bash @@ -1,9 +1,9 @@ #!/usr/bin/env bash shellcheck --shell=bash --external-sources \ - bin/* --source-path=template/lib/ \ - lib/* \ - scripts/* + bin/* \ + lib/* \ + scripts/* -shfmt --language-dialect bash --diff \ - ./**/* +shfmt --language-dialect bash --indent 2 --diff \ + ./**/*