Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added merge feature #18

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/adapters/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func GetAdapters() []Adapter {
&DeletePropertyAdapter{},
&FilterAdapter{},
&PrintAdapter{},
&MergeAdapter{},
}
}
56 changes: 56 additions & 0 deletions internal/adapters/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package adapters

import (
"os"

"github.com/akamensky/argparse"
"github.com/dploeger/icarus/v2/pkg/processors"
"github.com/emersion/go-ical"
)

// The FilterAdapter filters the calendar for selected events
type MergeAdapter struct {
toolbox processors.Toolbox
mergeCalendar *os.File
overwrite *bool
mergeProps *[]string
}

func (a *MergeAdapter) Initialize(parser *argparse.Parser) (*argparse.Command, error) {
command := parser.NewCommand("merge", "Merge another calendar into the input calendar. The filter options work on the additional calendar for this comand.")
a.mergeCalendar = command.File("I", "merge-calendar", os.O_RDONLY, 0600, &argparse.Options{
Help: "Calendar to merge into the input calendar",
Required: true,
})
a.overwrite = command.Flag("O", "overwrite", &argparse.Options{
Help: "Overwrite events in the input calendar with those from the other calendar",
})
a.mergeProps = command.StringList("P", "merge-props", &argparse.Options{
Help: "The ical event properties which decide if two events are the same",
Default: []string{ical.PropSummary, ical.PropDateTimeStart, ical.PropDateTimeEnd},
})
return command, nil
}

func (a *MergeAdapter) Process(input ical.Calendar, output *ical.Calendar) error {
var mergeCalendar ical.Calendar
dec := ical.NewDecoder(a.mergeCalendar)
if cal, err := dec.Decode(); err != nil {
return err
} else {
mergeCalendar = *cal
}
p := processors.NewMergeProcessor(mergeCalendar)
p.MergeProps = *a.mergeProps
if *a.overwrite {
p.MergeOption = processors.MergeOptionOverWrite
}
p.SetToolbox(a.toolbox)
return p.Process(input, output)
}

func (f *MergeAdapter) SetToolbox(toolbox processors.Toolbox) {
f.toolbox = toolbox
}

var _ Adapter = &MergeAdapter{}
2 changes: 1 addition & 1 deletion internal/adapters/testdata/script/filter.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
stdin example.ics
exec icarus filter -s Test2
! stderr .
! stdout SUMMARY:Test$
! stdout 'SUMMARY:Test\r\n'

-- example.ics --
BEGIN:VCALENDAR
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/testdata/script/filter_dates_start.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
stdin example.ics
exec icarus filter -b 2024-01-24T00:00:00Z
! stderr .
! stdout SUMMARY:Test$
! stdout 'SUMMARY:Test\r\n'

-- example.ics --
BEGIN:VCALENDAR
Expand Down
108 changes: 108 additions & 0 deletions internal/adapters/testdata/script/merge.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
stdin example.ics
exec icarus merge -I examplemerge.ics
! stderr .
stdout 'SUMMARY:Test\r\n'
stdout 'SUMMARY:Test3\r\n'

-- example.ics --
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Apple Inc.//macOS 14.2.1//EN
VERSION:2.0
X-APPLE-CALENDAR-COLOR:#1BADF8
X-WR-CALNAME:example
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
DTSTART:19810329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
TZNAME:MESZ
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:19961027T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
TZNAME:MEZ
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20240123T214815Z
DTEND;TZID=Europe/Berlin:20240123T113000
DTSTAMP:20240123T214844Z
DTSTART;TZID=Europe/Berlin:20240123T101500
LAST-MODIFIED:20240123T214815Z
SEQUENCE:0
SUMMARY:Test
TRANSP:OPAQUE
UID:B9243EBE-1F38-477B-A21C-D9C0BD61C025
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
BEGIN:VEVENT
CREATED:20240123T214829Z
DTEND;VALUE=DATE:20240125
DTSTAMP:20240123T214844Z
DTSTART;VALUE=DATE:20240124
LAST-MODIFIED:20240123T214829Z
SEQUENCE:0
SUMMARY:Test2
TRANSP:TRANSPARENT
UID:87C40F70-6E3F-4409-B8DC-874BA54C4048
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR

