Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created May 13, 2022 15:12
Show Gist options
  • Save bpmct/c3ff47ad49d0312ef20049f1efd05c71 to your computer and use it in GitHub Desktop.
Save bpmct/c3ff47ad49d0312ef20049f1efd05c71 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu
shopt -s nullglob
###############################################################################
# #
# This script will run every time your environment is built. #
# A dotfiles repo may be help in managing your tooling and personalization. #
# #
# Learn more about how Coder uses Dotfiles: #
# https://coder.com/docs/workspaces/personalization#dotfiles-repo #
# #
# /!\ This file will be automatically recreated if it is deleted! /!\ #
# #
###############################################################################
##### BEGIN: Dotfiles clone behavior.
function configure_dotfiles () {
# `DOTFILES_REPO` will be automatically provided during your environment's build.
if [[ ! -v DOTFILES_REPO ]]; then
echo "This Coder account has not configured Dotfiles or a personalize script."
echo
echo "All files and applications outside of your home directory will be lost if"
echo "the environment is rebuilt or turned off due to inactivity."
echo
echo "We recommend configuring a Dotfiles repo or personalize script to ensure"
echo "your preferences are applied every time your environment turns on."
echo
echo "Learn more about how Coder uses:"
echo
echo " - Dotfiles: https://coder.com/docs/workspaces/personalization#dotfiles-repo"
echo " - ~/personalize: https://coder.com/docs/workspaces/personalization#personalize"
echo
return 0
fi
echo "Dotfiles repo configured at $DOTFILES_REPO"
if [[ ! -x "$(command -v git)" ]]; then
echo "Git not found, skipping dotfiles configuration..."
return 0
fi
DOTFILES_CLONE_PATH=$HOME/dotfiles
DOTFILES_INSTALL_SCRIPT_PATH=$DOTFILES_CLONE_PATH/install.sh
LAST_DOTFILES_CLONE_URI_PATH=$HOME/.last_dotfiles_clone_uri
# Cloning of the dotfiles repo occurs in a non-interactive script,
# thus unverified hosts must be permitted without user confirmation.
# Temporarily disables remote host key validation to avoid permanently saving an unverified key.
NO_SSH_VALIDATION="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
if [[ -f $LAST_DOTFILES_CLONE_URI_PATH ]]; then
LAST_URI=$(< "$LAST_DOTFILES_CLONE_URI_PATH")
if [[ ("$DOTFILES_REPO" != "$LAST_URI") && -d $DOTFILES_CLONE_PATH ]]; then
DATE_SUFFIX=$(date +"%s")
BACKUP_DOTFILES_CLONE_PATH="${DOTFILES_CLONE_PATH}_backup_${DATE_SUFFIX}"
echo "Dotfiles repo was has changed from $LAST_URI"
echo "Moving $DOTFILES_CLONE_PATH to $BACKUP_DOTFILES_CLONE_PATH"
mv "$DOTFILES_CLONE_PATH" "$BACKUP_DOTFILES_CLONE_PATH"
fi
fi
if [[ ! -d $DOTFILES_CLONE_PATH ]]; then
echo "Cloning repo $DOTFILES_REPO into $DOTFILES_CLONE_PATH"
GIT_SSH_COMMAND=$NO_SSH_VALIDATION git clone "$DOTFILES_REPO" "$DOTFILES_CLONE_PATH"
echo "$DOTFILES_REPO" > "$LAST_DOTFILES_CLONE_URI_PATH"
fi
cd "$DOTFILES_CLONE_PATH"
GIT_SSH_COMMAND=$NO_SSH_VALIDATION git pull --ff-only
echo
if [[ -x "$DOTFILES_INSTALL_SCRIPT_PATH" ]]; then
echo "Executing installation script found in $DOTFILES_INSTALL_SCRIPT_PATH"
"$DOTFILES_INSTALL_SCRIPT_PATH"
else
echo "Installation script $DOTFILES_CLONE_PATH not found or not executable"
echo "Symlinking all files and directories starting with '.' into $HOME"
for dotfile in "$DOTFILES_CLONE_PATH/".*; do
# Skip `..` and '.'
[[ $dotfile =~ \.{1,2}$ ]] && continue
# Skip Git related
[[ $dotfile =~ \.git$ ]] && continue
[[ $dotfile =~ \.gitignore$ ]] && continue
[[ $dotfile =~ \.gitattributes$ ]] && continue
echo "Symlinking $dotfile"
ln -sf "$dotfile" "$HOME"
done
fi
cd "$HOME"
}
configure_dotfiles
##### END: Dotfiles clone behavior.
###############################################################################
# #
# Add shell commands below if this environment has a unique personalization. #
# #
###############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment