diff --git a/cmd/kiwimix/config.go b/cmd/kiwimix/config.go index 5d11ea5..3908717 100644 --- a/cmd/kiwimix/config.go +++ b/cmd/kiwimix/config.go @@ -12,7 +12,7 @@ import ( ) const ( - programBinary string = "musala" + programBinary string = "kiwimix" ) var ( diff --git a/go.mod b/go.mod index d9649bf..85a7ac4 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,12 @@ require ( github.com/PuloV/ics-golang v0.0.0-20190808201353-a3394d3bcade github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 + github.com/stretchr/testify v1.8.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect ) require ( diff --git a/pkg/basetypes/calendar.go b/pkg/basetypes/calendar.go index b26562c..c6d5344 100644 --- a/pkg/basetypes/calendar.go +++ b/pkg/basetypes/calendar.go @@ -14,19 +14,42 @@ func NewCalendar() *Calendar { return &Calendar{*ics.NewCalendar()} } -// SetEvent adds an event to the calendar -func (cal *Calendar) SetEvent(event Event) (*Calendar, error) { - c, err := cal.SetEvent(event) - return c, err +// ParseCalendarFile parses an ICS file and returns a calendar +func ParseCalendarFile(filePath string) (*Calendar, error) { + return &Calendar{*ics.NewCalendar()}, nil +} + +// AddEvent adds an event to the calendar +func (cal *Calendar) AddEvent(event *Event) (*Calendar, error) { + //c, err := cal.SetEvent(event) + return cal, nil +} + +// AddEvents adds all events to the calendar +func (cal *Calendar) AddEvents(event []*Event) (*Calendar, error) { + //c, err := cal.SetEvent(event) + return cal, nil +} + +// AddEvent adds an event to the calendar +func (cal *Calendar) RemoveEvent(event *Event) (*Calendar, error) { + // c, err := cal.SetEvent(event) + return cal, nil +} + +// AddEvent adds an event to the calendar +func (cal *Calendar) RemoveEvents(event []*Event) (*Calendar, error) { + // c, err := cal.SetEvent(event) + return cal, nil } // GetEvents gets all events in the calendar -func (cal *Calendar) GetEvents() []Event { +func (cal *Calendar) GetEvents() []*Event { subcal := cal.Calendar subevents := subcal.GetEvents() - wrapped := make([]Event, len(subevents)) + wrapped := make([]*Event, len(subevents)) for i, v := range subevents { - wrapped[i] = Event{v} + wrapped[i] = &Event{v} } return wrapped } diff --git a/pkg/basetypes/calendar_test.go b/pkg/basetypes/calendar_test.go new file mode 100644 index 0000000..e67f06e --- /dev/null +++ b/pkg/basetypes/calendar_test.go @@ -0,0 +1,13 @@ +package basetypes_test + +import ( + "testing" + + "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" + "github.com/stretchr/testify/assert" +) + +func TestCalendarFoo(t *testing.T) { + cal = basetypes.NewCalendar() + assert.Equal(t, cal, nil, "they should be equal") +} diff --git a/pkg/basetypes/event.go b/pkg/basetypes/event.go index 7eb1ce9..edf7b9c 100644 --- a/pkg/basetypes/event.go +++ b/pkg/basetypes/event.go @@ -6,3 +6,7 @@ import ics "github.com/PuloV/ics-golang" type Event struct { ics.Event } + +func (event *Event) Equals(other *Event) bool { + return false +} diff --git a/pkg/basetypes/stack.go b/pkg/basetypes/stack.go index 5eec800..cccae11 100644 --- a/pkg/basetypes/stack.go +++ b/pkg/basetypes/stack.go @@ -1 +1,9 @@ package basetypes + +type Stack struct { + calendars []Calendar +} + +func (stack *Stack) Push(calendar *Calendar) (*Stack, error) { + return nil, nil +} diff --git a/pkg/calendarops/boolean_difference.go b/pkg/calendarops/boolean_difference.go index 07f9f5c..85f2cef 100644 --- a/pkg/calendarops/boolean_difference.go +++ b/pkg/calendarops/boolean_difference.go @@ -13,10 +13,10 @@ type DifferenceCalendarOperation struct{} // Ensure DifferenceCalendarOperation implements CalendarOperation var _ CalendarOperation = (*DifferenceCalendarOperation)(nil) -func (op DifferenceCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { +func (op *DifferenceCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) { // Check that two calendars are provided if len(calendars) != 2 { - return basetypes.Calendar{}, errors.New("difference operation requires exactly two calendars") + return &basetypes.Calendar{}, errors.New("difference operation requires exactly two calendars") } // Create a new calendar to store the difference @@ -38,7 +38,7 @@ func (op DifferenceCalendarOperation) Execute(calendars ...basetypes.Calendar) ( // If the event is unique to the first calendar, add it to the difference calendar if isUnique { - differenceCalendar.SetEvent(event1) + differenceCalendar.AddEvent(event1) } } diff --git a/pkg/calendarops/boolean_intersect.go b/pkg/calendarops/boolean_intersect.go index fa34c30..e9dde09 100644 --- a/pkg/calendarops/boolean_intersect.go +++ b/pkg/calendarops/boolean_intersect.go @@ -13,9 +13,9 @@ type IntersectCalendarOperation struct{} // Ensure IntersectCalendarOperation implements CalendarOperation var _ CalendarOperation = (*IntersectCalendarOperation)(nil) -func (op IntersectCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { +func (op *IntersectCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) { if len(calendars) != 2 { - return basetypes.Calendar{}, errors.New("Difference operation requires exactly two calendars") + return &basetypes.Calendar{}, errors.New("Difference operation requires exactly two calendars") } // Do your union operation here and return the new union basetypes. return calendars[0], nil // Replace this with actual implementation diff --git a/pkg/calendarops/boolean_union.go b/pkg/calendarops/boolean_union.go index b45903c..31d853a 100644 --- a/pkg/calendarops/boolean_union.go +++ b/pkg/calendarops/boolean_union.go @@ -15,9 +15,9 @@ type UnionCalendarOperation struct { var _ CalendarOperation = (*UnionCalendarOperation)(nil) // Execute combines the events of two calendars and returns a new calendar that contains all events from both. -func (op *UnionCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { +func (op *UnionCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) { if len(calendars) != 2 { - return basetypes.Calendar{}, errors.New("Union operation requires exactly two calendars") + return &basetypes.Calendar{}, errors.New("Union operation requires exactly two calendars") } // Do your union operation here and return the new union basetypes. return calendars[0], nil // Replace this with actual implementation diff --git a/pkg/calendarops/calendar_operation.go b/pkg/calendarops/calendar_operation.go index e7a442c..00466d4 100644 --- a/pkg/calendarops/calendar_operation.go +++ b/pkg/calendarops/calendar_operation.go @@ -4,5 +4,5 @@ import basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" // CalendarOperation defines the interface for operations on calendars. type CalendarOperation interface { - Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) + Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) } diff --git a/pkg/calendarops/filter.go b/pkg/calendarops/filter.go index 8c13021..c1b91a5 100644 --- a/pkg/calendarops/filter.go +++ b/pkg/calendarops/filter.go @@ -15,9 +15,9 @@ type FilterCalendarOperation struct { var _ CalendarOperation = (*FilterCalendarOperation)(nil) // Execute filters the events of a calendar and returns a new filtered calendar. -func (op *FilterCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { +func (op *FilterCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) { if len(calendars) != 1 { - return basetypes.Calendar{}, errors.New("Filter operation requires exactly one calendar") + return &basetypes.Calendar{}, errors.New("Filter operation requires exactly one calendar") } // Do your filter operation here and return the filtered calendar. return calendars[0], nil // Replace this with actual implementation diff --git a/pkg/calendarops/merge.go b/pkg/calendarops/merge.go index 046bb3b..9bb6308 100644 --- a/pkg/calendarops/merge.go +++ b/pkg/calendarops/merge.go @@ -1,8 +1,6 @@ package calendarops import ( - "sort" - basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" ) @@ -19,27 +17,33 @@ func NewMergeCalendarOperation() *MergeCalendarOperation { } // Execute réalise l'opération de fusion. -func (op *MergeCalendarOperation) Execute(cal ...basetypes.Calendar) (basetypes.Calendar, error) { - if len(cal.Events) == 0 { - return cal, nil +func (op *MergeCalendarOperation) Execute(cal ...*basetypes.Calendar) (*basetypes.Calendar, error) { + // Guard: exit if no calendar provided + if len(cal) == 0 { + return nil, nil } - sort.Slice(cal.Event, func(i, j int) bool { - return cal.Event[i].Start.Before(cal.Event[j].Start) - }) + mergedEvents := cal[0].GetEvents() + /* + events := cal.GetEvents() + sort.Slice(cal.Event, func(i, j int) bool { + return cal.Event[i].Start.Before(cal.Event[j].Start) + }) - mergedEvents := []basetypes.Event{cal.Event[0]} + mergedEvents := []basetypes.Event{cal.Event[0]} - for _, event := range cal.Event[1:] { - lastEvent := &mergedEvents[len(mergedEvents)-1] - if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) { - if event.End.After(lastEvent.End) { - lastEvent.End = event.End + for _, event := range cal.Event[1:] { + lastEvent := &mergedEvents[len(mergedEvents)-1] + if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) { + if event.End.After(lastEvent.End) { + lastEvent.End = event.End + } + } else { + mergedEvents = append(mergedEvents, event) } - } else { - mergedEvents = append(mergedEvents, event) } - } - - return &basetypes.Calendar{Events: mergedEvents}, nil + */ + mergedCalendar := &basetypes.Calendar{} + mergedCalendar.AddEvents(mergedEvents) + return mergedCalendar, nil } diff --git a/pkg/stackops/generate.go b/pkg/stackops/generate.go index 9977f5b..ad8fa6f 100644 --- a/pkg/stackops/generate.go +++ b/pkg/stackops/generate.go @@ -1,16 +1,24 @@ package stackops -import "time" +import ( + "time" + + basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" +) // GeneratorLimit is a type definining type GenerateConfig struct { - DateBegin *time.Date - DateEnd *time.Date + DateBegin *time.Time + DateEnd *time.Time TimeBegin *time.Time TimeEnd *time.Time } +type GenerateStackOperation struct{} + +var _ StackOperation = (*GenerateStackOperation)(nil) + // generate events that last all day (from X hour to Y hour) -func (cal *Calendar) generate() *Calendar { - return nil +func (op *GenerateStackOperation) Execute(stack *basetypes.Stack, str string) (*basetypes.Stack, error) { + return nil, nil } diff --git a/pkg/stackops/load.go b/pkg/stackops/load.go index 7365e72..8b27809 100644 --- a/pkg/stackops/load.go +++ b/pkg/stackops/load.go @@ -6,8 +6,8 @@ import ( type LoadOperation struct{} -func (op *LoadOperation) Execute(stack *basetypes.CalendarStack, filePath string) error { - cal, err := basetypes.ParseICSFile(filePath) // hypothetically, ParseICSFile parses an ICS file into a Calendar object +func (op *LoadOperation) Execute(stack *basetypes.Stack, filePath string) error { + cal, err := basetypes.ParseCalendarFile(filePath) // hypothetically, ParseICSFile parses an ICS file into a Calendar object if err != nil { return err } diff --git a/pkg/stackops/save.go b/pkg/stackops/save.go index 7215c31..732386c 100644 --- a/pkg/stackops/save.go +++ b/pkg/stackops/save.go @@ -13,7 +13,7 @@ func NewSaveOperation(filePath string) *SaveOperation { return saveop } -func (op *SaveOperation) Execute(stack *basetypes.CalendarStack) error { +func (op *SaveOperation) Execute(stack *basetypes.Stack) error { cal, err := basetypes.ParseCalendarFile(op.filePath) if err != nil { return err diff --git a/pkg/stackops/stack_operation.go b/pkg/stackops/stack_operation.go index f5d6663..1ba4b30 100644 --- a/pkg/stackops/stack_operation.go +++ b/pkg/stackops/stack_operation.go @@ -1,5 +1,7 @@ package stackops +import basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" + type StackOperation interface { - Execute(stack *CalendarStack, filePath string) error + Execute(stack *basetypes.Stack, filePath string) (*basetypes.Stack, error) }