---
version: '3'

tasks:
  labels:
    deps:
      - :install:software:jq
    vars:
      GROUP_URL:
        sh: jq -r '.repository.group.dockerfile' .variables.json
    log:
      start: Ensuring `Dockerfile` labels are up-to-date
      success: '`Dockerfile` labels are up-to-date'
    cmds:
      - task: labels:add
      - task: labels:update
    sources:
      - .variables.json
      - Dockerfile
      - package.json
    preconditions:
      - sh: test -f Dockerfile
        msg: A `Dockerfile` is not present in the root of this project!
      - sh: test -f .variables.json
        msg: The `.variables.json` file is not present in the root of this project!
      - sh: test -f package.json
        msg: The `package.json` file is not present in the root of this project!

  labels:add:
    vars:
      AUTHORS:
        sh: jq -r '.docker_label_authors' .variables.json
      HELP_EMAIL:
        sh: jq -r '.email.help' .variables.json
      LICENSE:
        sh: jq -r '.license' .variables.json
      ORGANIZATION:
        sh: jq -r '.organization' .variables.json
      URL:
        sh: jq -r '.link.home' .variables.json
    log:
      error: Failed to add Dockerfile labels
      start: Ensuring default Dockerfile labels are present (e.g. `org.opencontainers.image` tags)
      success: Successfully injected `org.opencontainers.image` labels into Dockerfile
    cmds:
      - |
        echo 'ARG BUILD_DATE' >> Dockerfile
        echo 'ARG REVISION' >> Dockerfile
        echo 'ARG VERSION' >> Dockerfile
        echo '' >> Dockerfile
        echo 'LABEL maintainer="{{.ORGANIZATION}} <{{.HELP_EMAIL}}>"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.authors="{{.AUTHORS}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.created=$BUILD_DATE' >> Dockerfile
        echo 'LABEL org.opencontainers.image.description="[[ Injected by running `task update` ]]"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.documentation="[[ Injected by running `task update` ]]"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.licenses="{{.LICENSE}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.revision=$REVISION' >> Dockerfile
        echo 'LABEL org.opencontainers.image.source="[[ Injected by running `task update` ]]"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.url="{{.URL}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.vendor="{{.ORGANIZATION}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.version=$VERSION' >> Dockerfile
        echo 'LABEL space.megabyte.type="{{.REPOSITORY_SUBTYPE}}"' >> Dockerfile
        echo '' >> Dockerfile
    status:
      - grep 'org.opencontainers.image.documentation' Dockerfile

  # yamllint disable rule:line-length
  # eslint-disable max-len
  labels:update:
    vars:
      AUTHORS:
        sh: jq -r '.docker_label_authors' .variables.json
      GROUP_URL:
        sh: jq -r '.repository.group.dockerfile' .variables.json
      LICENSE:
        sh: jq -r '.license' .variables.json
      ORGANIZATION:
        sh: jq -r '.organization' .variables.json
      URL:
        sh: jq -r '.link.home' .variables.json
    env:
      DESCRIPTION:
        sh: jq -r '.description' package.json
      REPOSITORY_GITHUB:
        sh: jq -r '.blueprint.repository.github' package.json
    log:
      error: Encountered error while updating `Dockerfile` labels
    cmds:
      - sed -i "s^.*org.opencontainers.image.authors.*^LABEL org.opencontainers.image.authors=\"{{.AUTHORS}}\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.description.*^LABEL org.opencontainers.image.description=\"${DESCRIPTION}\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.documentation.*^LABEL org.opencontainers.image.documentation=\"${REPOSITORY_GITHUB}/blob/master/README.md\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.licenses.*^LABEL org.opencontainers.image.licenses=\"{{.LICENSE}}\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.source.\*^LABEL org.opencontainers.image.source=\"${REPOSITORY_GITHUB}.git\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.url.\*^LABEL org.opencontainers.image.url=\"{{.URL}}\"^g" Dockerfile
      - sed -i "s^.*org.opencontainers.image.vendor.\*^LABEL org.opencontainers.image.vendor=\"{{.ORGANIZATION}}\"^g" Dockerfile
# yamllint enable rule:line-length
# eslint-enable max-len
