version: 2.1
orbs:
  win: circleci/windows@2.4.0  
  aws-cli: circleci/aws-cli@1.4.0

executors:
  go-1_15:
    working_directory: '/go/src/github.com/influxdata/telegraf'
    docker:
      - image: 'quay.io/influxdb/telegraf-ci:1.15.8'
    environment:
      GOFLAGS: -p=8
  go-1_16:
    working_directory: '/go/src/github.com/influxdata/telegraf'
    docker:
      - image: 'quay.io/influxdb/telegraf-ci:1.16.5'
    environment:
      GOFLAGS: -p=8
  mac:
    macos:
      xcode: 12.4.0
    working_directory: '~/go/src/github.com/influxdata/telegraf'
    environment:
      HOMEBREW_NO_AUTO_UPDATE: 1
      GOFLAGS: -p=8

commands:
  check-changed-files-or-halt:
      steps:
        - run: ./scripts/check-file-changes.sh
  check-changed-files-or-halt-windows:
      steps:
        - run: 
            command: ./scripts/check-file-changes.sh
            shell: bash.exe
  test-go:
    parameters:
      goarch:
        type: string
        default: "amd64"
    steps:
      - checkout
      - check-changed-files-or-halt
      - attach_workspace:
          at: '/go'
      - run: 'GOARCH=<< parameters.goarch >> make'
      - run: 'GOARCH=<< parameters.goarch >> make check'
      - run: 'GOARCH=<< parameters.goarch >> make check-deps'
      - run: 'GOARCH=<< parameters.goarch >> make test'
  package-build:
    parameters:
      release:
        type: boolean
        default: false
      nightly:
        type: boolean
        default: false
      type:
        type: string
        default: ""
    steps:
      - checkout
      - check-changed-files-or-halt
      - attach_workspace:
          at: '/go'
      - when:
          condition: << parameters.release >>
          steps: 
            - run: 'mips=1 mipsel=1 arm64=1 amd64=1 static=1 armel=1 armhf=1 s390x=1 ppc641e=1 i386=1 windows=1 darwin=1 make package'
      - when:
          condition: << parameters.nightly >>
          steps: 
            - run: 'mips=1 mipsel=1 arm64=1 amd64=1 static=1 armel=1 armhf=1 s390x=1 ppc641e=1 i386=1 windows=1 darwin=1 NIGHTLY=1 make package'
            - run: 'make upload-nightly'
      - unless:
          condition:
            or:
              - << parameters.nightly >>
              - << parameters.release >>
          steps:
            - run: '<< parameters.type >>=1 make package'
      - store_artifacts:
          path: './build/dist'
          destination: 'build/dist'
      - persist_to_workspace:
          root: './build'
          paths:
            - 'dist'
