Skip to content

Simple scripts that help you have better preparation for earthquakes.

License

Notifications You must be signed in to change notification settings

imbaggaarm/zshrc-auto-sync

Repository files navigation

ZSH Run Configuration Auto Sync

This repository will help you to utilize fswatch and launchd in order to get your ZSH run configuration (.zshrc) synced with the git remote repository automatically.

If you want to deeply understand how it works, you should read this article: Medium - Organize and Auto Back Up Your ZSH Run Configuration

Quick Start

Follow these steps to automatically sync your .zshrc with the remote repository.

  1. Install required tools

    fswatch: For detecting file changes, install it using Homebrew

    brew install fswatch
    
  2. Create git repository

    mkdir ~/code && mkdir ~/code/zsh-backup && cd ~/code/zsh-backup
    git init
    gh repo create

    Notes: If you haven't installed github command-line tool yet, please use:

    brew install gh

    Remember to set the repo's visibility to Private.

  3. Copy necessary files

    1. You should clone this repository and copy 3 files into zsh-backup folder:
    2. Make scripts executable:
      chmod +x commit_changes.sh && chmod +x sync_zshrc.sh
    3. Modify fswatch absolute path:
      • Use which fswatch to get the absolute path of fswatch and replace /opt/homebrew/bin/fswatch in sync_zshrc.sh with the result.
  4. Verify the scripts

    1. Navigate the zsh-backup directory
      cd ~/code/zsh-backup
    2. Run sync_zshrc.sh
      ./sync_zshrc.sh
    3. Change the content of ~/.zshrc or ~/.zsh/ and check whether new changes are committed and pushed to the remote repository.
  5. Modify launchd property list file (com.[username].auto-sync-zshrc.plist)

    1. Replace imbaggaarm with the username in your local machine.

    2. Replace /Users/eastagile/ to the home directory of your logged in user.

      Notes: For StandardOutPath and StandardErrorPath in the .plist, you can use whatever you want, but please remember to make sure that the paths are existed (create if they are not) and your user has enough permissions to modify the files.

  6. Make sync_zshrc run as a background service

    1. Copy com.<username>.auto-sync-zshrc.plist to ~/Library/LaunchAgents/:
      cp com.<username>.auto-sync-zshrc.plist ~/Library/LaunchAgents/
    2. Navigate to ~/Library/LaunchAgents/ and start the service:
      launchctl load com.<username>.auto-sync-zshrc.plist
      • If you get Load failed: 5: Input/output error, please check if the .plist file is in the correct format and you are in the right directory.
    3. You can check files at StandardErrorPath and StandardOutPath to debug if there is anything wrong. In that case, I suggest you use LaunchControl for easier debugging. You can install it using Homebrew Cask:
      brew install --cask launchcontrol
    4. If you open the LaunchControl application and see all fields are green in com.<username>.auto-sync-zshrc.plist then congratulations, now all of your ZSH run configurations are synced automatically with the remote repository. Example: image Notes: Remember to select UserAgents in the top left button of the LaunchControl application.
  7. Verify that our scripts are working properly

    • Change ~/.zshrc or ~/.zsh/ and verify that our changes are committed and pushed automatically to the remote repository.

Tokens, Credentials, Secrets

  • You should store your private tokens, secrets in other files, such as secrets.sh, and load them by adding source secrets.sh into .zshrc file. Even though our repository is private, but you might accidentally publish it or in case Github is hacked, your credentials won't be leaked.

About

Simple scripts that help you have better preparation for earthquakes.

Resources

License

Stars

Watchers

Forks

Languages