dotfiles

My personal dotfiles

config.fish (2572B)

      1 function config -d "A simple configuration manager"
      2   # Calls git in the appropriate repository
      3   function _call_git
      4     git --git-dir="$HOME/Documents/dotfiles" --work-tree="$HOME" $argv
      5   end
      6 
      7   argparse u/update=+ a-add=+ r-rm=+ l/list g-log s/status d/diff f-edit-self e/edit -- $argv
      8 
      9   # List the dotfiles specified in configurations list
     10   if test -n "$_flag_list"
     11     string replace --all "$HOME" '~' (cat ~/.local/share/configs.list)
     12     return
     13   end
     14 
     15   # Call git log
     16   if test -n "$_flag_log"
     17     _call_git log
     18     return
     19   end
     20 
     21   # Show the configuration files that have been modified
     22   if test -n "$_flag_status"
     23     _call_git status >> /tmp/config-git-status
     24 
     25     echo 'modified files:'
     26     cat /tmp/config-git-status \
     27       | grep 'modified' \
     28       | awk '{ print $2 }' \
     29       | xargs -r realpath \
     30       | string replace "$HOME" '~' \
     31       | xargs -r -n 1 printf '    %s\n'
     32 
     33     echo 'deleted files:'
     34     cat /tmp/config-git-status \
     35       | grep 'deleted' \
     36       | awk '{ print $2 }' \
     37       | xargs -r realpath \
     38       | string replace "$HOME" '~' \
     39       | xargs -r -n 1 printf '    %s\n'
     40 
     41     rm /tmp/config-git-status
     42     return
     43   end
     44 
     45   # Call git diff
     46   if test -n "$_flag_diff"
     47     _call_git diff
     48     return
     49   end
     50 
     51   if test -n "$_flag_edit_self"
     52     $EDITOR (type -p config)
     53   end
     54 
     55   # Add files to the list of dotfiles
     56   for item in $_flag_add
     57     echo "Adding '$item' to the list of dotfiles"
     58     echo (realpath "$item") >> ~/.local/share/configs.list
     59   end
     60 
     61   # Remove files from the list of dotfiles
     62   for item in $_flag_rm
     63     echo "Removing '$item' from the list of dotfile"
     64 
     65     # Set is used because 'item' may be a directory
     66     set pattern (realpath "$item")
     67     set pattern (string replace --all '/' '\/' "$pattern")
     68     set pattern (string replace --all '.' '\.' "$pattern")
     69     sed -e "/^$pattern/d" -i ~/.local/share/configs.list
     70     _call_git rm "$item" --cached -r
     71   end
     72 
     73   # Update the repository and it's remotes
     74   if test -n "$_flag_update"
     75     # Add the relevant files to the repo
     76     cat ~/.local/share/configs.list \
     77       | xargs git --git-dir="$HOME/Documents/dotfiles" --work-tree="$HOME" add
     78 
     79     for msg in $_flag_update
     80       printf '%s\n\n' "$msg" >> /tmp/config-git-message
     81     end
     82 
     83     # Commit the changes
     84     and _call_git commit -F /tmp/config-git-message
     85 
     86     # Return if there's an error
     87     or return $status
     88 
     89     # Push the changes to all remotes
     90     for remote in (_call_git remote -v | awk '{ print $1 }' | uniq)
     91       _call_git push "$remote" master
     92     end
     93 
     94     rm /tmp/config-git-message
     95   end
     96 end
     97