name: Go

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

jobs:
  build-linux:
    name: Build (ubuntu-latest)
    runs-on: ubuntu-latest
    container:
      image: ubuntu:latest
      options: --privileged
      env:
        DEBIAN_FRONTEND: noninteractive

    steps:
    - name: Check out code into $GITHUB_WORKSPACE directory
      uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: ^1.22
      id: go

    - name: Install Dependencies
      run: |
        apt-get update
        apt-get install -y gnome-keyring build-essential ca-certificates
        mkdir -p /github/home/.cache/
        mkdir -p /github/home/.local/share/keyrings/
        chmod 700 -R /github/home/.local/

    - name: Get dependencies
      run: |
        go get -v -t -d ./...

    - name: Build
      run: |
        go build -v ./...

    - name: Test
      run: |
        echo 'somecredstorepass' | gnome-keyring-daemon --unlock
        go test -v ./...
      shell: dbus-run-session -- bash --noprofile --norc -eo pipefail {0}

  build-other:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macOS-latest, windows-latest]

    steps:
    - name: Check out code into $GITHUB_WORKSPACE directory
      uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: ^1.22
      id: go

    - name: Get dependencies
      run: |
        go get -v -t -d ./...

    - name: Build
      run: |
        go build -v ./...

    - name: Test
      run: |
        go test -v ./...

  build-freebsd:
    name: Build (FreeBSD)
    # runs-on: macos-12
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Test in FreeBSD
      id: test
      uses: vmactions/freebsd-vm@v1
      with:
        usesh: true
        prepare: pkg install -y go gnome-keyring
        run: |
          go version
          go build -v
          dbus-run-session -- sh -c "echo 'somecredstorepass' | gnome-keyring-daemon --unlock; go test -v ./..."
          # verify that we can build for freebsd with cgo disabled
          # This will disable the functionality as it depends on cgo (via
          # godbus) but should still be buildable to not break backwards
          # compatibility
          CGO_ENABLED=0 go build -v
