From c65a0a62d65248116cea26adb871d0713b32c704 Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Thu, 6 Jun 2024 10:54:36 +0500 Subject: [PATCH] In this release, I worked on the script to take notification minutes, hours, and weekdays. --- cmd/root.go | 2 +- interfaces/interfaces.go | 1 + logfile.log | 5 ++++ script.sh | 58 ++++++++++++++++++++++++++++++++++++ usecases/app/alert/alert.go | 59 +++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 logfile.log create mode 100644 script.sh diff --git a/cmd/root.go b/cmd/root.go index 57f5c22..6b55952 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.1.127" +const version = "v0.1.128" // rootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ diff --git a/interfaces/interfaces.go b/interfaces/interfaces.go index a2a13e9..e2d2ede 100644 --- a/interfaces/interfaces.go +++ b/interfaces/interfaces.go @@ -81,6 +81,7 @@ type ManageAlerts interface { AlertSetup(av *entities.AlertVariables) error SendAlert(category string) error SendNotification(category string) error + WriteNotificationValues(av *entities.AlertVariables) error } type AlertDB interface { diff --git a/logfile.log b/logfile.log new file mode 100644 index 0000000..2f86529 --- /dev/null +++ b/logfile.log @@ -0,0 +1,5 @@ +Thu Jun 6 10:50:01 PKT 2024: It's time! Running the desired command. +Thu Jun 6 10:51:01 PKT 2024: It's time! Running the desired command. +Thu Jun 6 10:52:01 PKT 2024: It's time! Running the desired command. +Thu Jun 6 10:53:01 PKT 2024: It's time! Running the desired command. +Thu Jun 6 10:54:01 PKT 2024: It's time! Running the desired command. diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..7a307f4 --- /dev/null +++ b/script.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Desired time in HH:MM format +DESIRED_TIME="10:52" + +DESIRED_WEEKDAY="friday" + +# Log file location +LOG_FILE="/mnt/d/go/src/github.com/ibilalkayy/flow/logfile.log" + +# Function to install and configure NTP +install_and_sync_ntp() { + # Check if NTP is installed + if ! command -v ntpd &> /dev/null; then + echo "$(date): NTP not found. Installing NTP..." >> "$LOG_FILE" + + # Install NTP based on the package manager available + if command -v apt-get &> /dev/null; then + sudo apt-get update && sudo apt-get install -y ntp + elif command -v yum &> /dev/null; then + sudo yum install -y ntp + elif command -v dnf &> /dev/null; then + sudo dnf install -y ntp + else + exit 1 + fi + fi + + # Start and enable NTP service + if command -v systemctl &> /dev/null; then + sudo systemctl start ntpd + sudo systemctl enable ntpd + elif command -v service &> /dev/null; then + sudo service ntp start + sudo update-rc.d ntp defaults + fi + + # Force synchronize the clock immediately + if command -v ntpd &> /dev/null; then + sudo ntpd -gq + elif command -v ntpdate &> /dev/null; then + sudo ntpdate pool.ntp.org + fi +} + +# Install and synchronize NTP +install_and_sync_ntp + +# Get the current time in HH:MM format +CURRENT_TIME=$(date +"%H:%M") + +CURRENT_WEEKDAY=$(date +"%A") + +# Check if the current time matches the desired time +DESIRED_WEEKDAY="friday" + # Run your command and log the output + echo "$(date): It's time! Running the desired command." >> "$LOG_FILE" +fi \ No newline at end of file diff --git a/usecases/app/alert/alert.go b/usecases/app/alert/alert.go index 7f832da..4483181 100644 --- a/usecases/app/alert/alert.go +++ b/usecases/app/alert/alert.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "os" "strings" "time" @@ -86,12 +87,23 @@ func (h MyAlert) SendAlert(category string) error { return errors.New("wrong weekday is selected") } + values := entities.AlertVariables{ + Category: category, + Hours: hour, + Minutes: minute, + Weekdays: weekdayStr, + } + switch method { case "email": switch frequency { case "hourly": h.Deps.SpendAmount.HourlyNotification(category) case "daily": + err := h.Deps.ManageAlerts.WriteNotificationValues(&values) + if err != nil { + return err + } h.Deps.SpendAmount.DailyNotification(hour, minute, category) case "weekly": h.Deps.SpendAmount.WeeklyNotification(weekday, hour, minute, category) @@ -106,6 +118,53 @@ func (h MyAlert) SendAlert(category string) error { return nil } +func (h MyAlert) WriteNotificationValues(av *entities.AlertVariables) error { + hours := av.Hours + minutes := av.Minutes + + desiredWeekday := av.Weekdays + desiredTime := fmt.Sprintf("%d:%d", hours, minutes) + + scriptFile := "/mnt/d/go/src/github.com/ibilalkayy/flow/script.sh" + + input, err := os.ReadFile(scriptFile) + if err != nil { + log.Fatal(err) + } + + value, err := h.Deps.AlertDB.ViewAlert(av.Category) + if err != nil { + return err + } + + category, ok := value[1].(string) + if !ok { + return errors.New("unable to convert to string") + } + + if len(av.Category) != 0 && av.Category == category { + lines := strings.Split(string(input), "\n") + for i, line := range lines { + if strings.Contains(line, "DESIRED_TIME=") { + lines[i] = fmt.Sprintf(`DESIRED_TIME="%s"`, desiredTime) + } + if strings.Contains(line, "DESIRED_WEEKDAY") { + lines[i] = fmt.Sprintf(`DESIRED_WEEKDAY="%s"`, desiredWeekday) + } + } + + output := strings.Join(lines, "\n") + err = os.WriteFile(scriptFile, []byte(output), 0644) + if err != nil { + return err + } + } else { + return errors.New("category is not present in the alert") + } + + return nil +} + func (h MyAlert) SendNotification(category string) error { budgetDetails, err := h.Deps.ManageBudget.ViewBudget(category) if err != nil {