From 7a8090318cacb8697bd86d02cbc0861e7d84e87a Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Thu, 23 Nov 2023 18:38:35 +0100 Subject: [PATCH] Rename variable Names Makes TO_EMAIL parameter optional --- README.md | 2 +- cron-job.sh | 3 --- main.go | 30 ++++++++++++++---------------- 3 files changed, 15 insertions(+), 20 deletions(-) delete mode 100644 cron-job.sh diff --git a/README.md b/README.md index e7f2c21..97d6575 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Before you begin, ensure you have the following installed: Create a file named `.env` in your project directory. Fill this file with the required environment variables: ```env - TO_EMAIL=recipient@example.com + TO_EMAIL=recipient@example.com (optional) NTFY_AUTH=echo "Basic $(echo -n 'testuser:fakepassword' | base64)" NTFY_HOST=https://ntfy.sh/alerts ICS_DIR=/path/to/ics/files diff --git a/cron-job.sh b/cron-job.sh deleted file mode 100644 index 3c17e8c..0000000 --- a/cron-job.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -echo "0 6 * * * /usr/local/bin/docker-compose -f /opt/containers/mail-reminder/docker-compose.yml up --build --exit-code-from go-app" > /etc/crontabs/root -crond -f -l 8 diff --git a/main.go b/main.go index 53046a6..abbe25a 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,9 @@ import ( ) var ( - toAddresses []string - authString string - hostString string + TO_EMAIL string + NTFY_AUTH string + NTFY_HOST string ) func main() { @@ -25,9 +25,9 @@ func main() { log.Info("Hello World!") // Read environment variables - toAddresses = strings.Split(os.Getenv("TO_EMAIL"), ",") - authString = os.Getenv("NTFY_AUTH") - hostString = os.Getenv("NTFY_HOST") + TO_EMAIL = os.Getenv("TO_EMAIL") + NTFY_AUTH = os.Getenv("NTFY_AUTH") + NTFY_HOST = os.Getenv("NTFY_HOST") // Get the current working directory dir, err := os.Getwd() @@ -38,13 +38,13 @@ func main() { // Read and split multiple folders folderList := os.Getenv("ICS_DIR") - if folderList == "" || len(toAddresses) == 0 || authString == "" || hostString == "" { - log.Fatal("Es fehlen noch einige Parameter!!!\nICS_DIR, TO_EMAIL, NTFY_AUTH, NTFY_HOST") + if folderList == "" || NTFY_AUTH == "" || NTFY_HOST == "" { + log.Fatal("Es fehlen noch einige Parameter!!!\nICS_DIR, NTFY_AUTH, NTFY_HOST") } folders := strings.Split(folderList, ",") for _, folder := range folders { - log.Infof("folder: %s to: %s password: %s host: %s", folder, toAddresses, authString, hostString) + log.Infof("folder: %s to: %s password: %s host: %s", folder, TO_EMAIL, NTFY_AUTH, NTFY_HOST) listFilesForFolder(folder) } } @@ -108,9 +108,7 @@ func getNotifications(file string) { } messageText += "\n\n This message is a service from go-ical-ntfy-reminder Version 1.1 written in Golang. \n Delivered by Simon Rieger" - for _, toAddress := range toAddresses { - sendMessage(messageSubject, messageText, toAddress) - } + sendMessage(messageSubject, messageText) } } @@ -118,13 +116,13 @@ func truncateToDay(t time.Time, tz *time.Location) time.Time { return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, tz) } -func sendMessage(messageSubject string, messageText string, toAddress string) { +func sendMessage(messageSubject string, messageText string) { - req, _ := http.NewRequest("POST", hostString, + req, _ := http.NewRequest("POST", NTFY_HOST, strings.NewReader(messageText)) req.Header.Set("Title", messageSubject) - req.Header.Set("Authorization", "Basic "+authString) - req.Header.Set("Email", toAddress) + req.Header.Set("Authorization", "Basic "+NTFY_AUTH) + req.Header.Set("Email", TO_EMAIL) req.Header.Set("Tags", "date,octopus") req.Header.Set("Priority", "high") do, err := http.DefaultClient.Do(req)