Skip to content

Instantly share code, notes, and snippets.

@mulle-nat
Created November 15, 2018 10:33
Show Gist options
  • Save mulle-nat/b24bab7bbe8eda235f36fb3563a03a5b to your computer and use it in GitHub Desktop.
Save mulle-nat/b24bab7bbe8eda235f36fb3563a03a5b to your computer and use it in GitHub Desktop.
Example task that synchronizes a directory using git
#! /usr/bin/env bash
r_evaluator()
{
local cmd="$1"
local statefile="$2"
redirect_exekutor "${statefile}" echo "${cmd}" || exit 1
case "${cmd}" in
"stash pull")
RVAL="pull"
exekutor git stash save --quiet
;;
"pull")
RVAL="stash pop"
exekutor git pull -f --quiet
;;
"stash pop")
RVAL="add"
exekutor git stash pop --quiet
;;
"add")
RVAL="commit"
exekutor git add --all
;;
"commit")
RVAL="push"
exekutor git commit -m "* automated push from \
${MULLE_HOSTNAME} with mulle-monitor" --quiet
;;
"push")
RVAL=""
exekutor git push -f --quiet
;;
*)
remove_file_if_present "${statefile}"
fail "Unknown command \"${cmd}\""
;;
esac
}
commit_and_push_task_run()
{
log_entry "commit_and_push_task_run" "$@"
local statefile
[ -z "${MULLE_MONITOR_VAR_DIR}" ] && internal_fail "MULLE_MONITOR_VAR_DIR is undefined"
statefile="${MULLE_MONITOR_VAR_DIR}/commit-and-push"
#
# Try not to lose anything...
#
# We keep an internal state and restart from there
# assuming it got interrupted there.
#
#
local cmd
local lenient
lenient='NO'
if [ -f "${statefile}" ]
then
cmd="`cat "${statefile}"`"
lenient='YES' # first command might have gone through
fi
cmd="${cmd:-stash pull}"
r_mkdir_parent_if_missing "${statefile}" || exit 1
while [ ! -z "${cmd}" ]
do
if ! r_evaluator "${cmd}" "${statefile}" "${lenient}"
then
[ "${lenient}" = 'NO' ] && exit 1
fi
cmd="${RVAL}"
lenient='NO'
done
remove_file_if_present "${statefile}"
}
if [ -z "${MULLE_MONITOR_TASK_LOAD}" ]
then
if [ -z "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}" ]
then
MULLE_BASHFUNCTIONS_LIBEXEC_DIR="`mulle-bashfunctions-env libexec-dir 2> /dev/null`"
[ -z "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}" ] && \
echo "mulle-bashfunctions are not installed" >&2 && \
exit 1
. "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}/mulle-bashfunctions.sh" || exit 1
fi
[ "${TRACE}" = 'YES' ] && set -x && : "$0" "$@"
commit_and_push_task_run
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment