name: Setup kwok

description: This action sets up kwok/kwokctl for use in your workflow

inputs:
  command:
    required: true
    description: Command to install
  kwok-version:
    required: false
    description: Specific version of command to install, defaults to latest release
  repository:
    required: false
    description: Repository is kwok's repository, will use release from this repository, defaults same as uses in this step
    default: "kubernetes-sigs/kwok"

runs:
  using: composite
  steps:
  - name: Install ${{ inputs.command }}
    shell: bash
    env:
      KWOK_REPO: ${{ inputs.repository }}
      KWOK_VERSION: ${{ inputs.kwok-version }}
    run: |
      if [[ -f /usr/local/bin/${{ inputs.command }} ]]; then
        echo "Found ${{ inputs.command }} in /usr/local/bin, skipping installation"
        exit 0
      fi

      if [[ -z "${KWOK_VERSION}" ]]; then
        echo "Fetching latest version..."
        KWOK_VERSION="$(curl "https://api.github.com/repos/${KWOK_REPO}/releases/latest" | jq -r '.tag_name')"
        if [[ -z "${KWOK_VERSION}" ]]; then
          echo "Failed to fetch latest version"
          exit 1
        fi
        if [[ "${KWOK_VERSION}" == "null" ]]; then
          echo "Failed to fetch latest version"
          exit 1
        fi
        echo "Latest version is ${KWOK_VERSION}"
      fi

      echo "Installing ${{ inputs.command }} ${KWOK_VERSION}..."
      wget -O ${{ inputs.command }} "https://github.com/${KWOK_REPO}/releases/download/${KWOK_VERSION}/${{ inputs.command }}-$(go env GOOS)-$(go env GOARCH)"
      chmod +x ${{ inputs.command }}
      sudo mv ${{ inputs.command }} /usr/local/bin/
      if ! ${{ inputs.command }} --version; then
        echo "Failed to run ${{ inputs.command }} --version"
        exit 1
      fi