-- examplemerge.ics --
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Apple Inc.//macOS 14.2.1//EN
VERSION:2.0
X-APPLE-CALENDAR-COLOR:#1BADF8
X-WR-CALNAME:example
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
DTSTART:19810329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
TZNAME:MESZ
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:19961027T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
TZNAME:MEZ
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20240123T214815Z
DTEND;TZID=Europe/Berlin:20240223T113000
DTSTAMP:20240123T214844Z
DTSTART;TZID=Europe/Berlin:20240223T101500
LAST-MODIFIED:20240123T214815Z
SEQUENCE:0
SUMMARY:Test3
TRANSP:OPAQUE
UID:B9243EBE-1F38-477B-A21C-D9C0BD61C025
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR
126 changes: 126 additions & 0 deletions internal/adapters/testdata/script/merge_filter.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
stdin example.ics
exec icarus merge -I examplemerge.ics -s Test3
! stderr .
stdout 'SUMMARY:Test\r\n'
stdout 'SUMMARY:Test3\r\n'
! stdout 'SUMMARY:Test4\r\n'

-- example.ics --
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Apple Inc.//macOS 14.2.1//EN
VERSION:2.0
X-APPLE-CALENDAR-COLOR:#1BADF8
X-WR-CALNAME:example
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
DTSTART:19810329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
TZNAME:MESZ
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:19961027T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
TZNAME:MEZ
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20240123T214815Z
DTEND;TZID=Europe/Berlin:20240123T113000
DTSTAMP:20240123T214844Z
DTSTART;TZID=Europe/Berlin:20240123T101500
LAST-MODIFIED:20240123T214815Z
SEQUENCE:0
SUMMARY:Test
TRANSP:OPAQUE
UID:B9243EBE-1F38-477B-A21C-D9C0BD61C025
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
BEGIN:VEVENT
CREATED:20240123T214829Z
DTEND;VALUE=DATE:20240125
DTSTAMP:20240123T214844Z
DTSTART;VALUE=DATE:20240124
LAST-MODIFIED:20240123T214829Z
SEQUENCE:0
SUMMARY:Test2
TRANSP:TRANSPARENT
UID:87C40F70-6E3F-4409-B8DC-874BA54C4048
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR

-- examplemerge.ics --
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Apple Inc.//macOS 14.2.1//EN
VERSION:2.0
X-APPLE-CALENDAR-COLOR:#1BADF8
X-WR-CALNAME:example
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
DTSTART:19810329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
TZNAME:MESZ
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:19961027T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
TZNAME:MEZ
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20240123T214815Z
DTEND;TZID=Europe/Berlin:20240223T113000
DTSTAMP:20240123T214844Z
DTSTART;TZID=Europe/Berlin:20240223T101500
LAST-MODIFIED:20240123T214815Z
SEQUENCE:0
SUMMARY:Test3
TRANSP:OPAQUE
UID:B9243EBE-1F38-477B-A21C-D9C0BD61C025
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
BEGIN:VEVENT
CREATED:20240123T214815Z
DTEND;TZID=Europe/Berlin:20240223T113000
DTSTAMP:20240123T214844Z
DTSTART;TZID=Europe/Berlin:20240223T101500
LAST-MODIFIED:20240123T214815Z
SEQUENCE:0
SUMMARY:Test4
TRANSP:OPAQUE
UID:B9243EBE-1F38-477B-A21C-D9C0BD61C025
X-APPLE-CREATOR-IDENTITY:com.apple.calendar
X-APPLE-CREATOR-TEAM-IDENTITY:0000000000
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR
Loading
Loading