Skip to content

Commit

Permalink
Make durations configurable (#4)
Browse files Browse the repository at this point in the history
* add config options for frequency, eventdays and emailhours

* add taskfile to build and publish
  • Loading branch information
nakamume authored Sep 9, 2024
1 parent 8c020d4 commit 5f359bf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
builds/
.env
config.json
24 changes: 12 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -56,32 +56,32 @@ 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)
}
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)
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -15,7 +17,8 @@
"imap": {
"host": "mail.example.org",
"username": "[email protected]",
"password": "password1"
"password": "password1",
"emailHours": 6
}
}
]
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 11 additions & 25 deletions pkg/user/user.go → pkg/config/user.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -21,33 +20,20 @@ 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
}
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
Expand Down
38 changes: 38 additions & 0 deletions taskfile.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5f359bf

Please sign in to comment.