replicate.sh: Huge refactor

Doesn't change the behavior of the script, but does centralize logging and make the code look a lot nicer
This commit is contained in:
Salt 2018-09-16 12:13:54 -05:00
parent 69ba42a111
commit 248bd93d39

View File

@ -1,32 +1,71 @@
#!/bin/sh #!/bin/sh
name="$(basename "$0")"
deps="git rsync"
for dep in $deps; do
if ! which $dep > /dev/null 2>&1; then
printf "$0: error: could not find program \"$dep\" in PATH\n" 1>&2
exit 1
fi
done
mkdir Desktop Documents Downloads Games Music Pictures Projects System Videos
tempfolder="/tmp/dots-`whoami`" ## Set up some variables
name="$(basename "$0" .sh)"
deps="git rsync mktemp basename"
logfile="${PWD}/${name}.log"
tempfolder="$(mktemp -d)"
gitdir="$PWD/.dotfiles" gitdir="$PWD/.dotfiles"
homedir="$PWD" homedir="$PWD"
repo='git@gitlab.com:rehashedsalt/home.git' repo='git@gitlab.com:rehashedsalt/home.git'
git clone --recursive --depth 100 --separate-git-dir="$gitdir" "$repo" "$tempfolder" ## Define some functions
log() {
[ -z ${1+x} ] && return 1
printf "${name}: $1\n"
}
validatedep() {
if ! which $1 > /dev/null 2>&1; then
return 1
fi
return 0
}
## Do the do
# Rotate the logfile, if necessary
if [ -f "$logfile" ]; then
log "Found old logfile. Rotating"
mv "$logfile" "$logfile.old"
fi
# Ensure we have the proper dependencies
log "Validating dependencies"
for dep in $deps; do
if ! validatedep "$dep"; then
log "Could not find critical dependency \"$dep\""
exit 1
fi
done
# Build Home folder skeleton
log "Building skeleton folder layout"
mkdir Desktop Documents Downloads Games Music Pictures Projects System Videos
# Clone the dotfiles
log "Cloning repository"
git clone --recursive --depth 100 --separate-git-dir="$gitdir" "$repo" "$tempfolder" >> "$logfile" 2>&1
error="$?" error="$?"
if ! [ "$error" == "0" ]; then if ! [ "$error" -eq "0" ]; then
printf "$name: error: failed cloning repository \"$repo\": $error\n" 1>&2 log "Failed cloning repository \"$repo\": git returned $error"
printf "Do you have the appropriate permissions? Does the remote host know your key?\n" log "See $logfile for details"
log "Do you have the appropriate permissions? Does the remote host know your key?"
exit 2 exit 2
fi fi
rsync -rvl --exclude ".git" $tempfolder/ $homedir/ # Move them to where they should be
rm -r $tempfolder log "Moving cloned repo from \"$tempfolder\" into \"$homedir\""
git --git-dir="$gitdir" --work-tree="$homedir" submodule update --init --recursive --remote rsync -rvl --exclude ".git" "$tempfolder/" "$homedir/" >> "$logfile"
git --git-dir="$gitdir" --work-tree="$homedir" config status.showUntrackedFiles no rm -r "$tempfolder"
printf "$name: dotfiles set up successfully\n" rmdir "$tempfolder"
printf "Be sure and configure the following before issuing any commits:\n" # Finish syncing
printf " git config --global user.email\n" log "Updating submodules and performing basic configuration"
printf " git config --global user.name\n" git --git-dir="$gitdir" --work-tree="$homedir" submodule update --init --recursive --remote >> "$logfile" 2>&1
git --git-dir="$gitdir" --work-tree="$homedir" config status.showUntrackedFiles no >> "$logfile" 2>&1
# Print some post-install instructions
log "Dotfiles set up successfully"
log "Be sure and configure the following before issuing any commits:"
log " git config --global user.email"
log " git config --global user.name"
log "You may now delete the script"
exit 0 exit 0