name: Go

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  vet:
    name: Vet
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.15

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

    - name: Vet code
      run: go vet

  format:
    name: Format
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.15

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

    - name: Set Git user
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"

    - name: Format code
      run: gofmt -l -w -s .

    - name: Commit and push changes
      id: commit-dockerfiles
      run: |
        git add .
        if output=$(git status --porcelain) && [ ! -z "$output" ]; then
          git commit -m "[Action] go fmt" -a
          git push
        fi

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.15

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

    - name: Build
      run: make

    - name: Test
      run: make test
