name: Go

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:

  build:
    runs-on: ubuntu-latest
    steps:

      - name: Check out code into the Go module directory
        uses: actions/checkout@v3

      - name: Set up Go 1.x
        uses: actions/setup-go@v4
        with:
          go-version: '>=1.25.0'

      - name: Check Go Version
        run: go version

      - name: Link workspace with GOPATH
        continue-on-error: false
        run: |
          sudo mkdir -vp $(dirname ${GOPATH}/src/github.com/${GITHUB_REPOSITORY})
          sudo chown -R ${USER}:users $(dirname ${GOPATH}/src/github.com/${GITHUB_REPOSITORY})
          sudo chmod ug+rw $(dirname ${GOPATH}/src/github.com/${GITHUB_REPOSITORY})
          sudo ln -svf $(pwd) ${GOPATH}/src/github.com/${GITHUB_REPOSITORY}

      - name: Check out tools into the Go module directory
        continue-on-error: false
        run: |
          git clone https://github.com/nabbar/gotools.git scripts
          ./scripts/prepare

      - name: Update vendor or dependancies
        continue-on-error: false
        run: ./scripts/ci_depend

      - name: Check goFmt & goImport
        continue-on-error: false
        run: ./scripts/ci_format

      - name: Check Missing License
        continue-on-error: false
        run: ./scripts/ci_license

      - name: Check goLinter
        continue-on-error: true
        run: ./scripts/ci_linter
        env:
          GOOS: linux
          GOARCH: amd64
          CGO_ENABLED: 0

      - name: Check goSecu + snyk.io
        continue-on-error: true
        run: ./scripts/ci_secu $SNYK_TOKEN
        env:
          GOOS: linux
          GOARCH: amd64
          CGO_ENABLED: 0
          SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

      - name: Check tests
        uses: nick-fields/retry@v2.7.1
        continue-on-error: false
        with:
          max_attempts: 3
          timeout_minutes: 15
          command: |
            wget --quiet --output-document="aws/minio" "https://dl.min.io/server/minio/release/linux-amd64/minio" 
            chmod -v +x "aws/minio"
            go version
            ginkgo version
            for PKG in $(find $(pwd) -type f -name "*_suite_test.go" | sort -u );
            do
              cd $(dirname $PKG);
              echo "testing >>> $(basename $(dirname $PKG))";
              ginkgo run --cover .
            done
        env:
          GOOS: linux
          GOARCH: amd64
          CGO_ENABLED: 0

      - name: Add ming-w32 (x86 + win64)
        continue-on-error: false
        run: sudo apt-get install gcc-multilib gcc-mingw-w64

      - name: Test Build Linux/amd64 with suffix/CGO
        continue-on-error: false
        run: |
          IGNORE_BUILD=$(sed '/^[[:space:]]*$/d' "build.$(go env | grep GOARCH | cut -d'=' -f2 | tr -d '"' | tr -d "'")" | tr '\n' '|')
          CGO_ENABLED=1 GOOS=linux GOARCH=amd64 GOAMD64=v4 go build -a -race -installsuffix cgo -ldflags "-w -s -extldflags '-static' " $(go list ./... | grep -vPi ${IGNORE_BUILD::-1})

      - name: Test Build Windows/amd64 with CGO
        continue-on-error: false
        run: |
          IGNORE_BUILD=$(sed '/^[[:space:]]*$/d' "build.$(go env | grep GOARCH | cut -d'=' -f2 | tr -d '"' | tr -d "'")" | tr '\n' '|')
          CC=/usr/bin/x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 GOAMD64=v4 go build -a -ldflags "-w -s -extldflags '-static' " $(go list ./... | grep -vPi ${IGNORE_BUILD::-1})

      - name: Test Build Darwin/arm64 with suffix/CGO
        continue-on-error: false
        run: |
          IGNORE_BUILD=$(sed '/^[[:space:]]*$/d' "build.$(go env | grep GOARCH | cut -d'=' -f2 | tr -d '"' | tr -d "'")" | tr '\n' '|')
          CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 GOAMD64=v1 go build -a -ldflags "-w -s -extldflags '-static' " $(go list ./... | grep -vPi ${IGNORE_BUILD::-1})

