---
name: Setup build
description: Setup the OS-independent build environment
inputs:
  setup-go:
    description: Enable Go setup
    required: false
    default: "true"
  setup-node:
    description: Enable Node setup
    required: false
    default: "true"
  node-version:
    description: Node version to install
    required: false
    default: "20"
outputs:
  iana-cache-hit:
    description: Was cache hit for IANA files?
    value: ${{ steps.iana-cache.outputs.cache-matched-key != '' }}
  iana-cache-key:
    description: Cache key to be used for IANA files
    value: ${{ steps.iana-cache.outputs.cache-primary-key }}
runs:
  using: composite
  steps:
    - uses: actions/setup-go@v5
      if: ${{ inputs.setup-go == 'true' }}
      id: go-setup
      with:
        go-version-file: go.mod
        check-latest: true
        cache: false
    - name: Get Go environment
      if: ${{ inputs.setup-go == 'true' }}
      shell: bash
      run: |
        echo "gocache=$(go env GOCACHE)" >> $GITHUB_ENV
        echo "gomodcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
        echo "today=$(date +%Y%m%d)" >> $GITHUB_ENV
    - uses: actions/cache@v4
      if: ${{ inputs.setup-go == 'true' }}
      with:
        path: |
          ${{ env.gocache }}
          ${{ env.gomodcache }}
        key: ${{ runner.os }}-${{ runner.arch }}-go${{ steps.go-setup.outputs.go-version }}-${{ hashFiles('go.sum', 'bin/external-tool') }}-${{ env.today }}
        restore-keys: |
          ${{ runner.os }}-${{ runner.arch }}-go${{ steps.go-setup.outputs.go-version }}-${{ hashFiles('go.sum', 'bin/external-tool') }}-
          ${{ runner.os }}-${{ runner.arch }}-go${{ steps.go-setup.outputs.go-version }}-
    - uses: pnpm/action-setup@v4
      if: ${{ inputs.setup-node == 'true' }}
      with:
        package_json_file: console/frontend/package.json
    - uses: actions/setup-node@v4
      if: ${{ inputs.setup-node == 'true' }}
      with:
        node-version: ${{ inputs.node-version }}
        cache: pnpm
        cache-dependency-path: |
          console/frontend/pnpm-lock.yaml
          bin/external-tool
    - name: Restore IANA files from cache
      uses: actions/cache/restore@v4
      id: iana-cache
      with:
        path: |
          orchestrator/clickhouse/data/udp.csv
          orchestrator/clickhouse/data/tcp.csv
        key: iana-cache-${{ hashFiles('Makefile', 'flake.lock') }}-${{ github.run_id }}
        restore-keys: |
          iana-cache-${{ hashFiles('Makefile', 'flake.lock') }}-
