-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to apply stow config to the current host
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ansible | ||
bash | ||
code | ||
fontconfig | ||
git | ||
linters | ||
scripts | ||
vim | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#! /usr/bin/env bash | ||
# | ||
# Author: Bert Van Vreckem <[email protected]> | ||
# | ||
#/ Usage: SCRIPTNAME [OPTIONS]... | ||
#/ | ||
#/ Install the dotfiles on the current host using GNU Stow | ||
#/ | ||
#/ OPTIONS | ||
#/ -h, --help | ||
#/ Print this help message | ||
#/ -n, --noop | ||
#/ Print what would be installed without actually executing | ||
#/ | ||
#/ EXAMPLES | ||
#/ | ||
|
||
|
||
#{{{ Bash settings | ||
# abort on nonzero exitstatus | ||
set -o errexit | ||
# abort on unbound variable | ||
set -o nounset | ||
# don't hide errors within pipes | ||
set -o pipefail | ||
#}}} | ||
#{{{ Variables | ||
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces) | ||
script_name=$(basename "${0}") | ||
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
# Debug info ('on' to enable) | ||
readonly debug='on' | ||
|
||
# File containing the directories to install on this host | ||
stow_dirs="$(hostname).dirs" | ||
|
||
# If `yes`, run stow with the --no option | ||
noop=no | ||
|
||
readonly script_name script_dir stow_dirs | ||
#}}} | ||
|
||
main() { | ||
process_args "${@}" | ||
|
||
if ! which stow &> /dev/null; then | ||
error "Command 'stow' not found, install it first!" | ||
exit 1 | ||
fi | ||
|
||
if [ ! -f "${stow_dirs}" ]; then | ||
error "I have no rules for this host." | ||
error "Create file '${stow_dirs}' with a list of directories to install" | ||
exit 1 | ||
fi | ||
|
||
while read -r dir; do | ||
[ "${dir}" = '' ] && break | ||
log "Installing dir: ${dir}" | ||
if [ "${noop}" = 'yes' ]; then | ||
stow --target="${HOME}" --verbose=2 --no "${dir}" | ||
else | ||
stow --target="${HOME}" --verbose=2 "${dir}" | ||
fi | ||
done < "$(hostname).dirs" | ||
} | ||
|
||
#{{{ Helper functions | ||
|
||
# Usage: process_args "${@}" | ||
process_args() { | ||
for arg in "${@}"; do | ||
case "${arg}" in | ||
-h|--help) | ||
usage | ||
exit 0 | ||
;; | ||
-n|--no) | ||
noop=yes | ||
;; | ||
-*) | ||
error "Unknow option: ${arg}" | ||
usage | ||
exit 2 | ||
;; | ||
*) | ||
error "This script does not take arguments" | ||
usage | ||
exit 2 | ||
;; | ||
esac | ||
done | ||
} | ||
|
||
# Print usage message on stdout by parsing start of script comments | ||
# The comment should start with #/ followed by either a newline or a space | ||
usage() { | ||
grep '^#/' "${script_dir}/${script_name}" | sed 's/^#\/\($\| \)//' | ||
} | ||
|
||
# Usage: log [ARG]... | ||
# | ||
# Prints all arguments on the standard output stream | ||
log() { | ||
printf 'ℹ️ \e[0;34m%s\e[0m\n' "${*}" | ||
} | ||
|
||
# Usage: debug [ARG]... | ||
# | ||
# Prints all arguments on the standard output stream, | ||
# if debug output is enabled | ||
debug() { | ||
[ "${debug}" != 'on' ] || printf '🪲 \e[0;36mi%s\e[0m\n' "${*}" | ||
} | ||
|
||
# Usage: error [ARG]... | ||
# | ||
# Prints all arguments on the standard error stream | ||
error() { | ||
printf '🔥 \e[0;31m%s\e[0m\n' "${*}" 1>&2 | ||
} | ||
|
||
#}}} | ||
|
||
main "${@}" | ||
|