version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:1.12
    environment:
      GO111MODULE: "on"
    working_directory: /go/src/github.com/jesseduffield/lazydocker
    steps:
      - checkout
      - run:
          name: Run gofmt -s
          command: |
            if [ $(find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \;|wc -l) -gt 0 ]; then
             find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \;
             exit 1;
            fi
      - restore_cache:
          keys:
            - pkg-cache-{{ checksum "go.sum" }}-v5
      - run:
          name: Run tests
          command: |
            ./test.sh
      - run:
          name: Push on codecov result
          command: |
            bash <(curl -s https://codecov.io/bash)
      - run:
          name: Compile project on every platform
          command: |
            go get github.com/mitchellh/gox
            gox -mod=vendor -parallel 10 -os "linux freebsd" -osarch "darwin/i386 darwin/amd64"
      - save_cache:
          key: pkg-cache-{{ checksum "go.sum" }}-v5
          paths:
            - ~/.cache/go-build

  release:
    docker:
      - image: circleci/golang:1.12
    working_directory: /go/src/github.com/jesseduffield/lazydocker
    steps:
      - checkout
      - run:
          name: Install snapcraft
          command: |
            ./.circleci/install_snapcraft.sh
      - run:
          name: Login to snapcraft
          # if you ever lose this, you can recreate it via
          # `snapcraft export-login snapcraft.login && base64 snapcraft.login` and then chuck that
          # in circle ci as the SNAPCRAFT_LOGIN_FILE env variable
          # You'll need your ubuntu one password
          command: |
            echo $SNAPCRAFT_LOGIN_FILE | base64 --decode --ignore-garbage > snapcraft.login
            snapcraft login --with snapcraft.login
            rm snapcraft.login
      - run:
          name: Run gorelease
          command: |
            curl -sL https://git.io/goreleaser | bash
      - run:
          name: Update docs
          command: |
            ./.circleci/update_docs.sh

workflows:
  version: 2
  build:
    jobs:
      - build
  release:
    jobs:
      - release:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
