Skip to content

Instantly share code, notes, and snippets.

@pkit
Created December 10, 2023 01:55
Show Gist options
  • Save pkit/a98411d21ecc9293066f4579088187d1 to your computer and use it in GitHub Desktop.
Save pkit/a98411d21ecc9293066f4579088187d1 to your computer and use it in GitHub Desktop.
ghcr.io credential helper
#!/bin/bash
set -e
cmd="$1"
if [ "erase" = "$cmd" ]; then
cat - >/dev/null
exit 0
fi
if [ "store" = "$cmd" ]; then
cat - >/dev/null
exit 0
fi
if [ "get" != "$cmd" ]; then
exit 1
fi
host="$(cat -)"
host="${host#https://}"
host="${host%/}"
if [ "$host" != "ghcr.io" ] && [ "$host" != "docker.pkg.github.com" ]; then
exit 1
fi
token="$(gh config get -h github.com oauth_token)"
if [ -z "$token" ]; then
exit 1
fi
printf '{"Username":"%s", "Secret":"%s"}\n' "$(gh api user -q '.login')" "$token"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment