apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: download-file
  labels:
    dagweave.io/catalog-item: download-file
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.1"
    dagweave.io/catalog-category: "HTTP"
spec:
  entrypoint: download
  templates:
    - name: download
      inputs:
        parameters:
          # Absolute URL of the file to download. Redirects are followed.
          - name: url
          # Filesystem path to write the downloaded file to.
          - name: dest-path
            default: /tmp/download
          # Name/key of a Kubernetes Secret holding an optional bearer token. If the
          # Secret or key is absent the Authorization header is omitted.
          - name: token-secret-name
            default: download-token
          - name: token-secret-key
            default: token
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # curlimages/curl:8.23.0
        image: curlimages/curl@sha256:3fb7e13e5d179a845c97d78204f3f1064d1907037f26a3ebf91710571151fd93
        command: [sh, -c]
        # Values are passed as env vars (below), never interpolated into the shell.
        # The optional bearer token is appended via positional params so a blank
        # value never leaks a stray flag into the curl invocation.
        args:
          - |
            set -eu
            if [ -n "${TOKEN:-}" ]; then
              set -- -H "Authorization: Bearer ${TOKEN}"
            else
              set --
            fi
            curl -sSfL "$@" -o "${DEST_PATH}" "${URL}"
            echo "downloaded ${URL} -> ${DEST_PATH}"
        env:
          - name: URL
            value: "{{inputs.parameters.url}}"
          - name: DEST_PATH
            value: "{{inputs.parameters.dest-path}}"
          - name: TOKEN
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.token-secret-name}}"
                key: "{{inputs.parameters.token-secret-key}}"
                optional: true
