adding notes now pushes changes to remote
[bash-notes.git] / notes.sh
index 0e56189..8af4d97 100755 (executable)
--- a/notes.sh
+++ b/notes.sh
@@ -51,6 +51,10 @@ NOTESDIR=${BASEDIR}/notes
 USEGIT=true
 # Address of your remote repository
 GITREMOTE=${GITREMOTE:-""}
+# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
+GITSYNCDELAY=${GITSYNCDELAY:-3600}
+# The name of this client. Defaults to the output of hostname
+GITCLIENT=${GITCLIENT:-""}
 
 } # end set_defaults, do not change this line.
 
@@ -91,7 +95,7 @@ fi
 echo $PID > "$PIDFILE"
 
 # Export config to file
-function export_config() {
+export_config() {
        if [ -r ${RCFILE} ]; then
                echo "Backing up current '${RCFILE}'...."
                mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
@@ -112,7 +116,7 @@ function export_config() {
 
 # we should expand on this function to add a sample note and explain a little bit
 # how the program works.
-function firstrun() {
+firstrun() {
        [ -f $RCFILE ] && RC=$RCFILE || RC="none"
 
        clear
@@ -159,7 +163,7 @@ if [[ ! -d $NOTESDIR ]]; then
        firstrun
 fi
 # check if input is a number, returns false or the number itself
-function check_noteID() {
+check_noteID() {
        IN=$1
        case $IN in
                ''|*[!0-9]*)
@@ -171,7 +175,7 @@ function check_noteID() {
        esac
 }
 
-function helptext() {
+helptext() {
     echo "Usage:"
     echo "  $0 [PARAMS] [note ID]..."
        echo ""
@@ -193,7 +197,7 @@ function helptext() {
     echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
 }
 
-function configtext() {
+configtext() {
     cat << __NOWCONF__ 
 ${BASENAME} configuration is:
 
@@ -212,7 +216,7 @@ __NOWCONF__
 }
 
 # this function returns a random 2 words title
-function random_title() {
+random_title() {
     # Constants 
     X=0
     DICT=/usr/share/dict/words
@@ -233,6 +237,10 @@ function random_title() {
     echo $OUTPUT
 }
 
+# check if GITCLIENT has been set or set it to the output of hostname
+if [ -z $GITCLIENT ]; then
+    GITCLIENT=$( hostname )
+fi
 # returns true if the argument provided directory is a git repository
 is_git_repo() {
     DIR=$1
@@ -248,9 +256,40 @@ is_git_repo() {
 
 # sync local repository to remote
 gitsync() {
-    echo "Syncing notes with git on remote \"$GITREMOTE\""
-    cd $BASEDIR
-    $GIT pull
+    if [[ $USEGIT && -n $GITREMOTE ]]; then
+        NOWSYNC=$(date +%s)
+        # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
+        LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB")
+        [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
+        SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} ))
+        if (( $SYNCDIFF > $GITSYNCDELAY )); then
+            #more than our delay time has passed. We can sync again.
+            $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
+            mv $TMPDB $DB
+            cd $BASEDIR
+            $GIT pull
+        else
+            # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
+            [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
+        fi
+    else
+        # no git, so we just keep going
+        true
+    fi
+}
+
+# add note to git and push it to remote
+gitadd() {
+    if [[ $USEGIT && -n $GITREMOTE ]]; then
+        [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\""
+        cd $BASEDIR
+        $GIT add .
+        $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
+        $GIT push origin master
+    else
+        # no git, so we just keep going
+        true
+    fi
 }
 
 # check for USEGIT and subsequent variables
@@ -262,7 +301,7 @@ if [[ $USEGIT && -n $GITREMOTE ]]; then
         $GIT init
         echo "adding all files to git"
         $GIT add .
-        $GIT commit -m "$(basename $0) - initial commit"
+        $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
         $GIT remote add origin $GITREMOTE
         $GIT push -u origin master
     fi
@@ -271,7 +310,9 @@ elif [[ $USEGIT && -z $GITREMOTE ]]; then
     USEGIT=false
 fi
 
-function addnote() {
+addnote() {
+       # attempt syncing before adding a note
+       gitsync
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
                rm $TMPDB
@@ -294,8 +335,10 @@ function addnote() {
        # alacritty --class notes --title notes -e /usr/bin/vim ...
        # shellcheck disable=SC2086,SC2091
        $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
+       # add note to git and push to remote
+       gitadd
 }
-function backup_data() {
+backup_data() {
        BACKUPDIR="$1"
     echo "backing up data in $BACKUPDIR"
 
@@ -336,7 +379,7 @@ function backup_data() {
        fi
 }
 
-function backup_restore() {
+backup_restore() {
        BACKUPDIR="$1"
        echo "restoring backup from $BACKUPDIR"
        echo "This will overwrite all your notes and configurations with the backup."
@@ -383,7 +426,7 @@ function backup_restore() {
        esac
 }
 
-function editnote() {
+editnote() {
        NOTE=$1
        # shellcheck disable=SC2155
        local OK=$(check_noteID "$NOTE")
@@ -406,7 +449,9 @@ function editnote() {
                 exit 1
        fi
 }
-function listnotes() {
+listnotes() {
+       # attempt syncing before listing all notes
+       gitsync
        # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
        if [[ $(ls -A "$NOTESDIR") ]]; then
                if [ $PLAIN == false ]; then
@@ -428,7 +473,7 @@ function listnotes() {
                echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
        fi
 }
-function rmnote() {
+rmnote() {
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
                rm $TMPDB
@@ -483,7 +528,7 @@ function rmnote() {
                fi
        fi
 }
-function shownote() {
+shownote() {
        NOTE=$1
 
        # shellcheck disable=SC2155