From 133c0fa31b3b71c85055f3eb2ad12c8dbd4a916f Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Thu, 27 Apr 2023 13:56:22 +0300
Subject: [PATCH] add simple reload

---
 laika.toml  |  2 +-
 src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/laika.toml b/laika.toml
index ba014bd..607b602 100644
--- a/laika.toml
+++ b/laika.toml
@@ -6,5 +6,5 @@ delay = 2
 start = "11:50"
 #end = { hour = 12, minute = 55 }
 end = "12:55"
-
+#exit_now = true
 
diff --git a/src/main.rs b/src/main.rs
index 8a74d5c..c44112b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ use itertools::Itertools;
 use serde::Deserialize;
 use std::path::Path;
 use std::str::FromStr;
+use std::time::SystemTime;
 use std::{thread, time::Duration};
 use sysinfo::{System, SystemExt};
 
@@ -70,6 +71,9 @@ impl TryFrom<String> for TimeHM {
 /// Application config
 #[derive(Deserialize, Default, Debug)]
 struct Config {
+    /// modification time
+    #[serde(skip)]
+    modified: Option<SystemTime>,
     /// is application active
     #[serde(default = "default_true")]
     active: bool,
@@ -118,15 +122,49 @@ impl Config {
         }
 
         let path = path.unwrap();
-        let cfg = Config::read(path);
+        let cfg = Config::read(&path);
         match cfg {
             Err(_e) => {
                 //println!("{}", _e);
                 Config::default()
             }
-            Ok(cfg) => cfg,
+            Ok(mut cfg) => {
+                let metadata = fs::metadata(&path);
+                if let Ok(meta) = metadata {
+                    if let Ok(time) = meta.modified() {
+                        cfg.modified = Some(time);
+                    }
+                }
+                cfg
+            }
         }
     }
+    fn is_file_modified(&self) -> bool {
+        if self.modified.is_none() {
+            return true;
+        }
+        let path = Config::file_location();
+        if let Err(_e) = path {
+            return false;
+        }
+        let path = path.unwrap();
+        let metadata = fs::metadata(path);
+        let mut result = false;
+        if let Ok(meta) = metadata {
+            if let Ok(time) = meta.modified() {
+                result = self.modified.unwrap() < time
+            }
+        }
+        result
+    }
+    fn reload(&mut self) {
+        if !self.is_file_modified() {
+            return;
+        }
+
+        let new_cfg = Self::get();
+        *self = new_cfg;
+    }
     fn target_name(&self) -> String {
         if !self.target.is_empty() {
             self.target.clone()
@@ -183,11 +221,12 @@ impl Laika {
         let _res = cmd.spawn();
     }
 
-    fn main_loop(self) {
+    fn main_loop(mut self) {
         if !self.config.is_valid() {
             return;
         }
         loop {
+            self.config.reload();
             if self.config.exit_now {
                 return;
             } else if self.is_active() && !self.is_target_alive() {