Skip to content

Commit

Permalink
In this release, I worked on the script to take notification minutes,…
Browse files Browse the repository at this point in the history
… hours, and weekdays.
  • Loading branch information
ibilalkayy committed Jun 6, 2024
1 parent d4f32a3 commit c65a0a6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions logfile.log
Original file line number Diff line number Diff line change
@@ -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.
58 changes: 58 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions usecases/app/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit c65a0a6

Please sign in to comment.