install.fairie/home/dot_local/bin/post-installx/executable_post-docker-desktop.sh

41 lines
1.8 KiB
Bash
Raw Normal View History

2024-05-03 19:40:44 -07:00
#!/usr/bin/env bash
# @file DockerHub Login
# @brief Logs into DockerHub for Docker Desktop
# @description
# This script logs into DockerHub so that Docker Desktop is pre-authenticated. This
# functionality requires that the `DOCKERHUB_USER` be passed in as an environment variable (or
# directly editted in the `~/.config/chezmoi/chezmoi.yaml` file) and that the `DOCKERHUB_TOKEN`
# be passed in as a secret (either via the encrypted secret method or passed in as an environment
# variable).
2024-05-27 20:45:14 -07:00
set -Eeuo pipefail
2024-05-27 20:50:11 -07:00
trap "gum log -sl error 'Script encountered an error!'" ERR
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
if command -v docker > /dev/null; then
2024-05-09 19:53:38 -07:00
### Acquire DOCKERHUB_USER
if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml" ]; then
DOCKERHUB_USER="$(yq '.data.user.docker.username' ~/.config/chezmoi/chezmoi.yaml)"
else
2024-05-27 20:50:11 -07:00
gum log -sl error "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml is missing which is required for populating the DOCKERHUB_USER"
2024-05-27 04:15:03 -07:00
exit 1
2024-05-09 19:53:38 -07:00
fi
### Launch Docker.app
2024-05-03 19:40:44 -07:00
if [ -d "/Applications/Docker.app" ] || [ -d "$HOME/Applications/Docker.app" ]; then
2024-05-27 23:55:42 -07:00
gum log -sl info 'Ensuring Docker.app is running' && open --background -a Docker --args --accept-license --unattended
2024-05-03 19:40:44 -07:00
fi
2024-05-09 19:53:38 -07:00
2024-05-27 20:45:14 -07:00
### Pre-authenticate with DockerHub
if get-secret --exists DOCKERHUB_TOKEN; then
if [ "$DOCKERHUB_USER" != 'null' ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Headlessly authenticating with DockerHub registry'
2024-05-27 20:45:14 -07:00
echo "$(get-secret DOCKERHUB_TOKEN)" | docker login -u "$DOCKERHUB_USER" --password-stdin > /dev/null
2024-05-27 23:55:42 -07:00
gum log -sl info 'Successfully authenticated with DockerHub registry'
2024-05-27 20:45:14 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Skipping logging into DockerHub because DOCKERHUB_USER is undefined'
2024-05-27 20:45:14 -07:00
fi
2024-05-09 19:53:38 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Skipping logging into DockerHub because DOCKERHUB_TOKEN is undefined'
2024-05-09 19:53:38 -07:00
fi
2024-05-03 19:40:44 -07:00
fi