#!/usr/bin/env bash # # refresh-dotfiles # # Pull the latest configuration from Git and process ERB templates with Puppet # # Process files relative to this script. # This needs to be an absolute path for use in the generated Puppet manifest. BASEDIR="$(cd "$(dirname "$0")/.." && echo "$PWD")" # Just download and extract compiled repository in non-git-based directories if [[ ! -d "${BASEDIR}/.git" ]]; then curl -s https://gitlab.james.tl/james/dotfiles/-/archive/preprocessed/dotfiles-preprocessed.tar.gz | tar -C "$BASEDIR" -xvzf - --strip 1 exit fi # Create place to store generated Puppet manifest MANIFEST="$(mktemp)" trap "rm -f '$MANIFEST'" EXIT INT TERM # Puppet on Windows is not a Cygwin app--it needs Windows paths if [[ $OSTYPE == 'cygwin' ]]; then BASEDIR="$(cygpath -am "$BASEDIR")" MANIFEST="$(cygpath -am "$MANIFEST")" PUPPET='puppet.bat' else PUPPET='puppet' fi # A place to keep track of what this script manages DOT_TEMPLATES="${BASEDIR}/.templates" # # Read and parse command line options # usage() { cat >&2 < '${mode}', content => template('${BASEDIR}/${template}'), } file_line { '${template%.erb}': path => '${DOT_TEMPLATES}', line => '${template%.erb}', require => File['${BASEDIR}/${template%.erb}'], } END done # Remove dotfiles that we've managed in the past that are not in git # (processed through 'tr' because file_line generates Windows line # endings on Windows which 'read' doesn't strip off) < "$DOT_TEMPLATES" tr -d '\r' | while read dotfile; do if [[ ! -f "${BASEDIR}/${dotfile}.erb" ]]; then cat < absent, } file_line { '${dotfile}': ensure => absent, path => '${DOT_TEMPLATES}', line => '${dotfile}', require => File['${BASEDIR}/${dotfile}'], } END fi done } # # Main program # cd "$BASEDIR" touch "$DOT_TEMPLATES" branch="$(git rev-parse --abbrev-ref HEAD)" # Grab latest updates from git if [[ -z $no_fetch ]]; then git fetch git reset --hard "origin/${branch}" fi # Process templates with Puppet if we're on the main branch or a clone if [[ $branch == 'main' || $PWD != $HOME ]]; then generate_manifest > "$MANIFEST" cat >> "$MANIFEST" < { mode => '0600', } END_SPECIAL "$PUPPET" apply --modulepath="${BASEDIR}/lib/puppet" "$MANIFEST" fi