# pass_sync
# ---------
# Sincroniza el password store con el repositorio git remoto (pull + push).
# Sale con exit code 1 si la sincronizacion falla.
#
# USO (sourced):
#   source pass_sync.sh
#   pass_sync

pass_sync() {
    local pull_out
    pull_out=$(pass git pull 2>&1)
    if [ $? -ne 0 ]; then
        echo "pass_sync: fallo en git pull: $pull_out" >&2
        return 1
    fi

    local push_out
    push_out=$(pass git push 2>&1)
    if [ $? -ne 0 ]; then
        echo "pass_sync: fallo en git push: $push_out" >&2
        return 1
    fi

    printf '{"pull":"%s","push":"%s"}' \
        "$(echo "$pull_out" | tail -1 | sed 's/"/\\"/g')" \
        "$(echo "$push_out" | tail -1 | sed 's/"/\\"/g')"
}
