From b8ad7a6aa043ba59b823147c7d01ffa9f73a7fd0 Mon Sep 17 00:00:00 2001 From: unkayk Date: Wed, 29 Jul 2026 04:07:56 +0630 Subject: [PATCH] Improve run-komac action: safer shell, optional args, and outputs Signed-off-by: unkayk --- action.yml | 169 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 69 deletions(-) diff --git a/action.yml b/action.yml index 1fc4784..4522aaf 100644 --- a/action.yml +++ b/action.yml @@ -1,69 +1,100 @@ -name: 'Run Komac' -description: 'Sets up and runs Komac with optional customizations' -branding: - color: 'blue' - icon: 'upload-cloud' - -inputs: - komac-version: - description: 'Komac version to use (default: latest)' - required: false - default: 'latest' - args: - description: 'Arguments to run Komac with' - required: true - custom-fork-owner: - description: 'Specify a custom fork owner of Komac, if any' - required: false - custom-tool: - description: 'Name of a custom tool to use with Komac' - required: false - custom-tool-url: - description: 'URL of the custom tool, if applicable' - required: false - -runs: - using: 'composite' - steps: - - name: Check for Komac Installation - id: check_komac - shell: bash - run: | - if komac --version &> /dev/null; then - echo "Komac is already installed." - echo "komac_installed=true" >> $GITHUB_OUTPUT - else - echo "Komac is not installed." - echo "komac_installed=false" >> $GITHUB_OUTPUT - fi - - - name: Install binstall - if: steps.check_komac.outputs.komac_installed == 'false' - uses: cargo-bins/cargo-binstall@main - - - name: Install Komac - if: steps.check_komac.outputs.komac_installed == 'false' - shell: bash - run: | - if [ "${{ inputs.komac-version }}" == 'latest' ]; then - cargo binstall komac -y - else - cargo binstall komac@${{ inputs.komac-version }} -y - fi - - - name: Set Custom Environment Variables - shell: bash - run: | - if [[ -n "${{ inputs.custom-fork-owner }}" ]]; then - echo "KOMAC_FORK_OWNER=${{ inputs.custom-fork-owner }}" >> $GITHUB_ENV - fi - if [[ -n "${{ inputs.custom-tool }}" ]]; then - echo "KOMAC_CREATED_WITH=${{ inputs.custom-tool }}" >> $GITHUB_ENV - fi - if [[ -n "${{ inputs.custom-tool-url }}" ]]; then - echo "KOMAC_CREATED_WITH_URL=${{ inputs.custom-tool-url }}" >> $GITHUB_ENV - fi - - - name: Run Komac - shell: bash - run: komac ${{ inputs.args }} +name: 'Run Komac' +description: 'Sets up and runs Komac with optional customizations' +branding: + color: 'blue' + icon: 'upload-cloud' + +inputs: + komac-version: + description: 'Komac version to use (default: latest)' + required: false + default: 'latest' + args: + description: 'Arguments to run Komac with' + required: false + default: '' + custom-fork-owner: + description: 'Specify a custom fork owner of Komac, if any' + required: false + custom-tool: + description: 'Name of a custom tool to use with Komac' + required: false + custom-tool-url: + description: 'URL of the custom tool, if applicable' + required: false + +outputs: + komac-exit-code: + description: 'Exit code returned by komac' + +runs: + using: 'composite' + steps: + - name: Check for Komac Installation + id: check_komac + shell: bash + run: | + # fail fast and safer shell behavior + set -euo pipefail + + # Prefer using command -v to detect whether komac is available in PATH + if command -v komac >/dev/null 2>&1; then + echo "Komac is already installed." + echo "komac_installed=true" >> $GITHUB_OUTPUT + else + echo "Komac is not installed." + echo "komac_installed=false" >> $GITHUB_OUTPUT + fi + + - name: Install binstall + if: steps.check_komac.outputs.komac_installed == 'false' + # Pin this to a released tag or commit instead of `main` for security. + # e.g., cargo-bins/cargo-binstall@vX.Y.Z or a commit SHA. + uses: cargo-bins/cargo-binstall@main + + - name: Install Komac + if: steps.check_komac.outputs.komac_installed == 'false' + shell: bash + run: | + set -euo pipefail + if [ "${{ inputs.komac-version }}" = 'latest' ]; then + cargo binstall komac -y + else + cargo binstall "komac@${{ inputs.komac-version }}" -y + fi + + - name: Set Custom Environment Variables + shell: bash + run: | + set -euo pipefail + if [[ -n "${{ inputs.custom-fork-owner }}" ]]; then + echo "KOMAC_FORK_OWNER=${{ inputs.custom-fork-owner }}" >> $GITHUB_ENV + fi + if [[ -n "${{ inputs.custom-tool }}" ]]; then + echo "KOMAC_CREATED_WITH=${{ inputs.custom-tool }}" >> $GITHUB_ENV + fi + if [[ -n "${{ inputs.custom-tool-url }}" ]]; then + echo "KOMAC_CREATED_WITH_URL=${{ inputs.custom-tool-url }}" >> $GITHUB_ENV + fi + + - name: Run Komac + id: run_komac + shell: bash + run: | + set -euo pipefail + + # If args were provided, pass them; otherwise run with no args + if [[ -n "${{ inputs.args }}" ]]; then + # inputs.args is inserted by GitHub Actions — it will be passed as-is. + komac ${{ inputs.args }} + else + komac + fi + EXIT_CODE=$? + echo "komac_exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT + + - name: Export komac exit code as action output + shell: bash + run: | + set -euo pipefail + echo "komac-exit-code=${{ steps.run_komac.outputs.komac_exit_code }}" >> $GITHUB_OUTPUT