Skip to content

Commit

Permalink
updated the script file to include the day also
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Jun 17, 2024
1 parent 39a8b01 commit 4ad9437
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
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.129"
const version = "v0.1.130"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
53 changes: 9 additions & 44 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,23 @@
#!/bin/bash

# Desired time in HH:MM format
DESIRED_TIME="19:31"
DESIRED_TIME="21:11"

DESIRED_WEEKDAY="sunday"
DESIRED_DAY="17"

# Log file location
LOG_FILE="${SCRIPT_PATH}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
DESIRED_WEEKDAY="Monday"

# Get the current time in HH:MM format
CURRENT_TIME=$(date +"%H:%M")

CURRENT_DAY=$(date +"%d")

CURRENT_WEEKDAY=$(date +"%A")

# Check if the current time matches the desired time
DESIRED_WEEKDAY="sunday"
if [ "$CURRENT_TIME" == "$DESIRED_TIME" ] && [ "$CURRENT_WEEKDAY" == "$DESIRED_WEEKDAY" ]; then
# Run your command and log the output
echo "$(date): It's time! Running the desired command." >> "$LOG_FILE"
echo "$(date): It's time! Running the desired command."
else
echo "not set ${CURRENT_WEEKDAY} ${CURRENT_TIME} ${CURRENT_DAY}"
fi
14 changes: 12 additions & 2 deletions usecases/app/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"
"time"
"unicode"

"github.com/ibilalkayy/flow/entities"
"github.com/ibilalkayy/flow/handler"
Expand Down Expand Up @@ -91,6 +92,7 @@ func (h MyAlert) SendAlert(category string) error {
Category: category,
Hours: hour,
Minutes: minute,
Days: day,
Weekdays: weekdayStr,
}

Expand Down Expand Up @@ -122,10 +124,15 @@ func (h MyAlert) WriteNotificationValues(av *entities.AlertVariables) error {
hours := av.Hours
minutes := av.Minutes

desiredDay := av.Days
desiredWeekday := av.Weekdays
desiredTime := fmt.Sprintf("%d:%d", hours, minutes)
desiredTime := fmt.Sprintf("%02d:%02d", hours, minutes)

// Convert to upper letter
if len(desiredWeekday) > 0 {
desiredWeekday = string(unicode.ToUpper(rune(desiredWeekday[0]))) + strings.ToLower(desiredWeekday[1:])
}

// Retrieve the SCRIPT_PATH environment variable
scriptPath := os.Getenv("SCRIPT_PATH")
if scriptPath == "" {
return errors.New("SCRIPT_PATH environment variable is not set")
Expand Down Expand Up @@ -153,6 +160,9 @@ func (h MyAlert) WriteNotificationValues(av *entities.AlertVariables) error {
if strings.Contains(line, "DESIRED_TIME=") {
lines[i] = fmt.Sprintf(`DESIRED_TIME="%s"`, desiredTime)
}
if strings.Contains(line, "DESIRED_DAY=") {
lines[i] = fmt.Sprintf(`DESIRED_DAY="%d"`, desiredDay)
}
if strings.Contains(line, "DESIRED_WEEKDAY=") {
lines[i] = fmt.Sprintf(`DESIRED_WEEKDAY="%s"`, desiredWeekday)
}
Expand Down

0 comments on commit 4ad9437

Please sign in to comment.