# this is a taskfile and is used with the dev tool named task
# you can install this tool with the following command:
# env GO111MODULE=on go get -u github.com/go-task/task/v3/cmd/task
# if you want to use it in a CI build script chain you can just use
# this
# go install github.com/go-task/task/v3/cmd/task@latest

---

version: '3'

vars:
  EXE: beaver{{exeExt}}
  OUTPUT: "build/{{.EXE}}"
  GOLANGCI_LINT_VERSION: v1.56.2
  GOLANGCI_LINT_BASE: tools/bin/golangci-lint
  GOLANGCI_LINT_BIN: "{{.GOLANGCI_LINT_BASE}}-{{.GOLANGCI_LINT_VERSION}}"
  GOLANGCI_LINT_ARGS:
  BUILD_COMMIT_ID:
    sh: hg id -i
  # this var is fetched from the env vars. It is intended to capture
  # the build tag from our CI builder if you want to use it manually
  # use a command like this:
  # CI_COMMIT_TAG=myTag task build
  # this will use the provided version for build linking
  BUILD_VERSION: "{{.CI_COMMIT_TAG}}/{{.BUILD_COMMIT_ID}}"
  GOOS:
    sh: uname | tr '[:upper:]' '[:lower:]'

tasks:
  default:
    cmds:
      - task: dev-build

  dev-build:
    desc: build golang project
    cmds:
      - mkdir -p build
      - |
        CGO_ENABLED=0 GOOS={{.GOOS}} GOARCH=amd64 go build \
          -o {{.OUTPUT}} \
          main.go
    generates:
      - "{{.OUTPUT}}"

  build:
    desc: build golang project
    cmds:
      - mkdir -p build
      - |
        CGO_ENABLED=0 GOOS={{.GOOS}} GOARCH=amd64 go build \
          -a -tags netgo \
          -ldflags "-X 'orus.io/orus-io/beaver/lib.version=${CI_COMMIT_TAG}' -X 'orus.io/orus-io/beaver/lib.commitSha=$(hg id -i --debug)' -X 'orus.io/orus-io/beaver/lib.buildDate=$(date)' -w -extldflags "-static"" \
          -o {{.OUTPUT}} \
          main.go
    generates:
      - "{{.OUTPUT}}"

  install:
    desc: install binary in $GOPATH/bin
    deps:
      - build
    cmds:
      - install -m 755 {{.OUTPUT}} $GOPATH/bin

  fetch-golangci-lint:
    desc: fetch golangci-lint tool
    cmds:
      - mkdir -p tools/bin
      - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b tools/bin {{.GOLANGCI_LINT_VERSION}}
      - mv {{.GOLANGCI_LINT_BASE}} {{.GOLANGCI_LINT_BIN}}
    status:
      - test -x {{.GOLANGCI_LINT_BIN}}
    generates:
      - "{{.GOLANGCI_LINT_BIN}}"

  fetch-govulncheck:
    desc: fetch govulncheck
    cmds:
      - go install golang.org/x/vuln/cmd/govulncheck@latest

  lint:
    desc: lint our golang code to make sure we catch as much errors as possible
    deps: [fetch-golangci-lint]
    cmds:
      - "{{.GOLANGCI_LINT_BIN}} run {{.GOLANGCI_LINT_ARGS}}"

  vulncheck:
    desc: search for known vulnerabilities in our code and our libraries
    deps: [fetch-govulncheck]
    cmds:
      - "govulncheck ./..."

  test:
    desc: Run the tests
    deps: []
    cmds:
      - go test ./...

  cover:
    desc: Run tests with coverage
    deps: []
    cmds:
      - go test -p 1 -covermode=count -coverpkg=./... -coverprofile .cover.cov ./...
      - go tool cover -func=.cover.cov
    generates:
      - .cover.cov

  cover-html:
    desc: Creates an html report from the last cover run
    deps: []
    cmds:
      - go tool cover -html=.cover.cov -o coverage.html
    generates:
      - coverage.html
