2023-07-09 01:11:02 -07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# @file .local/bin/gitomatic-service
|
|
|
|
# @brief Helper script to run gitomatic to monitor git repositories
|
|
|
|
# @description
|
|
|
|
# This script is executed by gitomatic systemd service. `gitomatic` does not support monitoring multiple
|
|
|
|
# repositories in a single process. This script starts as many gitomatic processes as there are repositories.
|
|
|
|
#
|
|
|
|
# This feature allows you to specify git repositories and corresponding paths to keep in-sync, using both git
|
|
|
|
# push and pull.
|
|
|
|
#
|
|
|
|
# ## Links
|
|
|
|
#
|
|
|
|
# [Systemd Unit file](https://github.com/megabyte-labs/install.doctor/blob/master/home/dot_config/gitomatic/gitomatic.service.tmpl)
|
|
|
|
|
2023-07-12 19:58:08 -07:00
|
|
|
if command -v gitomatic > /dev/null && command -v jq > /dev/null && command -v yq > /dev/null && command -v git > /dev/null; then
|
|
|
|
if [ -d /Applications ] && [ -d /System ]; then
|
|
|
|
USER_FOLDER="/Users"
|
|
|
|
else
|
|
|
|
USER_FOLDER="/home"
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Cycle through user folder chezmoi.yaml configurations
|
|
|
|
while read FOLDER; do
|
|
|
|
USER_FROM_FOLDER="$(echo "$FOLDER" | sed 's/\/\(.*\)$/\1/')"
|
|
|
|
CHEZMOI_YAML="$USER_FOLDER/$USER_FROM_FOLDER/.config/chezmoi/chezmoi.yaml"
|
|
|
|
if [ -f "$CHEZMOI_YAML" ]; then
|
|
|
|
for IM in $(yq eval -o=j "$CHEZMOI_YAML" | jq -cr '.data.user.gitomatic[]'); do
|
|
|
|
GIT="$(echo "$IM" | jq -r '.git' -)"
|
|
|
|
GIT_PATH="$(echo "$IM" | jq -r '.path' -)"
|
|
|
|
if [ ! -d "$GIT_PATH" ]; then
|
|
|
|
sudo su - "$USER_FROM_FOLDER" -c 'git clone "$GIT" "$GIT_PATH"'
|
|
|
|
fi
|
|
|
|
cd "$GIT_PATH"
|
|
|
|
sudo su - "$USER_FROM_FOLDER" -c 'gitomatic -email "$(git config user.email)" "$GIT_PATH" &'
|
|
|
|
done
|
2023-07-09 01:11:02 -07:00
|
|
|
fi
|
2023-07-12 19:58:08 -07:00
|
|
|
done < <(find "$USER_FOLDER" -mindepth 1 -maxdepth 1 -type d)
|
|
|
|
|
|
|
|
### Handle root user config
|
2023-11-27 00:32:46 -08:00
|
|
|
if [ -f /root/.config/chezmoi/chezmoi.yaml ]; then
|
2023-07-12 19:58:08 -07:00
|
|
|
for IM in $(yq eval -o=j /root/.config/chezmoi.chezmoi.yaml | jq -cr '.data.user.gitomatic[]'); do
|
|
|
|
GIT="$(echo "$IM" | jq -r '.git' -)"
|
|
|
|
GIT_PATH="$(echo "$IM" | jq -r '.path' -)"
|
|
|
|
if [ ! -d "$GIT_PATH" ]; then
|
|
|
|
git clone "$GIT" "$GIT_PATH"
|
|
|
|
fi
|
|
|
|
cd "$GIT_PATH"
|
|
|
|
gitomatic -email "$(git config user.email)" "$GIT_PATH" &
|
|
|
|
done
|
|
|
|
fi
|
2023-07-09 01:11:02 -07:00
|
|
|
else
|
2023-12-05 10:07:50 -08:00
|
|
|
echo 'gitomatic, jq, yq, and git should be installed!' && exit 1
|
2023-07-09 01:11:02 -07:00
|
|
|
fi
|