link-from-dotfiles.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. # absolute path to your git folder
  3. dotDir="${HOME}/.dotfiles"
  4. display_help(){
  5. echo "Usage: ${0##*/} [flag]"
  6. echo "Check if '${dotDir}' is yours dotfiles directory path"
  7. echo "No flags == DRY RUN"
  8. echo "-d, --dry-run == link files"
  9. echo "-l, --link == link files"
  10. echo "-F, --force == link files with overwrite"
  11. exit 2
  12. }
  13. create_dirs(){
  14. file="$1"
  15. new="${file//${dotDir}/${HOME}}"
  16. newDir="${new%/*}"
  17. mkdir -p "$newDir"
  18. }
  19. dry_run(){
  20. file="$1"
  21. symlink="${file//${dotDir}/${HOME}}"
  22. [[ ! -f $symlink ]] && echo "DRY RUN: Linking ${file} --> ${symlink}"
  23. }
  24. link_file(){
  25. file="$1"
  26. create_dirs "$file"
  27. ln -sv "$file" "$new" 2>/dev/null
  28. }
  29. link_force(){
  30. file="$1"
  31. create_dirs "$file"
  32. ln -sfv "$file" "$new"
  33. }
  34. [[ ! -d $dotDir || $# -gt 1 ]] && display_help
  35. for file in $(find "$dotDir" -type f -not -path "*\.git*"); do
  36. case "$1" in
  37. -d|--dry-run) dry_run "$file";;
  38. -l|--link) link_file "$file";;
  39. -f|--force) link_force "$file";;
  40. -h|--help) display_help;;
  41. *) display_help;;
  42. esac
  43. done