dotfiles script v0.2.0
- add help text - add 'install' command - add 'diff' command - add 'link' command - add 'pull' command (alias for 'git pull') - remove 'createlinks' command
This commit is contained in:
parent
ed1031d0f2
commit
801cabcba4
123
bin/dotfiles
123
bin/dotfiles
|
|
@ -2,8 +2,33 @@
|
|||
|
||||
##
|
||||
## binaryDiv's dotfiles management script
|
||||
## Version 0.2.0
|
||||
##
|
||||
|
||||
usage() {
|
||||
cat <<- END_OF_HELPTEXT
|
||||
Usage: $(basename $0) COMMAND [...]
|
||||
Manage dotfiles via git.
|
||||
|
||||
General commands:
|
||||
help print this help message
|
||||
|
||||
Installation:
|
||||
install run this on first start: install dotfiles script in ~/bin, configure git, ...
|
||||
(this will NOT create links for all dotfiles, though)
|
||||
|
||||
Dotfile management:
|
||||
ls [DIR] list contents of .dotfiles directory (or DIR relative to it)
|
||||
diff FILE use vimdiff to compare ~/FILE to ~/.dotfiles/FILE
|
||||
link FILE... create a symlink from ~/FILE to ~/.dotfiles/FILE (multiple files possible)
|
||||
|
||||
Synchronization:
|
||||
git ARGS... wrapper for git (all arguments are passed on to git)
|
||||
pull shortcut for git pull
|
||||
END_OF_HELPTEXT
|
||||
}
|
||||
|
||||
|
||||
# Define root dir for dotfiles management (defaults to $HOME, can be overridden
|
||||
# with environment variables for debugging)
|
||||
if [ -z "$DOTFILESROOT" ]; then
|
||||
|
|
@ -15,53 +40,93 @@ homedir="$DOTFILESROOT"
|
|||
dotfilesdir="$DOTFILESROOT/.dotfiles"
|
||||
|
||||
|
||||
# Check for command
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "no command given"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Fetch command
|
||||
# Get command
|
||||
[ $# -gt 0 ] || { usage; exit 1; }
|
||||
cmd=$1
|
||||
shift
|
||||
|
||||
|
||||
# Parse command
|
||||
case "$cmd" in
|
||||
help)
|
||||
# help: print usage help
|
||||
echo "help: not implemented yet, sorry /o\\"
|
||||
help|--help|-h)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
|
||||
install)
|
||||
bin_source="$dotfilesdir/bin/dotfiles"
|
||||
bin_target="$homedir/bin/dotfiles"
|
||||
bin_dir="$homedir/bin"
|
||||
|
||||
if [ ! -e "$bin_target" ]; then
|
||||
echo "* Installing dotfiles script in $bin_dir/..."
|
||||
mkdir -pv "$bin_dir"
|
||||
ln -sr "$bin_source" "$bin_target" || exit 1
|
||||
elif [ ! "$(realpath $bin_target)" -ef "$bin_source" ]; then
|
||||
echo "! Warning: File $bin_target already exists but is NOT a link to $bin_source"
|
||||
fi
|
||||
|
||||
if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
|
||||
echo "! Warning: $bin_dir is not in your PATH."
|
||||
fi
|
||||
|
||||
echo "* Configuring git settings..."
|
||||
git -C "$dotfilesdir" config merge.ff only
|
||||
;;
|
||||
|
||||
ls)
|
||||
# ls: list dotfiles via ls
|
||||
echo "$dotfilesdir:"
|
||||
ls -lAh --color=auto "$dotfilesdir/"
|
||||
lsdir="$dotfilesdir"
|
||||
[ -n "$1" ] && lsdir="$lsdir/$1"
|
||||
|
||||
echo "$lsdir:"
|
||||
ls -lAh --color=auto "$lsdir"
|
||||
;;
|
||||
|
||||
git)
|
||||
# git: do git commands (forwards all arguments)
|
||||
git -C "$dotfilesdir" "$@"
|
||||
;;
|
||||
# TODO: Command 'status' or 'linkstatus' to show which files are linked and which are not
|
||||
|
||||
createlinks)
|
||||
# createlinks: create or update links for dotfiles in home dir
|
||||
diff)
|
||||
[ -n "$1" ] || { echo "$(basename $0) $cmd: Missing argument."; exit 1; }
|
||||
file_A="$homedir/$1"
|
||||
file_B="$dotfilesdir/$1"
|
||||
|
||||
# (Use an extra script for this to separate the management script from
|
||||
# user specific dotfiles)
|
||||
if [ -f "$dotfilesdir/createlinks.sh" ]; then
|
||||
# arguments: home dir and .dotfiles dir
|
||||
bash "$dotfilesdir/createlinks.sh" "$homedir" "$dotfilesdir"
|
||||
if [ "$(realpath "$file_A")" -ef "$file_B" ]; then
|
||||
echo "$file_A is a link to $file_B"
|
||||
else
|
||||
echo "no createlinks.sh found in $dotfilesdir"
|
||||
exit 1
|
||||
vimdiff "$file_A" "$file_B"
|
||||
fi
|
||||
;;
|
||||
|
||||
link)
|
||||
[ -n "$1" ] || { echo "$(basename $0) $cmd: Missing argument."; exit 1; }
|
||||
|
||||
for filename in "$@"; do
|
||||
file_home="$homedir/$filename"
|
||||
file_dotfiles="$dotfilesdir/$filename"
|
||||
|
||||
if [ ! -e "$file_dotfiles" ]; then
|
||||
echo "! $file_dotfiles: target file does not exist"
|
||||
exit 1
|
||||
elif [ -e "$file_home" ]; then
|
||||
if [ ! "$(realpath "$file_home")" -ef "$file_dotfiles" ]; then
|
||||
echo "! $file_home already exists but is NOT a link to $file_dotfiles, please fix manually"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "* Creating link $file_home -> $file_dotfiles"
|
||||
ln -sr "$file_dotfiles" "$file_home" || exit 1
|
||||
fi
|
||||
done
|
||||
;;
|
||||
|
||||
git)
|
||||
git -C "$dotfilesdir" "$@"
|
||||
;;
|
||||
|
||||
pull)
|
||||
git -C "$dotfilesdir" pull "$@"
|
||||
;;
|
||||
|
||||
*)
|
||||
# unknown command
|
||||
echo "unknown command '$cmd'"
|
||||
echo "Unknown command '$cmd', use 'help' to show all commands."
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
if [ $# -ne 2 ]; then
|
||||
echo "wrong argument count"
|
||||
fi
|
||||
|
||||
# Set variables
|
||||
homedir="$1"
|
||||
dotfilesdir="$2"
|
||||
|
||||
# Change directory to home dir
|
||||
cd "$homedir"
|
||||
|
||||
# Define a function that create a link unless it already exists
|
||||
makelink() {
|
||||
test $# -eq 2 || { echo "makelink: invalid function call"; exit 1; }
|
||||
|
||||
if [ ! -e "$1" ]; then
|
||||
echo "can't create link '$2': target '$1' does not exist"
|
||||
return
|
||||
fi
|
||||
|
||||
if [ ! -e "$2" ]; then
|
||||
# Create link
|
||||
ln -sr "$1" "$2"
|
||||
elif [ ! -L "$2" ]; then
|
||||
# A file with the same name exists and it's not a link
|
||||
# -> warn user!
|
||||
echo "can't create link '$2': file already exists"
|
||||
fi
|
||||
}
|
||||
|
||||
makelink_simple() {
|
||||
test $# -eq 1 || { echo "makelink_simple: invalid function call"; exit 1; }
|
||||
|
||||
makelink "$dotfilesdir/$1" "$1"
|
||||
}
|
||||
|
||||
# -- CREATE THE LINKS HERE --
|
||||
|
||||
# ~/bin
|
||||
makelink "$dotfilesdir/bin/" bin
|
||||
|
||||
# bash, profile, etc.
|
||||
makelink_simple .profile
|
||||
makelink_simple .bashrc
|
||||
makelink_simple .bash_profile
|
||||
makelink_simple .bash_logout
|
||||
|
||||
# vim
|
||||
makelink_simple .vimrc
|
||||
makelink_simple .gvimrc
|
||||
|
||||
# misc apps
|
||||
makelink_simple .bcrc
|
||||
makelink_simple .toprc
|
||||
|
||||
# X stuff
|
||||
makelink_simple .Xdefaults
|
||||
makelink_simple .xprofile
|
||||
|
||||
# TODO more links...
|
||||
|
||||
Loading…
Reference in New Issue