name: mc-find
description: "Find files in s3."
inputs:
  accessKey:
    description: "S3 access key."
    required: true
  secretKey:
    description: "S3 secret key."
    required: true
  alias:
    description: "S3 alias."
    required: true
  url:
    description: "S3 url."
    required: true
    default: https://s3-us-gov-west-1.amazonaws.com
  path:
    description: "The path to search for."
    required: true
  name:
    description: "The name pattern to search for."
    required: true
    default: ""
  additionalArgs:
    description: "Additional arguments."
    required: false
outputs:
  console:
    description: "The console output of the command."
    value: ${{ steps.output.outputs.console }}
  success:
    description: "Whether files were found."
    value: ${{ steps.output.outputs.success }}
runs:
  using: "composite"
  steps:
  - name: "Build command."
    id: command
    run: |
      COMMAND="find \"${{ inputs.path }}\""

      if [[ -n "${{ inputs.name }}" ]]; then
        COMMAND="$COMMAND --name '${{ inputs.name }}'"
      fi

      COMMAND="$COMMAND ${{ inputs.additionalArgs }}"

      echo "command=$COMMAND" >> "$GITHUB_OUTPUT"
    shell: bash
  - name: "Find file in S3."
    id: find
    uses: act/common/minio/mc@master
    with:
      alias: ${{ inputs.alias }}
      command: ${{ steps.command.outputs.command }}
      accessKey: ${{ inputs.accessKey }}
      secretKey: ${{ inputs.secretKey }}
      url: ${{ inputs.url }}
      catchErrors: true
  - name: "Set output."
    id: output
    run: |
      RESULT="${{ steps.find.outputs.exitCode }}"

      if [[ "$RESULT" != "0" ]]; then
        SUCCESS="false"
      else
        echo "console<<EOF" >> "$GITHUB_OUTPUT"
        echo "${{ steps.find.outputs.console }}" >> "$GITHUB_OUTPUT"
        echo "EOF" >> "$GITHUB_OUTPUT"
        SUCCESS="true"
      fi

      echo "success=$SUCCESS" >> "$GITHUB_OUTPUT"
    shell: bash
