#!/usr/bin/env bash
#
# A simplified docker login approach that doesn't depends on the docker binary
#
# Usage: ./docker-login <username> <password> [registry]
set -euo pipefail

auth=$1
registry=${2:-docker.io}

# Encode some funky docker heuristic
if [[ $registry = *docker.io ]]; then
  # use the v2 registry so that skopeo can do noop layer copies
  registry=https://index.docker.io/v2/
fi

mkdir -p ~/.docker

cat <<DOCKER_CONF > ~/.docker/config.json
{
  "auths": {
    "$registry": {
      "auth": "$(echo -n "$auth" | base64)"
    }
  }
}
DOCKER_CONF
