language: go

go:
  - 1.10.8
  - 1.11.5
  - master

env:
  - DEP_VERSION="0.5.0"

sudo: required

services:
  - docker

before_install:
  # Download the binary to bin folder in $GOPATH
  - curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
  # Make the binary executable
  - chmod +x $GOPATH/bin/dep

install:
  - dep ensure

matrix:
  # It's ok if our code fails on unstable development versions of Go.
  allow_failures:
    - go: master
  # Don't wait for tip tests to finish. Mark the test run green if the
  # tests pass on the stable versions of Go.
  fast_finish: true

# Don't email me the results of the test runs.
notifications:
  email: false
  webhooks:
    urls:
      - https://webhooks.gitter.im/e/ec30ac1bb280c976f8be
    on_success: change  # options: [always|never|change] default: always
    on_failure: always  # options: [always|never|change] default: always
    on_start: never     # options: [always|never|change] default: always

# Anything in before_script that returns a nonzero exit code will
# flunk the build and immediately stop. It's sorta like having
# set -e enabled in bash.
before_script:
  - GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/) # All the .go files, excluding vendor/
  - go get golang.org/x/lint/golint                             # Linter
  - go get honnef.co/go/tools/cmd/staticcheck                   # Badass static analyzer/linter
  - go get github.com/fzipp/gocyclo
  - go get github.com/mattn/goveralls

# script always run to completion (set +e). All of these code checks are must haves
# in a modern Go project.
script:
  - test -z $(gofmt -s -l $GO_FILES)         # Fail if a .go file hasn't been formatted with gofmt
  - go test --timeout 5s -v -race ./...      # Run all the tests with the race detector enabled
  - ./testAndCover.sh                        # Run the tests again to get coverage info
  - go vet -composites=false ./...           # go vet is the official Go static analyzer
  - staticcheck ./...                        # "go vet on steroids" + linter
  - gocyclo -over 15 $GO_FILES               # forbid code with huge functions
  - golint -set_exit_status $(go list ./...) # one last linter

# If the build succeeds, send coverage to coveralls
# goreleaser will run if the latest version tag matches the current commit
after_success:
  - $GOPATH/bin/goveralls -coverprofile=profile.cov -service=travis-ci
  - if [[ "$TRAVIS_SECURE_ENV_VARS" == true && "$TRAVIS_GO_VERSION" == 1.11* ]]; then docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"; curl -sL https://git.io/goreleaser | bash; fi
