#!/bin/sh
# Runs the GitHub CLI with the token git would get. gh does not ask git's credential helpers for
# one, so this stands ahead of the real gh on PATH and passes the token in GH_TOKEN.
#
# With no token it still runs gh, which then says that it is not signed in.
set -u

cma_bin=${CMA_BIN:-/opt/cma/bin}
token=$(printf 'protocol=https\nhost=github.com\n\n' | "$cma_bin/git-credential-cma" get | sed -n 's/^password=//p')
[ -z "$token" ] || export GH_TOKEN="$token"
exec "$cma_bin/gh" "$@"
