]> gitweb.fluxo.info Git - scripts.git/commitdiff
Fix: status: add function status_run_if_file_older_than
authorSilvio Rhatto <rhatto@riseup.net>
Sun, 4 Aug 2024 03:24:41 +0000 (00:24 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sun, 4 Aug 2024 03:24:41 +0000 (00:24 -0300)
status

diff --git a/status b/status
index b357b4efd09efed2b426f76ad5d37c1010818c53..51f9874bda6906d27a5f757e919d9bb4463f3f32 100755 (executable)
--- a/status
+++ b/status
@@ -72,6 +72,33 @@ function status_file_changed_delta {
   echo $(( timestamp - file_timestamp ))
 }
 
+# Run command if a file is older than a given number of seconds
+function status_run_if_file_older_than {
+  local file="$1"
+  local interval="$2"
+  local run="0"
+
+  shift 2
+
+  # Run command if there's still no file to test
+  if [ ! -e "$file" ]; then
+    run="1"
+  else
+    local age="`status_file_changed_delta $file`"
+    local interval="3600"
+
+    # Run command only when file is older or equal the interval
+    if (( $age >= $interval )); then
+      run="1"
+    fi
+  fi
+
+  # Run if criteria matches
+  if [ "$run" == "1" ]; then
+    $*
+  fi
+}
+
 # Run status
 function status_run {
   if [ ! -z "$PROJECT" ]; then
@@ -92,24 +119,8 @@ function status_run {
     # Update your mrconfig and check all registered repositories
     if which mr &> /dev/null; then
       local mrconfig="$HOME/.custom/mrconfig-automatic"
-      local mrconfig_run="0"
-
-      # Run mrconfig-updater if there's still no config
-      if [ ! -e "$mrconfig" ]; then
-        mrconfig_run="1"
-      else
-        local mrconfig_age="`status_file_changed_delta $mrconfig`"
-        local mrconfig_interval="3600"
-
-        # Run mrconfig-updater only every hour or so
-        if (( $mrconfig_age >= $mrconfig_interval )); then
-          mrconfig_run="1"
-        fi
-      fi
 
-      if [ "$mrconfig_run" == "1" ]; then
-        mrconfig-updater
-      fi
+      status_run_if_file_older_than $mrconfig 3600 mrconfig-updater
 
       # Pipe through cat so we don't have to deal with mr's dynamic output weirdness
       #mr -m status | cat