jobs:
  deps:
    executor: go-1_16
    steps:
      - checkout
      - restore_cache:
          key: go-mod-v1-{{ checksum "go.sum" }}
      - check-changed-files-or-halt
      - run: 'make deps'
      - run: 'make tidy'
      - save_cache:
          name: 'go module cache'
          key: go-mod-v1-{{ checksum "go.sum" }}
          paths:
            - '/go/pkg/mod'
      - persist_to_workspace:
          root: '/go'
          paths:
            - '*'
  test-go-1_15:
    executor: go-1_15
    steps:
      - test-go
  test-go-1_15-386:
    executor: go-1_15
    steps:
      - test-go:
          goarch: "386"
  test-go-1_16:
    executor: go-1_16
    steps:
      - test-go
  test-go-1_16-386:
    executor: go-1_16
    steps:
      - test-go:
          goarch: "386"
  test-go-mac:
    executor: mac
    steps:
      - checkout
      - restore_cache:
          key: mac-go-mod-v0-{{ checksum "go.sum" }}
      - check-changed-files-or-halt
      - run: 'sh ./scripts/mac_installgo.sh'
      - save_cache:
          name: 'Saving cache'
          key: mac-go-mod-v0-{{ checksum "go.sum" }}
          paths:
            - '/usr/local/Cellar/go'
            - '/usr/local/bin/go'
            - '/usr/local/bin/gofmt'
      - run: 'make deps'
      - run: 'make tidy'
      - run: 'make'
      - run: 'make check'
      - run: 'make test'
  test-go-windows:
    executor:
        name: win/default
        shell: powershell.exe
    steps:
      - checkout
      - check-changed-files-or-halt-windows
      - run: choco upgrade golang --version=1.16.5
      - run: choco install make
      - run: git config --system core.longpaths true
      - run: make test-windows

  windows-package:
    executor: go-1_16
    steps:
      - package-build:
          type: windows
  darwin-package:
    executor: go-1_16
    steps:
      - package-build:
          type: darwin
  i386-package:
    executor: go-1_16
    steps:
      - package-build:
          type: i386
  ppc641e-package:
    executor: go-1_16
    steps:
      - package-build:
          type: ppc641e
  s390x-package:
    executor: go-1_16
    steps:
      - package-build:
          type: s390x
  armel-package:
    executor: go-1_16
    steps:
      - package-build:
          type: armel
  amd64-package:
    executor: go-1_16
    steps:
      - package-build:
          type: amd64
  arm64-package:
    executor: go-1_16
    steps:
      - package-build:
          type: arm64
  mipsel-package:
    executor: go-1_16
    steps:
      - package-build:
          type: mipsel
  mips-package:
    executor: go-1_16
    steps:
      - package-build:
          type: mips
  static-package:
    executor: go-1_16
    steps:
      - package-build:
          type: static
  armhf-package:
    executor: go-1_16
    steps:
      - package-build:
          type: armhf

  release:
    executor: go-1_16
    steps:
      - package-build:
          release: true
  nightly:
    executor: go-1_16
    steps:
      - package-build:
          nightly: true
  package-consolidate:
    executor:
        name: win/default
        shell: powershell.exe
    steps:
      - attach_workspace:
          at: '/build'
      - store_artifacts:
          path: './build/dist'
          destination: 'build/dist'
  package-sign-windows:
    executor:
        name: win/default
        shell: powershell.exe
    steps:
      - checkout
      - check-changed-files-or-halt
      - attach_workspace:
          at: '/build'
      - run:
          name: "Sign Windows Executables"
          shell: powershell.exe
          command: |
            ./scripts/windows-signing.ps1
      - persist_to_workspace:
          root: './build'
          paths:
            - 'dist'
      - store_artifacts:
          path: './build/dist'
          destination: 'build/dist'
  package-sign-mac:
    macos:
      xcode: "11.3"
    working_directory: /Users/distiller/project
    environment:
      FL_OUTPUT_DIR: output
      FASTLANE_LANE: test
    shell: /bin/bash --login -o pipefail
    steps:
      - checkout
      - check-changed-files-or-halt
      - attach_workspace:
          at: '.'
      - run:
          command: |
            sh ./scripts/mac-signing.sh
      - store_artifacts:
          path: './dist'
          destination: 'build/dist'
  test-awaiter:
    executor: go-1_16
    steps:
      - run:
          command: |
            echo "Go tests complete."
  share-artifacts:
    executor: aws-cli/default  
    steps:
      - run:
          command: |
            PR=${CIRCLE_PULL_REQUEST##*/}
            printf -v payload '{ "pullRequestNumber": "%s" }' "$PR"
            curl -X POST "https://182c7jdgog.execute-api.us-east-1.amazonaws.com/prod/shareArtifacts" --data "$payload"   
                      
workflows:
  version: 2
  check:
    jobs:
      - 'deps':
          filters:
            tags:
              only: /.*/
      - 'test-go-1_15':
          requires:
            - 'deps'
          filters:
            tags:
              only: /.*/
      - 'test-go-1_15-386':
          requires:
            - 'deps'
          filters:
            tags:
              only: /.*/
      - 'test-go-1_16':
          requires:
            - 'deps'
          filters:
            tags:
              only: /.*/
      - 'test-go-1_16-386':
          requires:
            - 'deps'
          filters:
            tags:
              only: /.*/
      - 'test-go-mac':
          filters:
            tags: # only runs on tags if you specify this filter
              only: /.*/
      - 'test-go-windows':
          filters:
            tags:
              only: /.*/
      - 'test-awaiter': 
          requires:
            - 'test-go-1_15'
            - 'test-go-1_15-386'
            - 'test-go-1_16'
            - 'test-go-1_16-386'
      - 'windows-package':
          requires: 
            - 'test-go-windows'
      - 'darwin-package':
          requires: 
            - 'test-go-mac'
      - 'i386-package':
          requires: 
            - 'test-awaiter'
      - 'ppc641e-package':
          requires: 
            - 'test-awaiter'
      - 's390x-package':
          requires: 
            - 'test-awaiter'
      - 'armel-package':
          requires: 
            - 'test-awaiter'
      - 'amd64-package':
          requires: 
            - 'test-awaiter'
      - 'arm64-package':
          requires: 
            - 'test-awaiter'
      - 'armhf-package':
          requires: 
            - 'test-awaiter'
      - 'static-package':
          requires:
            - 'test-awaiter'
      - 'mipsel-package':
          requires: 
            - 'test-awaiter'
      - 'mips-package':
          requires: 
            - 'test-awaiter'
      - 'share-artifacts':
          requires:
            - 'i386-package'
            - 'ppc641e-package'
            - 's390x-package'
            - 'armel-package'
            - 'amd64-package'
            - 'mipsel-package'
            - 'mips-package'
            - 'darwin-package'
            - 'windows-package'
            - 'static-package'
            - 'arm64-package'
            - 'armhf-package'
          filters:
            branches:
              ignore:
                - master
      - 'release':
          requires:
            - 'test-go-windows'
            - 'test-go-mac'
            - 'test-go-1_15'
            - 'test-go-1_15-386'
            - 'test-go-1_16'
            - 'test-go-1_16-386'
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
      - 'package-sign-windows':
          requires:
            - 'release'
          filters:
              tags:
                only: /.*/
      - 'package-sign-mac':
           requires:
             - 'package-sign-windows' 
           filters:
              tags:
                only: /.*/

  nightly:
    jobs:
      - 'deps'
      - 'test-go-1_15':
          requires:
            - 'deps'
      - 'test-go-1_15-386':
          requires:
            - 'deps'
      - 'test-go-1_16':
          requires:
            - 'deps'
      - 'test-go-1_16-386':
          requires:
            - 'deps'
      - 'test-go-mac'
      - 'test-go-windows'
      - 'nightly':
          requires:
            - 'test-go-windows'
            - 'test-go-mac'
            - 'test-go-1_15'
            - 'test-go-1_15-386'
            - 'test-go-1_16'
            - 'test-go-1_16-386'
    triggers:
      - schedule:
          cron: "0 7 * * *"
          filters:
            branches:
              only:
                - master