From 5f359bf5bc4306992769bf122028e0fff651bad7 Mon Sep 17 00:00:00 2001 From: Umesh Date: Tue, 10 Sep 2024 01:01:33 +0900 Subject: [PATCH] Make durations configurable (#4) * add config options for frequency, eventdays and emailhours * add taskfile to build and publish --- .gitignore | 1 + cmd/main.go | 24 +++++++++++------------ config.example.json | 7 +++++-- go.sum | 1 - pkg/config/config.go | 21 ++++++++++++++++++++ pkg/{user => config}/user.go | 36 +++++++++++----------------------- taskfile.yaml | 38 ++++++++++++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 pkg/config/config.go rename pkg/{user => config}/user.go (54%) create mode 100644 taskfile.yaml diff --git a/.gitignore b/.gitignore index 265e634..89af2e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +builds/ .env config.json diff --git a/cmd/main.go b/cmd/main.go index 5973d40..b63b52f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,18 +11,18 @@ import ( "github.com/emersion/go-ical" "github.com/nakamorg/calbridge/pkg/backend" "github.com/nakamorg/calbridge/pkg/caldav" + "github.com/nakamorg/calbridge/pkg/config" "github.com/nakamorg/calbridge/pkg/email" - "github.com/nakamorg/calbridge/pkg/user" "github.com/nakamorg/calbridge/pkg/util" ) func main() { ctx := context.Background() var err error - var users []user.User + var users []config.User var storage backend.Backend - if users, err = user.LoadFromJson("config.json"); err != nil { + if users, err = config.LoadFromConfig("config.json"); err != nil { log.Fatal(err) } @@ -36,7 +36,7 @@ func main() { } } -func handleUser(ctx context.Context, user user.User, storage backend.Backend) error { +func handleUser(ctx context.Context, user config.User, storage backend.Backend) error { var err error var calClient *caldav.Client var smtpClient *email.SMTPClient @@ -56,25 +56,24 @@ func handleUser(ctx context.Context, user user.User, storage backend.Backend) er } defer imapClient.Close() - if err = sendInvites(ctx, user.Name, calClient, smtpClient, storage); err != nil { + if err = sendInvites(ctx, user.Name, user.CalDAV.EventDays, calClient, smtpClient, storage); err != nil { log.Fatal(err) } - if err = addInvites(ctx, user.Name, calClient, imapClient, storage); err != nil { + if err = addInvites(ctx, user.Name, user.IMAP.EmailHours, calClient, imapClient, storage); err != nil { log.Fatal(err) } return nil } -func sendInvites(ctx context.Context, username string, calClient *caldav.Client, smtpClient *email.SMTPClient, storage backend.Backend) error { +func sendInvites(ctx context.Context, username string, eventDays int, calClient *caldav.Client, smtpClient *email.SMTPClient, storage backend.Backend) error { var events []*ical.Calendar var err error var data backend.Data - if events, err = calClient.GetEvents(ctx, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 1, 0)); err != nil { + if events, err = calClient.GetEvents(ctx, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, eventDays)); err != nil { return fmt.Errorf("failed reading future events: %v", err) } - fmt.Println("sending") for _, event := range events { if data, err = eventBackendData(ctx, username, event, backend.DirectionOut, storage); err != nil { return fmt.Errorf("failed creating event backend data: %v", err) @@ -82,6 +81,7 @@ func sendInvites(ctx context.Context, username string, calClient *caldav.Client, if data.Synced || data.Direction != backend.DirectionOut { continue } + fmt.Println("sending") fmt.Println(event.Events()[0].Props) if err = smtpClient.SendCalendarInvite(event); err != nil { return fmt.Errorf("failed sending invitation: %v", err) @@ -95,15 +95,14 @@ func sendInvites(ctx context.Context, username string, calClient *caldav.Client, return nil } -func addInvites(ctx context.Context, username string, calClient *caldav.Client, imapClient *email.IMAPClient, storage backend.Backend) error { +func addInvites(ctx context.Context, username string, emailHours int, calClient *caldav.Client, imapClient *email.IMAPClient, storage backend.Backend) error { var events []*ical.Calendar var err error var data backend.Data - if events, err = imapClient.ReadCalendarInvites(3); err != nil { + if events, err = imapClient.ReadCalendarInvites(emailHours); err != nil { return fmt.Errorf("failed reading emails: %v", err) } - fmt.Println("adding") for _, event := range events { if data, err = eventBackendData(ctx, username, event, backend.DirectionIn, storage); err != nil { @@ -115,6 +114,7 @@ func addInvites(ctx context.Context, username string, calClient *caldav.Client, if err := calClient.PutEvent(ctx, event); err != nil { return fmt.Errorf("failed adding event: %v", err) } + fmt.Println("adding") fmt.Println(event.Events()[0].Props) data.Synced = true data.SyncedTime = time.Now() diff --git a/config.example.json b/config.example.json index e5bfbd8..8e6bc56 100644 --- a/config.example.json +++ b/config.example.json @@ -2,10 +2,12 @@ "users": [ { "name": "user1", + "frequency": "1h", "caldav": { "url": "https://caldav.example.com/calendars/user1/xyz/", "username": "user1", - "password": "password1" + "password": "password1", + "eventDays": 5 }, "smtp": { "host": "mail.example.org", @@ -15,7 +17,8 @@ "imap": { "host": "mail.example.org", "username": "user1@example.org", - "password": "password1" + "password": "password1", + "emailHours": 6 } } ] diff --git a/go.sum b/go.sum index e1c2920..295b714 100644 --- a/go.sum +++ b/go.sum @@ -26,7 +26,6 @@ github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0= go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I= -go.etcd.io/gofail v0.1.0/go.mod h1:VZBCXYGZhHAinaBiiqYvuDynvahNsAyLFwB3kEHKz1M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..027ee8b --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,21 @@ +package config + +import ( + "encoding/json" + "os" +) + +type config struct { + Users []User `json:"users"` +} + +// loadConfig reads and returns the json config +func loadConfig(path string) (config, error) { + var conf config + data, err := os.ReadFile(path) + if err != nil { + return conf, err + } + err = json.Unmarshal(data, &conf) + return conf, err +} diff --git a/pkg/user/user.go b/pkg/config/user.go similarity index 54% rename from pkg/user/user.go rename to pkg/config/user.go index b078b40..cb908bd 100644 --- a/pkg/user/user.go +++ b/pkg/config/user.go @@ -1,16 +1,15 @@ -package user - -import ( - "encoding/json" - "os" -) +package config type User struct { - Name string `json:"name"` - CalDAV struct { + Name string `json:"name"` + // How often events and emails are checked, parsed as golang time. Ex: 30m, 1h, 3h etc + Frequency string `json:"frequency"` + CalDAV struct { URL string `json:"url"` Username string `json:"username"` Password string `json:"password"` + // Number of upcoming days for which to read CalDAV events and send invitations. + EventDays int `json:"eventDays"` } `json:"caldav"` SMTP struct { Host string `json:"host"` @@ -21,15 +20,13 @@ type User struct { Host string `json:"host"` Username string `json:"username"` Password string `json:"password"` + // Number of past hours from which to read emails for calendar invites. + EmailHours int `json:"emailHours"` } `json:"imap"` } -type config struct { - Users []User `json:"users"` -} - -// LoadFromJson reads a json file containing calbridge user information and returns the Users -func LoadFromJson(path string) ([]User, error) { +// LoadFromConfig reads a json file containing calbridge user information and returns the Users +func LoadFromConfig(path string) ([]User, error) { config, err := loadConfig(path) if err != nil { return nil, err @@ -37,17 +34,6 @@ func LoadFromJson(path string) ([]User, error) { return config.Users, err } -// loadConfig reads and returns the json config -func loadConfig(path string) (config, error) { - var conf config - data, err := os.ReadFile(path) - if err != nil { - return conf, err - } - err = json.Unmarshal(data, &conf) - return conf, err -} - // LoadFromDB reads calbridge users info from db and returns func LoadFromDB(path string) ([]User, error) { var users []User diff --git a/taskfile.yaml b/taskfile.yaml new file mode 100644 index 0000000..b0c0f79 --- /dev/null +++ b/taskfile.yaml @@ -0,0 +1,38 @@ +version: '3' + +vars: + NEXT_VERSION: + sh: | + VERSION=$(git tag --sort=-v:refname | head -n 1) + MAJOR=$(echo $VERSION | cut -d. -f1) + MINOR=$(echo $VERSION | cut -d. -f2) + PATCH=$(echo $VERSION | cut -d. -f3) + NEW_TAG="$MAJOR.$MINOR.$((PATCH+1))" + echo "$NEW_TAG" + +tasks: + build: + desc: Build the binary for different platforms + cmds: + - go mod tidy + - GOOS=darwin GOARCH=arm64 go build -o builds/calbridge-darwin-arm64 cmd/main.go + - GOOS=linux GOARCH=amd64 go build -o builds/calbridge-linux-amd64 cmd/main.go + - GOOS=linux GOARCH=arm64 go build -o builds/calbridge-linux-arm64 cmd/main.go + env: + CGO_ENABLED: 0 + + release: + desc: Create a new GitHub release and upload binaries + cmds: + - | + VERSION=${VERSION_OVERRIDE:-{{.NEXT_VERSION}}} + gh release create "$VERSION" builds/* --title "Release $VERSION" --notes "Release $VERSION" + - git fetch --tags + env: + VERSION_OVERRIDE: '' + + all: + desc: Build and release + cmds: + - task: build + - task: release