dotfiles script v0.5.0
- add support for host-specific 'local' files in _local/HOSTNAME/ - works with: linkstatus, link, createfrom, ls
This commit is contained in:
parent
14104fc3ac
commit
154d8ea25e
68
bin/dotfiles
68
bin/dotfiles
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
##
|
||||
## binaryDiv's dotfiles management script
|
||||
## Version 0.4.5
|
||||
## Version 0.5.0
|
||||
##
|
||||
|
||||
usage() {
|
||||
|
|
@ -19,14 +19,18 @@ usage() {
|
|||
|
||||
Dotfile management:
|
||||
ls [DIR] list contents of .dotfiles directory (or DIR relative to it)
|
||||
(-l: list host-specific files, i.e. ~/.dotfiles/_local/HOSTNAME/DIR)
|
||||
edit FILE open ~/.dotfiles/FILE in vim
|
||||
diff FILE use vimdiff to compare ~/FILE to ~/.dotfiles/FILE
|
||||
link FILE... create a symlink from ~/FILE to ~/.dotfiles/FILE (multiple files possible)
|
||||
(-q: be quiet, only print something if an error occurs)
|
||||
(-l: link host-specific files, i.e. ~/FILE to ~/.dotfiles/_local/HOSTNAME/FILE)
|
||||
linkstatus [DIR] list contents of .dotfiles with information about whether there are links to
|
||||
them in ~/, whether they differ, etc...
|
||||
(-l: show linkstatus only for host-specific files, i.e. ~/.dotfiles/_local/HOSTNAME)
|
||||
createfrom FILE... copy ~/FILE to ~/.dotfiles/FILE and symlink the file
|
||||
(-n: only copy, leave the original, do not create symlink)
|
||||
(-l: create host-specific files, i.e. ~/FILE to ~/.dotfiles/_local/HOSTNAME/FILE)
|
||||
|
||||
Synchronization:
|
||||
git ARGS... wrapper for git (all arguments are passed on to git)
|
||||
|
|
@ -45,6 +49,9 @@ fi
|
|||
homedir="$DOTFILESROOT"
|
||||
dotfilesdir="$DOTFILESROOT/.dotfiles"
|
||||
|
||||
dotfiles_local_base="_local"
|
||||
dotfiles_local_prefix="$dotfiles_local_base/$(hostname)"
|
||||
|
||||
|
||||
# Get command
|
||||
[[ $# -gt 0 ]] || { usage; exit 1; }
|
||||
|
|
@ -108,6 +115,7 @@ case "$cmd" in
|
|||
|
||||
ls)
|
||||
lsdir="$dotfilesdir"
|
||||
[[ $1 = "-l" ]] && { lsdir="$lsdir/$dotfiles_local_prefix"; shift; }
|
||||
[[ -n $1 ]] && lsdir="$lsdir/$1"
|
||||
|
||||
echo "$lsdir:"
|
||||
|
|
@ -115,15 +123,20 @@ case "$cmd" in
|
|||
;;
|
||||
|
||||
linkstatus)
|
||||
subdir=""
|
||||
if [[ -n $1 ]]; then
|
||||
subdir="${1%/}"
|
||||
cd "$dotfilesdir"
|
||||
|
||||
subdir="$1"
|
||||
[[ $subdir = "-l" ]] && { subdir="$dotfiles_local_prefix/$1"; shift; }
|
||||
|
||||
if [[ -n $subdir ]]; then
|
||||
subdir="${subdir%/}"
|
||||
[[ $subdir = $dotfiles_local_base ]] && subdir=$dotfiles_local_prefix
|
||||
|
||||
[[ -e $subdir ]] || { echo "$dotfilesdir/$subdir does not exist"; exit 1; }
|
||||
[[ -d $subdir ]] && subdir="$subdir/"
|
||||
fi
|
||||
|
||||
shopt -s nullglob dotglob
|
||||
cd $dotfilesdir
|
||||
dotdirs=()
|
||||
|
||||
for file in $subdir*; do
|
||||
|
|
@ -133,6 +146,10 @@ case "$cmd" in
|
|||
file_home="$homedir/$file"
|
||||
file_dotfiles="$dotfilesdir/$file"
|
||||
|
||||
if [[ $subdir == $dotfiles_local_base/* ]]; then
|
||||
file_home="$homedir/${file#$dotfiles_local_prefix/}"
|
||||
fi
|
||||
|
||||
if [[ -e $file_home ]]; then
|
||||
if [[ "$(realpath "$file_home")" -ef "$file_dotfiles" ]]; then
|
||||
file_status=" LINKED "
|
||||
|
|
@ -144,6 +161,9 @@ case "$cmd" in
|
|||
else
|
||||
file_status="<> DIFFERS "
|
||||
fi
|
||||
elif [[ $file = $dotfiles_local_base ]]; then
|
||||
dotdirs=("$dotfiles_local_prefix" "${dotdirs[@]}")
|
||||
continue
|
||||
else
|
||||
if [[ -d $file_dotfiles ]]; then
|
||||
file_status="++ NEW DIR "
|
||||
|
|
@ -184,10 +204,17 @@ case "$cmd" in
|
|||
;;
|
||||
|
||||
link)
|
||||
if [[ $1 = '-q' ]]; then
|
||||
QUIET=1
|
||||
shift
|
||||
fi
|
||||
while getopts ":ql" opt; do
|
||||
case $opt in
|
||||
q) QUIET=1 ;;
|
||||
l) link_local=1 ;;
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND-1))
|
||||
|
||||
[[ -n $1 ]] || { echo "$(basename $0) $cmd: Missing argument."; exit 1; }
|
||||
|
||||
|
|
@ -195,6 +222,10 @@ case "$cmd" in
|
|||
file_home="$homedir/$filename"
|
||||
file_dotfiles="$dotfilesdir/$filename"
|
||||
|
||||
if [[ $link_local ]]; then
|
||||
file_dotfiles="$dotfilesdir/$dotfiles_local_prefix/$filename"
|
||||
fi
|
||||
|
||||
if [[ ! -e $file_dotfiles ]]; then
|
||||
echo "! $file_dotfiles: target file does not exist"
|
||||
exit 1
|
||||
|
|
@ -226,10 +257,17 @@ case "$cmd" in
|
|||
;;
|
||||
|
||||
createfrom)
|
||||
if [[ $1 = '-n' ]]; then
|
||||
NOLINK=1
|
||||
shift
|
||||
fi
|
||||
while getopts ":nl" opt; do
|
||||
case $opt in
|
||||
n) NOLINK=1 ;;
|
||||
l) create_local=1 ;;
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND-1))
|
||||
|
||||
[[ -n $1 ]] || { echo "$(basename $0) $cmd: Missing argument."; exit 1; }
|
||||
|
||||
|
|
@ -237,6 +275,10 @@ case "$cmd" in
|
|||
file_home="$homedir/$filename"
|
||||
file_dotfiles="$dotfilesdir/$filename"
|
||||
|
||||
if [[ $create_local ]]; then
|
||||
file_dotfiles="$dotfilesdir/$dotfiles_local_prefix/$filename"
|
||||
fi
|
||||
|
||||
if [[ ! -e $file_home ]]; then
|
||||
echo "! $file_home: source file does not exist"
|
||||
exit 1
|
||||
|
|
|
|||
Loading…
Reference in New Issue