From 0fe4662551c34418dedf5aab996e2e9d712d3c71 Mon Sep 17 00:00:00 2001 From: Hektor Misplon Date: Fri, 13 Sep 2024 14:17:00 +0200 Subject: [PATCH] Update taskwarrior sync setup --- .config/task/.gitignore | 2 +- .config/task/taskrc | 3 +- .config/task/taskrc.d/desktop-arch | 1 + .config/task/taskrc.d/setup.sh | 14 --------- .local/share/task/hooks/on-add.sync | 25 --------------- .local/share/task/hooks/on-exit.sync.py | 32 ++++++++++++++++++++ .local/share/task/hooks/on-modify.sync | 26 ---------------- .local/share/task/scripts/sync-and-notify.sh | 12 ++++++++ 8 files changed, 48 insertions(+), 67 deletions(-) create mode 100644 .config/task/taskrc.d/desktop-arch delete mode 100755 .config/task/taskrc.d/setup.sh delete mode 100755 .local/share/task/hooks/on-add.sync create mode 100755 .local/share/task/hooks/on-exit.sync.py delete mode 100755 .local/share/task/hooks/on-modify.sync create mode 100755 .local/share/task/scripts/sync-and-notify.sh diff --git a/.config/task/.gitignore b/.config/task/.gitignore index bf5455d..1f15efd 100644 --- a/.config/task/.gitignore +++ b/.config/task/.gitignore @@ -1 +1 @@ -taskrc.d/server +taskrc.d/sync diff --git a/.config/task/taskrc b/.config/task/taskrc index 4fc424d..35a4c1f 100644 --- a/.config/task/taskrc +++ b/.config/task/taskrc @@ -1,7 +1,8 @@ data.location=$XDG_DATA_HOME/task/ hooks.location=$XDG_DATA_HOME/task/hooks -include ./taskrc.d/server +include ./taskrc.d/$HOSTNAME +include ./taskrc.d/sync include /usr/share/doc/task/rc/light-256.theme color.active=bold white on black diff --git a/.config/task/taskrc.d/desktop-arch b/.config/task/taskrc.d/desktop-arch new file mode 100644 index 0000000..6bd48d3 --- /dev/null +++ b/.config/task/taskrc.d/desktop-arch @@ -0,0 +1 @@ +recurrence=off diff --git a/.config/task/taskrc.d/setup.sh b/.config/task/taskrc.d/setup.sh deleted file mode 100755 index 7d9a2fb..0000000 --- a/.config/task/taskrc.d/setup.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -# Evaluate environment variables into `server` config file - -source ~/.env - -config_file=~/.config/task/taskrc.d/server - -echo "taskd.certificate=$XDG_DATA_HOME/task/default-client.cert.pem" > $config_file -echo "taskd.key=$XDG_DATA_HOME/task/default-client.key.pem" >> $config_file -echo "taskd.ca=$XDG_DATA_HOME/task/ca.cert.pem" >> $config_file -echo "taskd.trust=ignore hostname" >> $config_file -echo "taskd.server=$TASKD_SERVER:$TASKD_PORT" >> $config_file -echo "taskd.credentials=$TASKD_CREDS" >> $config_file diff --git a/.local/share/task/hooks/on-add.sync b/.local/share/task/hooks/on-add.sync deleted file mode 100755 index ae88474..0000000 --- a/.local/share/task/hooks/on-add.sync +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -# Inspired by https://gist.github.com/primeapple/d3d82fbd28e9134d24819dd72430888e - -read new_task - -source ~/.env - -log_file=./sync.log - -is_up() { - if ! nc -z $TASKD_SERVER $TASKD_PORT; then - echo "Server is down" >> $log_file - exit 1 - fi -} - -is_up -date > $log_file -task rc.verbose:nothing sync >> $log_file & - -echo $new_task -echo 'on-add' - -exit 0 diff --git a/.local/share/task/hooks/on-exit.sync.py b/.local/share/task/hooks/on-exit.sync.py new file mode 100755 index 0000000..807b60c --- /dev/null +++ b/.local/share/task/hooks/on-exit.sync.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +# Source: + +# This hooks script syncs task warrior to the configured task server without blocking. +# The on-exit event is triggered once, after all processing is complete. + +# Make sure hooks are enabled and this hook script is executable. +# Run `task diag` for diagnostics on the hook. + +import json +import subprocess +import sys + +try: + tasks = json.loads(sys.stdin.readline()) +except: + # No input + pass + +# Call the `sync` command +# hooks=0 ensures that the sync command doesn't call the on-exit hook +# verbose=nothing sets the verbosity to print nothing at all + +ps_1 = subprocess.Popen( + ["task", "rc.hooks=0", "sync"], + stdin=subprocess.PIPE, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, +) + +sys.exit(0) diff --git a/.local/share/task/hooks/on-modify.sync b/.local/share/task/hooks/on-modify.sync deleted file mode 100755 index ffe179f..0000000 --- a/.local/share/task/hooks/on-modify.sync +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -# Inspired by https://gist.github.com/primeapple/d3d82fbd28e9134d24819dd72430888e - -read original_task -read modified_task - -source ~/.env - -log_file=./sync.log - -is_up() { - if ! nc -z $TASKD_SERVER $TASKD_PORT; then - echo "Server is down" >> $log_file - exit 1 - fi -} - -is_up -date > $log_file -task rc.verbose:nothing sync >> $log_file & - -echo $modified_task -echo 'on-modify' - -exit 0 diff --git a/.local/share/task/scripts/sync-and-notify.sh b/.local/share/task/scripts/sync-and-notify.sh new file mode 100755 index 0000000..247297c --- /dev/null +++ b/.local/share/task/scripts/sync-and-notify.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# Redirect both stdout and stderr to notify-send as is, but set +# urgency to critical if the command fails +output="$(task rc.hooks=0 sync 2>&1)" +ret=$? +if [ $ret -ne 0 ]; then + urgency=critical +else + urgency=normal +fi +notify-send -u $urgency "Taskwarrior sync" "$output"